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 %> -
    -
  • <%= s.select :is_example, {t("org_admin.questions.example_answer_label") => true, t("org_admin.questions.suggested_answer_label") => false} %>
  • -
  • <%= s.text_area :text, :rows => 5 %>
  • -
- <%end%> -
-
- <%= 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"))%> -
- - -
-
- - -
- <%= hidden_field_tag :section_id, section.id, :class => "section_id" %> - <%= f.submit t("helpers.submit.save"), :class => "btn btn-primary new_question_save_button" %> - <%= hidden_field_tag :section_id, section.id, :class => "section_id_new" %> - <%= link_to t("helpers.submit.cancel"), '#', :class => "btn cancel cancel_add_new_question" %> -
-
- -<%end%> - diff --git a/app/views/dmptemplates/_admin_nav_tabs.html.erb b/app/views/dmptemplates/_admin_nav_tabs.html.erb deleted file mode 100644 index e2237ea..0000000 --- a/app/views/dmptemplates/_admin_nav_tabs.html.erb +++ /dev/null @@ -1,34 +0,0 @@ - - - diff --git a/app/views/templates/_admin_nav_tabs.html.erb b/app/views/templates/_admin_nav_tabs.html.erb index 0134df6..d5cb6c6 100644 --- a/app/views/templates/_admin_nav_tabs.html.erb +++ b/app/views/templates/_admin_nav_tabs.html.erb @@ -19,7 +19,7 @@ <% end %> - <% if current_user.can_org_admin? && (template.org_type != constant("organisation_types.funder")|| current_user.org_type == constant("organisation_types.funder")) %> + <% if current_user.can_org_admin? && (template.org.org_type != constant("organisation_types.funder")|| current_user.org_type == constant("organisation_types.funder")) %> <% if active == 'add_plan' %>
  • <% else %> diff --git a/app/views/templates/_show_phases_sections.html.erb b/app/views/templates/_show_phases_sections.html.erb index 63f5a08..606aabb 100644 --- a/app/views/templates/_show_phases_sections.html.erb +++ b/app/views/templates/_show_phases_sections.html.erb @@ -12,7 +12,7 @@ <% if phase.sections.length > 0 %> - <%= link_to t("helpers.preview"), admin_previewphase_template_path(phase), class: 'btn btn-primary'%> + <%= link_to t("helpers.preview"), admin_previewphase_template_path(id: phase.id), class: 'btn btn-primary'%> <% end %> <% if !phase.template.published? %> <%= link_to t("helpers.submit.delete"), admin_destroyphase_template_path(phase_id: phase.id), diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 99dde0d..027aa65 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -44,7 +44,19 @@ <%= raw org_template.description.truncate(90, omission: t('helpers.truncate_continued')) %> - <%= org_template.published %> + <% #Yes if published version exists, Yes[Unpublished changes] if newer version modified, No otherwise%> + <% if org_template.published %> + <%= "Yes" %> + <% elsif org_template.version > 0 && Template.where(dmptemplate_id: org_template.dmptemplate_id, published: true).present? %> + <% #there is a published version, but this version is not %> + <% if org_template.created_at < org_template.updated_at %> + <%= "Yes [Unpublished Changes]" %> + <% else %> + <%= "Yes" %> + <% end %> + <% else %> + <%= "No" %> + <% end %> <% last_temp_updated = org_template.updated_at %> @@ -61,11 +73,11 @@ <%= link_to t('helpers.history'), admin_template_history_template_path(id: org_template.id), class: "dmp_table_link" %> - <%end%> + <% end %> -<%end%> +<% end %>
    @@ -92,7 +104,7 @@ <% @templates_funders.each do |org_template| %> - <% if org_template.published? ||org_template.has_customisations?(current_user.organisation_id, org_template) then %> + <% if org_template.published ||org_template.has_customisations?(current_user.organisation_id, org_template) then %> <%= org_template.title%> diff --git a/db/schema.rb b/db/schema.rb index 2c02f1d..81d309b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,6 +13,9 @@ ActiveRecord::Schema.define(version: 20170201194502) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "answers", force: :cascade do |t| t.text "text" t.integer "plan_id" @@ -34,8 +37,8 @@ t.integer "plan_id" t.integer "user_id" t.string "format" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "file_types", force: :cascade do |t| @@ -43,8 +46,8 @@ t.string "icon_name" t.integer "icon_size" t.string "icon_location" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "file_uploads", force: :cascade do |t| @@ -55,8 +58,8 @@ t.boolean "published" t.string "location" t.integer "file_type_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "friendly_id_slugs", force: :cascade do |t| @@ -73,8 +76,8 @@ create_table "guidance_groups", force: :cascade do |t| t.string "name" t.integer "org_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "optional_subset" t.boolean "published" end @@ -82,8 +85,8 @@ create_table "guidances", force: :cascade do |t| t.text "text" t.integer "guidance_group_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "question_id" t.boolean "published" end @@ -125,8 +128,8 @@ t.string "abbreviation" t.string "target_url" t.string "wayfless_entity" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "parent_id" t.boolean "is_other" t.string "sort_name" @@ -142,8 +145,8 @@ create_table "perms", force: :cascade do |t| t.string "name" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end add_index "perms", ["name"], name: "index_perms_on_name" @@ -168,8 +171,8 @@ t.boolean "selected" end - add_index "plan_guidance_groups", ["guidance_group_id"], name: "index_plan_guidance_groups_on_guidance_group_id" - add_index "plan_guidance_groups", ["plan_id"], name: "index_plan_guidance_groups_on_plan_id" + add_index "plan_guidance_groups", ["guidance_group_id"], name: "index_plan_guidance_groups_on_guidance_group_id", using: :btree + add_index "plan_guidance_groups", ["plan_id"], name: "index_plan_guidance_groups_on_plan_id", using: :btree create_table "plans", force: :cascade do |t| t.integer "project_id" @@ -191,8 +194,8 @@ create_table "question_formats", force: :cascade do |t| t.string "title" t.text "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "option_based", default: false end @@ -257,16 +260,16 @@ t.text "value" t.integer "target_id", null: false t.string "target_type", null: false - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true create_table "splash_logs", force: :cascade do |t| t.string "destination" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "suggested_answers", force: :cascade do |t| @@ -296,8 +299,8 @@ create_table "themes", force: :cascade do |t| t.string "title" t.text "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "locale" end @@ -354,16 +357,52 @@ t.integer "language_id" end - add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true - add_index "users", ["email"], name: "index_users_on_email", unique: true - add_index "users", ["invitation_token"], name: "index_users_on_invitation_token", unique: true - add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree + add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree + add_index "users", ["invitation_token"], name: "index_users_on_invitation_token", unique: true, using: :btree + add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree create_table "users_perms", id: false, force: :cascade do |t| t.integer "user_id" t.integer "perm_id" end - add_index "users_perms", ["user_id", "perm_id"], name: "index_users_perms_on_user_id_and_perm_id" + add_index "users_perms", ["user_id", "perm_id"], name: "index_users_perms_on_user_id_and_perm_id", using: :btree + add_foreign_key "answers", "plans" + add_foreign_key "answers", "questions" + add_foreign_key "answers", "users" + add_foreign_key "answers_question_options", "answers" + add_foreign_key "answers_question_options", "question_options" + add_foreign_key "guidance_groups", "orgs" + add_foreign_key "guidances", "guidance_groups" + add_foreign_key "notes", "answers" + add_foreign_key "notes", "users" + add_foreign_key "org_token_permissions", "orgs" + add_foreign_key "org_token_permissions", "token_permission_types" + add_foreign_key "orgs", "languages" + add_foreign_key "orgs", "regions" + add_foreign_key "phases", "templates" + add_foreign_key "plan_guidance_groups", "guidance_groups" + add_foreign_key "plan_guidance_groups", "plans" + add_foreign_key "plans", "templates" + add_foreign_key "question_options", "questions" + add_foreign_key "questions", "question_formats" + add_foreign_key "questions", "sections" + add_foreign_key "questions_themes", "questions" + add_foreign_key "questions_themes", "themes" + add_foreign_key "roles", "plans" + add_foreign_key "roles", "users" + add_foreign_key "sections", "phases" + add_foreign_key "suggested_answers", "orgs" + add_foreign_key "suggested_answers", "questions" + add_foreign_key "templates", "orgs" + add_foreign_key "themes_in_guidance", "guidances" + add_foreign_key "themes_in_guidance", "themes" + add_foreign_key "user_identifiers", "identifier_schemes" + add_foreign_key "user_identifiers", "users" + add_foreign_key "users", "languages" + add_foreign_key "users", "orgs" + add_foreign_key "users_perms", "perms" + add_foreign_key "users_perms", "users" end