diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index e713bfe..14e9f41 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -459,21 +459,21 @@ # Load the org's template(s) unless org_id.nil? org = Org.find(org_id) - @templates = Template.where(published: true, org: org, customization_of: nil).to_a + @templates = Template.valid.where(published: true, org: org, customization_of: nil).to_a @msg = _("We found multiple DMP templates corresponding to the research organisation.") if @templates.count > 1 end else funder = Org.find(funder_id) # Load the funder's template(s) - @templates = Template.where(published: true, org: funder).to_a + @templates = Template.valid.where(published: true, org: funder).to_a unless org_id.blank? org = Org.find(org_id) # Swap out any organisational cusotmizations of a funder template @templates.each do |tmplt| - customization = Template.find_by(published: true, org: org, customization_of: tmplt.dmptemplate_id) + customization = Template.valid.find_by(published: true, org: org, customization_of: tmplt.dmptemplate_id) unless customization.nil? @templates.delete(tmplt) @templates << customization diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 6d7c775..76d55a5 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -14,8 +14,8 @@ funder_templates, org_templates, customizations = [], [], [] # 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.collect{|ft| ft.dmptemplate_id } }.flatten.uniq - org_ids = current_user.org.templates.collect{|t| t.dmptemplate_id }.flatten.uniq + funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq + org_ids = current_user.org.templates.valid.collect{|t| t.dmptemplate_id }.flatten.uniq org_ids.each do |id| current = Template.current(id) diff --git a/app/models/template.rb b/app/models/template.rb index f7d5639..903dbd2 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -1,8 +1,8 @@ class Template < ActiveRecord::Base include GlobalHelpers - + before_validation :set_creation_defaults - + scope :valid, -> {where(migrated: false)} ## # Associations belongs_to :org @@ -30,17 +30,17 @@ # Retrieves the list of all dmptemplate_ids (template versioning families) for the specified Org def self.dmptemplate_ids - Template.all.distinct.pluck(:dmptemplate_id) + Template.all.valid.distinct.pluck(:dmptemplate_id) end # Retrieves the most recent version of the template for the specified Org and dmptemplate_id def self.current(dmptemplate_id) - Template.where(dmptemplate_id: dmptemplate_id).order(version: :desc).first + Template.where(dmptemplate_id: dmptemplate_id).order(version: :desc).valid.first end # Retrieves the current published version of the template for the specified Org and dmptemplate_id def self.live(dmptemplate_id) - Template.where(dmptemplate_id: dmptemplate_id, published: true).first + Template.where(dmptemplate_id: dmptemplate_id, published: true).valid.first end ## diff --git a/db/schema.rb b/db/schema.rb index f0bfbd2..fd97cce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -176,8 +176,9 @@ t.boolean "modifiable" end + add_index "phases", ["template_id"], name: "index_phases_on_template_id", using: :btree + create_table "plans", force: :cascade do |t| - t.integer "project_id" t.string "title" t.integer "template_id" t.datetime "created_at" @@ -193,6 +194,8 @@ t.integer "visibility", default: 0, null: false end + add_index "plans", ["template_id"], name: "index_plans_on_template_id", using: :btree + create_table "plans_guidance_groups", force: :cascade do |t| t.integer "guidance_group_id" t.integer "plan_id" @@ -219,7 +222,6 @@ create_table "questions", force: :cascade do |t| t.text "text" t.text "default_value" - t.text "guidance" t.integer "number" t.integer "section_id" t.datetime "created_at" @@ -229,6 +231,8 @@ t.boolean "modifiable" end + add_index "questions", ["section_id"], name: "index_questions_on_section_id", using: :btree + create_table "questions_themes", id: false, force: :cascade do |t| t.integer "question_id", null: false t.integer "theme_id", null: false @@ -263,6 +267,8 @@ t.boolean "modifiable" end + add_index "sections", ["phase_id"], name: "index_sections_on_phase_id", using: :btree + create_table "settings", force: :cascade do |t| t.string "var", null: false t.text "value" @@ -293,9 +299,13 @@ t.integer "visibility" t.integer "customization_of" t.integer "dmptemplate_id" + t.boolean "migrated" t.boolean "dirty", default: false end + add_index "templates", ["org_id", "dmptemplate_id"], name: "template_organisation_dmptemplate_index", using: :btree + add_index "templates", ["org_id"], name: "index_templates_on_org_id", using: :btree + create_table "themes", force: :cascade do |t| t.string "title" t.text "description" @@ -349,7 +359,6 @@ t.datetime "invitation_sent_at" t.datetime "invitation_accepted_at" t.string "other_organisation" - t.boolean "dmponline3" t.boolean "accept_terms" t.integer "org_id" t.string "api_token"