diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 37c6800..7de0789 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -9,9 +9,10 @@ # GET /dmptemplates def admin_index authorize Template - #institutional templates + # institutional templates all_versions_own_templates = Template.where(org_id: current_user.org_id, customization_of: nil).order(version: :desc) current_templates = {} + # take most recent version of each template all_versions_own_templates.each do |temp| if current_templates[temp.dmptemplate_id].nil? current_templates[temp.dmptemplate_id] = temp @@ -19,7 +20,7 @@ end @templates_own = current_templates.values #funders templates - @templates_funders = Org.funders.collect{|o| o.templates } #Template.funders_templates + @templates_funders = []#Org.funders.collect{|o| o.templates } #Template.funders_templates end @@ -27,6 +28,9 @@ def admin_template @template = Template.find(params[:id]) authorize @template + if @template.published + # create a new template version + end end @@ -36,7 +40,7 @@ authorize @template @template.description = params["template-desc"] if @template.update_attributes(params[:template]) - redirect_to admin_template_template_path(params[:template]), notice: I18n.t('org_admin.templates.updated_message') + redirect_to admin_index_template_path(), notice: I18n.t('org_admin.templates.updated_message') else render action: "edit" end diff --git a/app/models/phase.rb b/app/models/phase.rb index 49029f8..eae90f1 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -86,4 +86,20 @@ end return has_section end + + ## + # deep copy the given phase and all it's associations + # + # @params [Phase] phase to be deep copied + # @return [Phase] the saved, copied phase + def self.deep_copy(phase) + phase_copy = phase.dup + phase_copy.save! + phase.sections.each do |section| + section_copy = Section.deep_copy(section) + section_copy.phase_id = phase_copy.id + section_copy.save! + end + return phase_copy + end end diff --git a/app/models/question.rb b/app/models/question.rb index 5493b79..72af4e0 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -44,6 +44,30 @@ "#{text}" end + ## + # deep copy the given question and all it's associations + # + # @params [Question] question to be deep copied + # @return [Question] the saved, copied question + def self.deep_copy(question) + question_copy = question.dup + question_copy.save! + question.question_options.each do |question_option| + question_option_copy = QuestionOption.deep_copy(question_option) + question_option_copy.quesion_id = question_copy.id + question_option_copy.save! + end + question.suggested_answers.each do |suggested_answer| + suggested_answer_copy = SuggestedAnswer.deep_copy(suggested_answer) + suggested_answer_copy.quesion_id = question_copy.id + suggested_answer_copy.save! + end + question.theme.each do |theme| + question_copy.themes << theme + end + return question_copy + end + # TODO: Commented this amoeba cloning gem definition out to see if its even used. The # amoeba documentations uses [object].amoeba_dup to clone the object, but that # command does not exist in the codebase diff --git a/app/models/question_option.rb b/app/models/question_option.rb index 1a079d0..11552b0 100644 --- a/app/models/question_option.rb +++ b/app/models/question_option.rb @@ -9,6 +9,17 @@ # -relies on protected_attributes gem as syntax depricated in rails 4.2 attr_accessible :text, :question_id, :is_default, :number, :question, :as => [:default, :admin] - + validates :text, :question, :number, presence: true + + ## + # deep copy the given question_option and all it's associations + # + # @params [QuestionOption] question_option to be deep copied + # @return [QuestionOption] the saved, copied question_option + def self.deep_copy(question_option) + question_option_copy = question_option.dup + question_option_copy.save! + return question_option_copy + end end diff --git a/app/models/section.rb b/app/models/section.rb index 4b484ab..8e04fbb 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -24,6 +24,22 @@ "#{title}" end + ## + # deep copy of the given section and all it's associations + # + # @params [Section] section to be deep copied + # @return [Section] the saved, copied section + def self.deep_copy(section) + section_copy = section.dup + section_copy.save! + section.questions.each do |question| + question_copy = Question.deep_copy(question) + question_copy.section_id = section_copy.id + question_copy.save! + end + return section_copy + end + # TODO: Commented this amoeba cloning gem definition out to see if its even used. The # amoeba documentations uses [object].amoeba_dup to clone the object, but that # command does not exist in the codebase diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb index f3aaaf8..8f427c6 100644 --- a/app/models/suggested_answer.rb +++ b/app/models/suggested_answer.rb @@ -28,4 +28,15 @@ "#{text}" end + + ## + # deep copy the given question_option and all it's associations + # + # @params [QuestionOption] question_option to be deep copied + # @return [QuestionOption] the saved, copied question_option + def self.deep_copy(suggested_answer) + suggested_answer_copy = suggested_answer.dup + suggested_answer_copy.save! + return suggested_answer_copy + end end \ No newline at end of file diff --git a/app/models/template.rb b/app/models/template.rb index cc8912c..87d79df 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -31,6 +31,21 @@ # What do they do? do they do it efficiently, and do we need them? + ## + # deep copy the given template and all of it's associations + # + # @params [Template] template to be deep copied + # @return [Template] saved copied template + def self.deep_copy(template) + template_copy = template.dup + template_copy.save! + template.phases.each do |phase| + phase_copy = Phase.deep_copy(phase) + phase_copy.template_id = template_copy.id + phase_copy.save! + end + return template_copy + end ## # takes a type or organisation and returns all published templates from diff --git a/app/views/dmptemplates/_add_question.html.erb b/app/views/dmptemplates/_add_question.html.erb deleted file mode 100644 index b9bbfc8..0000000 --- a/app/views/dmptemplates/_add_question.html.erb +++ /dev/null @@ -1,183 +0,0 @@ - - -<% @new_question = Question.new %> -<% @new_question.number = section.questions.count + 1 %> - - -<%= form_for @new_question, :url => {:action => "admin_createquestion"}, :html => {:id => "new_question_#{section.id}"} do |f| %> -<%= f.hidden_field :section_id, :value => section.id %> -
| <%= t("org_admin.questions.question_number_label")%> | -<%= f.number_field :number, :in => 1..50, :class => "number_field has-tooltip", "data-toggle" => "tooltip", "title" => t("org_admin.questions.number_help_text") %> - - - | -
| <%= t("org_admin.questions.question_text_label")%> | -<%= f.text_area :text, :rows => "5", :id => "new_question_text_#{section.id}" %> - - | -
| <%= t("org_admin.questions.answer_format_label")%> | -<%= f.hidden_field :section_id, :value => section.id, :class => "section_id" %>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <%= f.select :question_format_id,
- #options_from_collection_for_select(QuestionFormat.all.order("title"), :id, :title, QuestionFormat.find_by_title(t("helpers.text_area")).id),
- # the above was the line but it doesn't work because in the DB
- # the QuestionFormat title is in English (Text area)
- # but the above uses the Fr translation and so gets a nil
- options_from_collection_for_select(QuestionFormat.all.order("title"), :id, :title, QuestionFormat.find_by_title("Text area").id),
- {}, :id => "new-select-format-#{section.id}"%>
-
-
- <%= link_to( image_tag("help_button.png"), "#", :class => "question_format_popover", :rel => "popover", "data-html" => "true", "data-content" => t("org_admin.questions.question_format_help_text_html"))%>
-
-
- |
-
| <%= t("org_admin.questions.suggested_or_example_answer_label")%> | -
- <% suggested_answer = @new_question.suggested_answers.build %>
- <%= f.fields_for :suggested_answers, suggested_answer do |s|%>
- <%= s.hidden_field :organisation_id, :value => current_user.organisation.id %>
-
-
- <%= link_to( image_tag("help_button.png"), "#", :class => "suggested_answer_popover", :rel => "popover", "data-html" => "true", "data-content" => t("org_admin.questions.suggested_answer_help_text_html"))%>
-
-
-
-
- |
-
| <%= t("org_admin.questions.guidance_label")%> | -
- <%= text_area_tag("new-question-guidance", "", class: "tinymce") %>
-
-
- <%= link_to( image_tag("help_button.png"), "#", :class => "question_guidance_popover", :rel => "popover", "data-html" => "true", "data-content" => t("org_admin.questions.question_guidance_help_text_html"))%>
-
-
-
- |
-
| <%= t("org_admin.questions.themes_label")%> | -
- <%= f.collection_select(:theme_ids,
- Theme.all.order("title"),
- :id, :title, {:prompt => false, :include_blank => t('helpers.none')}, {:multiple => true})%>
-
-
- <%= link_to( image_tag("help_button.png"), "#", :class => "question_themes_popover", :rel => "popover", "data-html" => "true", "data-content" => t("org_admin.questions.question_themes_help_text_html"))%>
-
-
-
- |
-