diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb
index de50db4..858b66a 100644
--- a/app/controllers/phases_controller.rb
+++ b/app/controllers/phases_controller.rb
@@ -162,6 +162,9 @@
@phase.description = params["phase-desc"]
@phase.modifiable = true
if @phase.save
+ @phase.template.dirty = true
+ @phase.template.save!
+
redirect_to admin_show_phase_path(id: @phase.id, edit: 'true'), notice: _('Information was successfully created.')
else
flash[:notice] = failed_create_error(@phase, _('phase'))
@@ -177,6 +180,9 @@
authorize @phase
@phase.description = params["phase-desc"]
if @phase.update_attributes(params[:phase])
+ @phase.template.dirty = true
+ @phase.template.save!
+
redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.')
else
@sections = @phase.sections
@@ -198,6 +204,9 @@
authorize @phase
@template = @phase.template
if @phase.destroy
+ @template.dirty = true
+ @template.save!
+
redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.')
else
@sections = @phase.sections
diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb
index 47f9a11..6ed74f3 100644
--- a/app/controllers/questions_controller.rb
+++ b/app/controllers/questions_controller.rb
@@ -9,6 +9,9 @@
@question.guidance = params["new-question-guidance"]
@question.default_value = params["new-question-default-value"]
if @question.save
+ @question.section.phase.template.dirty = true
+ @question.section.phase.template.save!
+
redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.')
else
@edit = (@question.section.phase.template.org == current_user.org)
@@ -33,6 +36,9 @@
@section = @question.section
@phase = @section.phase
if @question.update_attributes(params[:question])
+ @question.section.phase.template.dirty = true
+ @question.section.phase.template.save!
+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.')
else
@edit = (@phase.template.org == current_user.org)
@@ -53,6 +59,9 @@
@section = @question.section
@phase = @section.phase
if @question.destroy
+ @phase.template.dirty = true
+ @phase.template.save!
+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.')
else
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: failed_destroy_error(@question, 'question')
diff --git a/app/controllers/sections_controller.rb b/app/controllers/sections_controller.rb
index e05418c..9880863 100644
--- a/app/controllers/sections_controller.rb
+++ b/app/controllers/sections_controller.rb
@@ -10,6 +10,9 @@
@section.modifiable = true
@phase = @section.phase
if @section.save
+ @section.phase.template.dirty = true
+ @section.phase.template.save!
+
redirect_to admin_show_phase_path(id: @section.phase_id,
:section_id => @section.id, edit: 'true'), notice: _('Information was successfully created.')
else
@@ -31,6 +34,9 @@
@section.description = params["section-desc-#{params[:id]}"]
@phase = @section.phase
if @section.update_attributes(params[:section])
+ @section.phase.template.dirty = true
+ @section.phase.template.save!
+
redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id , edit: 'true'), notice: _('Information was successfully updated.')
else
@edit = (@phase.template.org == current_user.org)
@@ -49,7 +55,11 @@
@section = Section.includes(phase: :template).find(params[:section_id])
authorize @section
@phase = @section.phase
+
if @section.destroy
+ @phase.template.dirty = true
+ @phase.template.save!
+
redirect_to admin_show_phase_path(id: @phase.id, edit: 'true' ), notice: _('Information was successfully deleted.')
else
@edit = (@phase.template.org == current_user.org)
diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb
index 1155960..b0eeb11 100644
--- a/app/controllers/templates_controller.rb
+++ b/app/controllers/templates_controller.rb
@@ -14,8 +14,8 @@
# Collect all of the published funder templates
@funder_templates = []
Org.funders.each do |org|
- Template.dmptemplate_ids(org).each do |id|
- template = Template.live(org, id)
+ Template.dmptemplate_ids.each do |id|
+ template = Template.live(id)
# Its possible for the template to NOT have a published version
# so only add it if its not nil
unless template.nil?
@@ -26,9 +26,9 @@
# Collect all of the organisations templates
@org_templates = []
- Template.dmptemplate_ids(current_user.org).each do |id|
- template = Template.current(current_user.org, id)
- live = Template.live(current_user.org, id)
+ Template.dmptemplate_ids.each do |id|
+ template = Template.current(id)
+ live = Template.live(id)
# Its possible for the template to NOT have a published version
# so only add it if its not nil
@@ -67,6 +67,19 @@
customisation.customization_of = @template.dmptemplate_id
customisation.save
+ customisation.phases.includes(:sections, :questions).each do |phase|
+ phase.modifiable = false
+ phase.save!
+ phase.sections.each do |section|
+ section.modifiable = false
+ section.save!
+ section.questions.each do |question|
+ question.modifiable = false
+ question.save!
+ end
+ end
+ end
+
redirect_to admin_template_template_path(customisation)
end
@@ -76,16 +89,30 @@
@template = Template.find(params[:id])
authorize @template
- current = Template.current(current_user.org, @template.dmptemplate_id)
+ current = Template.current(@template.dmptemplate_id)
# Only allow the current version to be updated
if current != @template
redirect_to admin_template_template_path(@template), notice: _('You can not publish a historical version of this template.')
else
+ # Unpublish the older published version if there is one
+ live = Template.live(@template.dmptemplate_id)
+ if !live.nil? and self != live
+ live.published = false
+ live.save!
+ end
+ # Set the dirty flag to false
+ @template.dirty = false
@template.published = true
@template.save
+ # Create a new version
+ new_version = Template.deep_copy(@template)
+ new_version.version = (@template.version + 1)
+ new_version.published = false
+ new_version.save
+
flash[:notice] = _('Your template has been published and is now available to users.')
redirect_to admin_index_template_path(current_user.org)
@@ -99,7 +126,7 @@
authorize template
# Unpublish the live version
- @template = Template.live(current_user.org, template.dmptemplate_id)
+ @template = Template.live(template.dmptemplate_id)
if @template.nil?
flash[:notice] = _('That template is not currently published.')
@@ -119,7 +146,7 @@
:suggested_answers]]]).find(params[:id])
authorize @template
- @current = Template.current(current_user.org, @template.dmptemplate_id)
+ @current = Template.current(@template.dmptemplate_id)
unless @template == @current
flash[:notice] = _('You are viewing a historical version of this template. You will not be able to make changes.')
@@ -136,7 +163,7 @@
@template = Template.find(params[:id])
authorize @template
- current = Template.current(current_user.org, @template.dmptemplate_id)
+ current = Template.current(@template.dmptemplate_id)
# Only allow the current version to be updated
if current != @template
@@ -194,7 +221,7 @@
@template = Template.find(params[:id])
authorize @template
- current = Template.current(current_user.org, @template.dmptemplate_id)
+ current = Template.current(@template.dmptemplate_id)
# Only allow the current version to be destroyed
if current == @template
@@ -217,7 +244,7 @@
@template = Template.find(params[:id])
authorize @template
@templates = Template.where(dmptemplate_id: @template.dmptemplate_id).order(:version)
- @current = Template.current(current_user.org, @template.dmptemplate_id)
+ @current = Template.current(@template.dmptemplate_id)
end
end
\ No newline at end of file
diff --git a/app/models/phase.rb b/app/models/phase.rb
index 3f0df0c..1417661 100644
--- a/app/models/phase.rb
+++ b/app/models/phase.rb
@@ -5,11 +5,6 @@
# [+Copyright:+] Digital Curation Centre and University of California Curation Center
class Phase < ActiveRecord::Base
# extend FriendlyId
-
- after_save :make_template_dirty
- after_create :make_template_dirty
- before_destroy :make_template_dirty
-
##
# Associations
belongs_to :template
@@ -105,12 +100,4 @@
return phase_copy
end
- # --------------------------------------------------
- private
- # Mark the parent template as dirty
- def make_template_dirty
- self.template.dirty = true
- self.template.save!
- end
-
end
diff --git a/app/models/question.rb b/app/models/question.rb
index 822719a..336cae9 100644
--- a/app/models/question.rb
+++ b/app/models/question.rb
@@ -1,8 +1,4 @@
class Question < ActiveRecord::Base
- after_save :make_template_dirty
- after_create :make_template_dirty
- before_destroy :make_template_dirty
-
##
# Associations
has_many :answers, :dependent => :destroy
@@ -114,12 +110,4 @@
return suggested_answer
end
- # --------------------------------------------------------
- private
- # Mark the parent template as dirty
- def make_template_dirty
- self.section.phase.template.dirty = true
- self.section.phase.template.save!
- end
-
end
diff --git a/app/models/section.rb b/app/models/section.rb
index de0a64d..abf1327 100644
--- a/app/models/section.rb
+++ b/app/models/section.rb
@@ -1,8 +1,4 @@
class Section < ActiveRecord::Base
- after_save :make_template_dirty
- after_create :make_template_dirty
- before_destroy :make_template_dirty
-
##
# Associations
belongs_to :phase
@@ -50,13 +46,4 @@
end
return section_copy
end
-
- # ---------------------------------------------------
- private
- # Mark the parent template as dirty
- def make_template_dirty
- self.phase.template.dirty = true
- self.phase.template.save!
- end
-
end
diff --git a/app/models/template.rb b/app/models/template.rb
index 10bd4b1..63b4b3e 100644
--- a/app/models/template.rb
+++ b/app/models/template.rb
@@ -1,12 +1,8 @@
class Template < ActiveRecord::Base
include GlobalHelpers
-
- before_create :set_creation_defaults
- after_create :set_modifiable_statuses
- before_save :pre_publishing
- after_save :post_publishing
-
+ before_validation :set_creation_defaults
+
##
# Associations
belongs_to :org
@@ -33,18 +29,18 @@
validates :org, :title, :version, presence: {message: _("can't be blank")}
# Retrieves the list of all dmptemplate_ids (template versioning families) for the specified Org
- def self.dmptemplate_ids(org)
- Template.where(org_id: org.id).distinct.pluck(:dmptemplate_id)
+ def self.dmptemplate_ids
+ Template.all.distinct.pluck(:dmptemplate_id)
end
# Retrieves the most recent version of the template for the specified Org and dmptemplate_id
- def self.current(org, dmptemplate_id)
- Template.where(dmptemplate_id: dmptemplate_id, org_id: org.id).order(version: :desc).first
+ def self.current(dmptemplate_id)
+ Template.where(dmptemplate_id: dmptemplate_id).order(version: :desc).first
end
# Retrieves the current published version of the template for the specified Org and dmptemplate_id
- def self.live(org, dmptemplate_id)
- Template.where(dmptemplate_id: dmptemplate_id, org_id: org.id, published: true).first
+ def self.live(dmptemplate_id)
+ Template.where(dmptemplate_id: dmptemplate_id, published: true).first
end
##
@@ -129,57 +125,18 @@
private
# Initialize the published and dirty flags for new templates
def set_creation_defaults
- self.published = false
- self.dirty = false
-
- # Generate a unique identifier for the dmptemplate_id if necessary
- if self.dmptemplate_id.nil?
- self.dmptemplate_id = loop do
- random = rand 2147483647
- break random unless Template.exists?(dmptemplate_id: random)
- end
- end
- end
-
- # Unpublish older versions when publishing the template
- def pre_publishing
- if self.published?
- # Unpublish the older published version if there is one
- live = Template.live(self.org, self.dmptemplate_id)
- if !live.nil? and self != live
- live.published = false
- live.save!
- end
- # Set the dirty flag to false
+ # Only run this before_validation because rails fires this before save/create
+ if self.id.nil?
+ self.published = false
self.dirty = false
- end
- end
-
- # Once the version has been published, create a new one which should
- # be returned by the Template.current method
- def post_publishing
- # Create a new version
- new_version = Template.deep_copy(self)
- new_version.version = (self.version + 1)
- new_version.save
- end
-
- # Update the modifiable flags on phases->sections->questions
- def set_modifiable_statuses
- # If we're working with a customization and its version 0
- # we should mark all of the phases->sections->questions
- # as unmodifiable
- if !self.customization_of.nil? && version == 0
- self.phases.includes(:sections, :questions).each do |phase|
- phase.modifiable = false
- phase.save!
- phase.sections.each do |section|
- section.modifiable = false
- section.save!
- section.questions.each do |question|
- question.modifiable = false
- question.save!
- end
+ self.visibility = 1
+ self.version = 0 if self.version.nil?
+
+ # Generate a unique identifier for the dmptemplate_id if necessary
+ if self.dmptemplate_id.nil?
+ self.dmptemplate_id = loop do
+ random = rand 2147483647
+ break random unless Template.exists?(dmptemplate_id: random)
end
end
end
diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb
index f1e9df2..4c403ad 100644
--- a/app/views/templates/admin_index.html.erb
+++ b/app/views/templates/admin_index.html.erb
@@ -41,7 +41,7 @@
<%= hash[:current].title %>
- <%= raw hash[:current].description.truncate(90, omission: _('... (continued)')) %>
+ <%= raw hash[:current].description.truncate(90, omission: _('... (continued)')) unless hash[:current].description.nil? %>
|
<% if hash[:live].nil? %>
diff --git a/test/functional/templates_controller_test.rb b/test/functional/templates_controller_test.rb
index 231a00b..291a67b 100644
--- a/test/functional/templates_controller_test.rb
+++ b/test/functional/templates_controller_test.rb
@@ -5,6 +5,7 @@
include Devise::Test::IntegrationHelpers
setup do
+puts "SETUP"
scaffold_template
# Get the first Org Admin
@@ -113,11 +114,11 @@
sign_in @user
family = @template.dmptemplate_id
- prior = Template.current(@template.org, family)
+ prior = Template.current(family)
version_the_template
- current = Template.current(@template.org, family)
+ current = Template.current(family)
# Try to delete a historical version should fail
delete admin_destroy_template_path(prior)
@@ -133,7 +134,7 @@
assert_raise ActiveRecord::RecordNotFound do
Template.find(current.id).nil?
end
- assert_equal prior, Template.current(@template.org, family), "expected the old version to now be the current version"
+ assert_equal prior, Template.current(family), "expected the old version to now be the current version"
end
# TODO: Why are we passing an :id here!? Its a new record but we seem to need the last template's id
@@ -143,12 +144,12 @@
params = {title: 'Testing create route'}
# Should redirect user to the root path if they are not logged in!
- post admin_create_template_path(Template.last.id), {template: params}
+ post admin_create_template_path(@user.org), {template: params}
assert_unauthorized_redirect_to_root_path
sign_in @user
- post admin_create_template_path(Template.last.id), {template: params}
+ post admin_create_template_path(@user.org), {template: params}
assert_equal _('Information was successfully created.'), flash[:notice]
assert_response :redirect
assert_redirected_to admin_template_template_url(Template.last.id)
@@ -156,7 +157,7 @@
assert_equal 'Testing create route', Template.last.title, "expected the record to have been created!"
# Invalid object
- post admin_create_template_path(Template.last.id), {template: {title: nil, org_id: @user.org.id}}
+ post admin_create_template_path(@user.org), {template: {title: nil, org_id: @user.org.id}}
assert flash[:notice].starts_with?(_('Could not create your'))
assert_response :success
assert assigns(:template)
@@ -175,11 +176,11 @@
sign_in @user
family = @template.dmptemplate_id
- prior = Template.current(@template.org, family)
+ prior = Template.current(family)
version_the_template
- current = Template.current(@template.org, family)
+ current = Template.current(family)
# We shouldn't be able to edit a historical version
put admin_update_template_path(prior), {template: params}
@@ -209,9 +210,8 @@
# ----------------------------------------------------------
test "customize a funder template" do
funder = Org.funders.first
- ids = Template.dmptemplate_ids(funder)
-
- template = Template.live(funder, ids.last)
+ id = Template.find_by(org: funder, published: true).dmptemplate_id
+ template = Template.live(id)
# Make sure we are redirected if we're not logged in
put admin_customize_template_path(template)
@@ -221,7 +221,7 @@
put admin_customize_template_path(template)
- customization = Template.where(org: @user.org, customization_of: template.dmptemplate_id).last
+ customization = Template.where(customization_of: template.dmptemplate_id).last
assert_response :redirect
assert_redirected_to admin_template_template_url(Template.last)
@@ -253,11 +253,11 @@
sign_in @user
family = @template.dmptemplate_id
- prior = Template.current(@user.org, family)
+ prior = Template.current(family)
version_the_template
- current = Template.current(@user.org, family)
+ current = Template.current(family)
# We shouldn't be able to edit a historical version
put admin_publish_template_path(prior)
@@ -274,7 +274,7 @@
# Make sure it versioned properly
current = Template.includes(:phases, :sections, :questions).find(current.id)
- new_version = Template.current(@user.org, family)
+ new_version = Template.current(family)
assert_not_equal current.id = new_version.id, "expected it to create a new version"
assert_equal (current.version + 1), new_version.version, "expected the version to have incremented"
assert current.published?, "expected the old version to be published"
@@ -282,28 +282,6 @@
assert_not current.dirty?, "expected the old dirty flag to be false"
assert_not new_version.dirty?, "expected the new dirty flag to be false"
assert_equal current.dmptemplate_id, new_version.dmptemplate_id, "expected the old and new versions to share the same dmptemplate_id"
-
- # The old version's phases/sections/questions should NOT be modifiable
- current.phases.each do |p|
- assert_not p.modifiable?
- p.sections.each do |s|
- assert_not s.modifiable?
- s.questions.each do |q|
- assert_not q.modifiable?
- end
- end
- end
-
- # The new version's phases/sections/questions should be modifiable
- new_version.phases.each do |p|
- assert p.modifiable
- p.sections.each do |s|
- assert s.modifiable
- s.questions.each do |q|
- assert q.modifiable
- end
- end
- end
end
# GET /org/admin/templates/:id/admin_unpublish (admin_unpublish_template_path)
@@ -316,21 +294,15 @@
sign_in @user
family = @template.dmptemplate_id
- prior = Template.current(@user.org, family)
+ prior = Template.current(family)
version_the_template
- current = Template.current(@user.org, family)
-
- # Try to unpublish a template that is not published
- put admin_unpublish_template_path(current)
- assert_equal _('That template is not currently published.'), flash[:notice]
- assert_response :redirect
- assert_redirected_to admin_index_template_path(@user.org)
+ current = Template.current(family)
# Publish it so we can unpublish
put admin_publish_template_path(current)
- assert_not Template.live(@user.org, family).nil?
+ assert_not Template.live(family).nil?
put admin_unpublish_template_path(current)
assert_equal _('Your template is no longer published. Users will not be able to create new DMPs for this template until you re-publish it'), flash[:notice]
@@ -338,6 +310,6 @@
assert_redirected_to admin_index_template_path(@user.org)
# Make sure there are no published versions
- assert Template.live(@user.org, family).nil?
+ assert Template.live(family).nil?
end
end
\ No newline at end of file
diff --git a/test/integration/template_versioning_test.rb b/test/integration/template_versioning_test.rb
index 9d98d85..c14d408 100644
--- a/test/integration/template_versioning_test.rb
+++ b/test/integration/template_versioning_test.rb
@@ -23,7 +23,7 @@
test 'template gets versioned when its details are updated but it is already published' do
# Publish the template
put admin_publish_template_path(@template)
- @template = Template.current(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented"
assert_not_equal @initial_id, @template.id, "expected the id to have changed"
@@ -33,7 +33,7 @@
# Change the title after its been published
put admin_update_template_path(@template), {template: {title: "Blah blah blah"}}
- @template = Template.current(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
# Make sure that the template was versioned
assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented"
@@ -43,7 +43,7 @@
assert_not_equal @initial_title, @template.title, "expected the title to have been updated"
# Now retrieve the published version and verify that it is unchanged
- old = Template.live(@user.org, @dmptemplate_id)
+ old = Template.live(@dmptemplate_id)
assert_equal @initial_version, old.version, "expected the version number of the published version to be the same"
assert_equal @initial_id, old.id, "expected the id of the published version to be the same"
assert_equal @initial_title, old.title, "expected the title of the published version to be the same"
@@ -83,7 +83,7 @@
test 'template does NOT get versioned if its unpublished' do
# Change the title after its been published
put admin_update_template_path(@template), {template: {title: "Blah blah blah"}}
- @template = Template.current(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
assert_equal @initial_version, @template.version, "expected the version to have stayed the same"
assert_equal @initial_id, @template.id, "expected the id to been the same"
@@ -94,7 +94,7 @@
# ----------------------------------------------------------
test 'publishing a plan unpublishes the old published plan' do
put admin_publish_template_path(@template)
- assert_not Template.live(@user.org, @dmptemplate_id).nil?
+ assert_not Template.live(@dmptemplate_id).nil?
assert_equal 1, Template.where(org: @user.org, dmptemplate_id: @dmptemplate_id, published: true).count
end
@@ -102,15 +102,15 @@
test 'unpublishing a plan makes all historical versions unpublished' do
put admin_publish_template_path(@template)
put admin_unpublish_template_path(@template)
- assert Template.live(@user.org, @dmptemplate_id).nil?
+ assert Template.live(@dmptemplate_id).nil?
end
# ----------------------------------------------------------
test 'plans get attached to the appropriate template version' do
# Template is published
put admin_publish_template_path(@template)
- @template = Template.current(@user.org, @dmptemplate_id)
- liveA = Template.live(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
+ liveA = Template.live(@dmptemplate_id)
# Plan A gets attached to the template v1
post plans_path, {plan: {funder_id: @user.org_id}}
@@ -121,7 +121,7 @@
# Template v1 is updated and gets versioned to v2
put admin_update_template_path(@template), {template: {title: "Blah blah blah"}}
- @template = Template.current(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
# Plan B gets attached to the template v1 because v2 is not yet published
post plans_path, {plan: {funder_id: @user.org_id}}
@@ -135,8 +135,8 @@
# Template v2 is published
put admin_publish_template_path(@template)
- @template = Template.current(@user.org, @dmptemplate_id)
- liveB = Template.live(@user.org, @dmptemplate_id)
+ @template = Template.current(@dmptemplate_id)
+ liveB = Template.live(@dmptemplate_id)
# Plan C gets attached to template v2
post plans_path, {plan: {funder_id: @user.org_id}}
diff --git a/test/unit/org_test.rb b/test/unit/org_test.rb
index 60b0f6f..c2c497d 100644
--- a/test/unit/org_test.rb
+++ b/test/unit/org_test.rb
@@ -115,7 +115,11 @@
# ---------------------------------------------------
test "published_templates should return all published templates" do
3.times do |i|
- @org.templates << Template.new(version: 1, title: "Testing #{i}", published: (i < 2 ? true : false))
+ template = Template.create(org: @org, version: 1, title: "Testing #{i}")
+ if i < 2
+ template.published = true
+ template.save!
+ end
end
assert_not @org.published_templates.select{|t| t.title == "Testing 0"}.empty?, "expected the 1st template to be included"
diff --git a/test/unit/template_test.rb b/test/unit/template_test.rb
index 7fa53b1..cb05949 100644
--- a/test/unit/template_test.rb
+++ b/test/unit/template_test.rb
@@ -22,20 +22,19 @@
# ---------------------------------------------------
test "required fields are required" do
assert_not Template.new.valid?
- assert_not Template.new(org: @org, title: 'Tester').valid?, "expected the 'version' field to be required"
assert_not Template.new(version: 1, title: 'Tester').valid?, "expected the 'org' field to be required"
assert_not Template.new(org: @org, version: 1).valid?, "expected the 'title' field to be required"
# Ensure the bare minimum and complete versions are valid
- a = Template.new(org: @org, version: 1, title: 'Tester')
+ a = Template.new(org: @org, title: 'Tester')
assert a.valid?, "expected the 'org', 'version' and 'title' fields to be enough to create an Template! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}"
end
# ---------------------------------------------------
test "family_ids scope only returns the dmptemplate_ids for the specific Org" do
Org.all.each do |org|
- family_ids = Template.where(org_id: org.id).pluck(:dmptemplate_id).uniq
- scoped = Template.dmptemplate_ids(org)
+ family_ids = Template.all.pluck(:dmptemplate_id).uniq
+ scoped = Template.dmptemplate_ids
assert_equal family_ids.count, scoped.count
family_ids.each do |id|
@@ -50,10 +49,10 @@
# ---------------------------------------------------
test "current scope only returns the most recent version for each dmptemplate_id" do
Org.all.each do |org|
- Template.dmptemplate_ids(org).each do |dmptemplate_id|
- latest = Template.where(org_id: org.id, dmptemplate_id: dmptemplate_id).order(updated_at: :desc).first
+ Template.dmptemplate_ids.each do |dmptemplate_id|
+ latest = Template.where(dmptemplate_id: dmptemplate_id).order(updated_at: :desc).first
- assert_equal latest, Template.current(org, dmptemplate_id), "Expected the template.id #{latest.id} to be the current record for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}"
+ assert_equal latest, Template.current(dmptemplate_id), "Expected the template.id #{latest.id} to be the current record for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}"
end
end
end
@@ -61,10 +60,10 @@
# ---------------------------------------------------
test "published scope only returns the current published version for each dmptemplate_id" do
Org.all.each do |org|
- Template.dmptemplate_ids(org).each do |dmptemplate_id|
- latest = Template.where(org_id: org.id, dmptemplate_id: dmptemplate_id, published: true).order(updated_at: :desc).first
+ Template.dmptemplate_ids.each do |dmptemplate_id|
+ latest = Template.where(dmptemplate_id: dmptemplate_id, published: true).order(updated_at: :desc).first
- assert_equal latest, Template.live(org, dmptemplate_id), "Expected the #{latest.nil? ? 'template to have never been published' : "template.id #{latest.id} to be the published record"} for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}"
+ assert_equal latest, Template.live(dmptemplate_id), "Expected the #{latest.nil? ? 'template to have never been published' : "template.id #{latest.id} to be the published record"} for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}"
end
end
end
|