diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index d5aab01..b87e673 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -14,10 +14,10 @@ funder_templates, org_templates, organisation_templates = [], [], [] # Get all of the unique template family ids (dmptemplate_id) for each funder and the current org - funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.where(published: true).valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq + funder_ids = Template.valid_published().joins(:org).where(Org.funder_condition).pluck(:dmptemplate_id) org_ids = current_user.org.templates.where(customization_of: nil).valid.collect{|t| t.dmptemplate_id }.flatten.uniq - # Get all of the unique template family ids (dmptemplate_id) for each organisation type - organisation_ids = Org.organisation.includes(:templates).where( templates: { published: true, migrated: false, is_default: true }).collect{ |org| org.templates.collect{| ot| ot.dmptemplate_id } }.flatten.uniq + organisation_ids = Template.valid_published(is_default: true).joins(:org) + .where(Org.organisation_condition).where(["org_id <> ?", current_user.org_id]).pluck(:dmptemplate_id) org_ids.each do |id| current = Template.current(id) diff --git a/app/models/template.rb b/app/models/template.rb index b72d216..956f5b6 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -2,7 +2,7 @@ include GlobalHelpers before_validation :set_creation_defaults - scope :valid, -> {where(migrated: false)} + ## # Associations belongs_to :org @@ -28,6 +28,14 @@ validates :org, :title, :version, presence: {message: _("can't be blank")} + scope :valid, -> { where(migrated: false) } + scope :published, -> { where(published: true) } + + # Retrieves all valid and published templates + scope :valid_published, -> (is_default: false) { + Template.where(templates: { is_default: is_default }).valid().published() + } + # Retrieves the list of all dmptemplate_ids (template versioning families) for the specified Org def self.dmptemplate_ids Template.all.valid.distinct.pluck(:dmptemplate_id) diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 2a4db1d..92b1fde 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -83,12 +83,10 @@ <%= _('Create a template') %> + <%= render partial: '/templates/list_template', locals: { title: n_('Default template', 'Default templates', @organisation_templates.length), templates: @organisation_templates } %> <% if !current_user.org.funder? %> <%= render partial: '/templates/list_template', locals: { title: _('Funders templates'), templates: @funder_templates } %> <% end %> - <% if !current_user.org.organisation? %> - <%= render partial: '/templates/list_template', locals: { title: _('Default templates'), templates: @organisation_templates } %> - <% end %>