diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index 4ac06b9..de50db4 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -162,10 +162,6 @@ @phase.description = params["phase-desc"] @phase.modifiable = true if @phase.save - # Set the template's dirty flag to true - @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')) @@ -181,10 +177,6 @@ authorize @phase @phase.description = params["phase-desc"] if @phase.update_attributes(params[:phase]) - # Set the template's dirty flag to true - @phase.template.dirty = true - @phase.template.save - redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.') else @sections = @phase.sections @@ -206,10 +198,6 @@ authorize @phase @template = @phase.template if @phase.destroy - # Set the template's dirty flag to true - @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 3b55aa0..47f9a11 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -9,10 +9,6 @@ @question.guidance = params["new-question-guidance"] @question.default_value = params["new-question-default-value"] if @question.save - # Set the template's dirty flag to true - @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) @@ -37,10 +33,6 @@ @section = @question.section @phase = @section.phase if @question.update_attributes(params[:question]) - # Set the template's dirty flag to true - @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) @@ -61,10 +53,6 @@ @section = @question.section @phase = @section.phase if @question.destroy - # Set the template's dirty flag to true - @question.section.phase.template.dirty = true - @question.section.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 65d5f9f..e05418c 100644 --- a/app/controllers/sections_controller.rb +++ b/app/controllers/sections_controller.rb @@ -10,10 +10,6 @@ @section.modifiable = true @phase = @section.phase if @section.save - # Set the template's dirty flag to true - @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 @@ -35,10 +31,6 @@ @section.description = params["section-desc-#{params[:id]}"] @phase = @section.phase if @section.update_attributes(params[:section]) - # Set the template's dirty flag to true - @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) @@ -58,10 +50,6 @@ authorize @section @phase = @section.phase if @section.destroy - # Set the template's dirty flag to true - @section.phase.template.dirty = true - @section.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 906eb70..1155960 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -64,32 +64,9 @@ customisation = Template.deep_copy(@template) customisation.org = current_user.org customisation.version = 0 - customisation.published = false - customisation.dirty = false customisation.customization_of = @template.dmptemplate_id - - customisation.dmptemplate_id = loop do - random = rand 2147483647 # max int field in psql - break random unless Template.exists?(dmptemplate_id: random) - end - customisation.save - # need to mark all of the original funder's phases, questions, - # sections as not-modifiable - 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 @@ -106,54 +83,11 @@ 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(current_user.org, @template.dmptemplate_id) - unless live.nil? - live.published = false - live.save - end - - # Publish the template @template.published = true - @template.dirty = false @template.save flash[:notice] = _('Your template has been published and is now available to users.') - # Create a new version - new_version = Template.deep_copy(@template) - new_version.version = (@template.version + 1) - new_version.published = false - new_version.save - - # Make all of the templates phase/section/questions as unmodifiable - @template.phases.each do |p| - p.sections.each do |s| - s.questions.each do |q| - q.modifiable = false - q.save - end - s.modifiable = false - s.save - end - p.modifiable = false - p.save - end - - # Make all of the new version's templates phase/section/questions modifiable - new_version.phases.each do |p| - p.sections.each do |s| - s.questions.each do |q| - q.modifiable = true - q.save - end - s.modifiable = true - s.save - end - p.modifiable = true - p.save - end - redirect_to admin_index_template_path(current_user.org) end end @@ -243,15 +177,6 @@ authorize @template @template.org_id = current_user.org.id @template.description = params['template-desc'] - @template.published = false - @template.version = 0 - @template.visibility = 0 - - # Generate a unique identifier for the dmptemplate_id - @template.dmptemplate_id = loop do - random = rand 2147483647 - break random unless Template.exists?(dmptemplate_id: random) - end if @template.save redirect_to admin_template_template_path(@template), notice: _('Information was successfully created.') @@ -271,7 +196,7 @@ current = Template.current(current_user.org, @template.dmptemplate_id) - # Only allow the current version to be updated + # Only allow the current version to be destroyed if current == @template if @template.destroy redirect_to admin_index_template_path diff --git a/app/models/phase.rb b/app/models/phase.rb index d3e813a..3f0df0c 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -6,6 +6,10 @@ 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 @@ -25,11 +29,7 @@ validates :title, :number, :template, presence: {message: _("can't be blank")} - - - - - + # EVALUATE CLASS AND INSTANCE METHODS BELOW # # What do they do? do they do it efficiently, and do we need them? @@ -104,4 +104,13 @@ end 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 eac588d..822719a 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,5 +1,8 @@ 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 @@ -29,7 +32,6 @@ validates :text, :section, :number, presence: {message: _("can't be blank")} - # EVALUATE CLASS AND INSTANCE METHODS BELOW # # What do they do? do they do it efficiently, and do we need them? @@ -112,4 +114,12 @@ 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 2a38972..de0a64d 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -1,4 +1,7 @@ class Section < ActiveRecord::Base + after_save :make_template_dirty + after_create :make_template_dirty + before_destroy :make_template_dirty ## # Associations @@ -48,4 +51,12 @@ 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 e0cc33a..10bd4b1 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -1,6 +1,12 @@ class Template < ActiveRecord::Base include GlobalHelpers + before_create :set_creation_defaults + after_create :set_modifiable_statuses + + before_save :pre_publishing + after_save :post_publishing + ## # Associations belongs_to :org @@ -119,4 +125,64 @@ return !modifiable end + # -------------------------------------------------------- + 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 + 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 + end + end + end + end + end