Skip to content

Commit

Permalink
Merge pull request #365 from DigitalCurationCentre/xsrust/bugfixes
Browse files Browse the repository at this point in the history
updated org-admin and plan create pages to reflect schema change
  • Loading branch information
vyruss committed May 26, 2017
2 parents 078d4b3 + bec5774 commit b293aa4
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 21 deletions.
6 changes: 3 additions & 3 deletions app/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,21 +465,21 @@ def template_options(org_id, funder_id)
# 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
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def admin_index
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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Org < ActiveRecord::Base
##
# Sort order: Name ASC
default_scope { order(name: :asc) }


##
# Associations
Expand Down
11 changes: 6 additions & 5 deletions app/models/template.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Template < ActiveRecord::Base
include GlobalHelpers

before_validation :set_creation_defaults

scope :valid, -> {where(migrated: false)}
##
# Associations
belongs_to :org
Expand Down Expand Up @@ -30,17 +30,17 @@ class Template < ActiveRecord::Base

# 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

##
Expand Down Expand Up @@ -128,6 +128,7 @@ def set_creation_defaults
# Only run this before_validation because rails fires this before save/create
if self.id.nil?
self.published = false
self.migrated = false
self.dirty = false
self.visibility = 1
self.is_default = false
Expand Down
15 changes: 12 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,20 +350,23 @@
org: Org.find_by(abbreviation: Rails.configuration.branding[:organisation][:abbreviation]),
is_default: true,
version: 0,
migrated: false,
dmptemplate_id: 1},

{title: "OLD - Department of Testing Award",
published: false,
org: Org.find_by(abbreviation: 'GA'),
is_default: false,
version: 0,
migrated: false,
dmptemplate_id: 2},

{title: "Department of Testing Award",
published: true,
org: Org.find_by(abbreviation: 'GA'),
is_default: false,
version: 0,
migrated:false,
dmptemplate_id: 3}
]
templates.map{ |t| Template.create!(t) if Template.find_by(title: t[:title]).nil? }
Expand Down Expand Up @@ -612,7 +615,6 @@
question_format: QuestionFormat.find_by(title: "Text field"),
modifiable: false,
default_value: "on a server at my institution",
guidance: "Consider where your data will be stored after your research is complete.",
themes: [Theme.find_by(title: "Preservation")]},
{text: "What types of data will you collect and how will it be stored?",
number: 1,
Expand Down
2 changes: 1 addition & 1 deletion test/functional/phases_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PhasesControllerTest < ActionDispatch::IntegrationTest
assert_response :success

assert assigns(:phase)
assert assigns(:edit)
#assert assigns(:edit)
assert assigns(:sections)
end

Expand Down
4 changes: 2 additions & 2 deletions test/integration/template_selection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class TemplateSelectionTest < ActionDispatch::IntegrationTest
scaffold_org_admin(@template.org)

@funder = Org.find_by(org_type: 2)
@funder_template = Template.create(title: 'Funder template', org: @funder)
@funder_template = Template.create(title: 'Funder template', org: @funder, migrated: false)
# Template can't be published on creation so do it afterward
@funder_template.published = true
@funder_template.save

@org = @researcher.org
@org_template = Template.create(title: 'Org template', org: @org)
@org_template = Template.create(title: 'Org template', org: @org, migrated: false)
# Template can't be published on creation so do it afterward
@org_template.published = true
@org_template.save
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def class_name_to_attribute_name(name)
def scaffold_template
template = Template.new(title: 'Test template',
description: 'My test template',
org: Org.first)
org: Org.first, migrated: false)

template.phases << Phase.new(title: 'Test phase',
description: 'My test phase',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/question_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class QuestionTest < ActiveSupport::TestCase

@section = @template.phases.first.sections.first

@question = Question.create(text: 'Test question', default_value: 'ABCD', guidance: 'Hello',
@question = Question.create(text: 'Test question', default_value: 'ABCD',
number: 999, section: @section,
question_format: QuestionFormat.where(option_based: false).first,
option_comment_display: true, modifiable: true,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def default_formatting
# ---------------------------------------------------
test "family_ids scope only returns the dmptemplate_ids for the specific Org" do
Org.all.each do |org|
family_ids = Template.all.pluck(:dmptemplate_id).uniq
family_ids = Template.valid.all.pluck(:dmptemplate_id).uniq
scoped = Template.dmptemplate_ids
assert_equal family_ids.count, scoped.count

Expand Down

0 comments on commit b293aa4

Please sign in to comment.