diff --git a/db/migrate/20161122152339_new_plan_template_structure.rb b/db/migrate/20161122152339_new_plan_template_structure.rb index 02f4f05..cfc942e 100644 --- a/db/migrate/20161122152339_new_plan_template_structure.rb +++ b/db/migrate/20161122152339_new_plan_template_structure.rb @@ -182,15 +182,15 @@ unless temp_match # no matches found, init template & phase & sections & questions & themes & options puts "creating new template for #{dmptemplate.title}" unless project.organisation.present? puts "creating new template for #{dmptemplate.title} customised by #{project.organisation.name}" unless project.organisation.nil? - template = initTemplate(dmptemplate) # needs to select next version of temp based on old_temp_id + modifiable = project.organisation.nil? + template = initTemplate(dmptemplate, modifiable, project.organisation_id) # needs to select next version of temp based on old_temp_id # some differences between a customised and un-customised template # customised templates need a different organisation_id - template.organisation_id = project.organisation_id # customised templates follow different version rules template.save! # since template was not a match, need to gen/copy all data below the template level versions.each do |version| - new_phase = initNewPhase(version.phase, version, template, true) + new_phase = initNewPhase(version.phase, version, template, modifiable) new_phase.save! sections = [] sections += version.sections.where("organisation_id = ? ", dmptemplate.organisation_id).pluck(:id) @@ -198,10 +198,10 @@ sections += Section.where(organisation_id: project.organisation_id, version_id: version.id).pluck(:id) end Section.includes(questions: [:themes, :options, :suggested_answers]).where(id: sections).each do |section| - new_section = initNewSection(section, new_phase, true) + new_section = initNewSection(section, new_phase, modifiable) new_section.save! section.questions.each do |question| - new_question = initNewQuestion(question, new_section, true) + new_question = initNewQuestion(question, new_section, modifiable) new_question.save! question.themes.each do |theme| new_question.themes << theme @@ -287,22 +287,26 @@ -def initTemplate(dmptemp) +def initTemplate(dmptemp, modifiable, organisation_id) template = Template.new template.title = dmptemp.title template.description = dmptemp.description template.published = dmptemp.published - template.organisation_id = dmptemp.organisation_id + template.organisation_id = organisation_id.present? ? organisation_id : dmptemp.organisation_id template.locale = dmptemp.locale template.is_default = dmptemp.is_default template.created_at = dmptemp.created_at template.updated_at = dmptemp.updated_at template.visibility = 0 # dummy value for private - template.customization_of = nil - template.version = Template.where(dmptemplate_id: dmptemp.id).blank? ? - 0 : Template.where(dmptemplate_id: dmptemp.id).pluck(:version).max + 1 - puts "NEW TEMPLATE: \n title: #{template.title} \n version: #{template.version} \n others_present? #{Template.where(dmptemplate_id: dmptemp.id).count}" + template.customization_of = modifiable ? nil : dmptemp.id template.dmptemplate_id = dmptemp.id + # if no templates with the same dmptemplate_id and organisation_id exist + # 0 + # otherwise + # take the maximum version from templates with the same dmptemplate_id and organisation_id and add 1 + template.version = Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).blank? ? + 0 : Template.where(dmptemplate_id: dmptemp.id, organisation_id: template.organisation_id).pluck(:version).max + 1 + puts "NEW TEMPLATE: \n title: #{template.title} \n version: #{template.version} \n others_present? #{Template.where(dmptemplate_id: dmptemp.id).count}" return template end diff --git a/db/schema.rb b/db/schema.rb index 3ff9498..bf990f6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161102221313) do +ActiveRecord::Schema.define(version: 20161122152339) 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" t.integer "user_id" t.integer "question_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "answers_options", id: false, force: :cascade do |t| @@ -27,14 +30,14 @@ t.integer "option_id", null: false end - add_index "answers_options", ["answer_id", "option_id"], name: "index_answers_options_on_answer_id_and_option_id" + add_index "answers_options", ["answer_id", "option_id"], name: "index_answers_options_on_answer_id_and_option_id", using: :btree create_table "comments", force: :cascade do |t| t.integer "user_id" t.integer "question_id" t.text "text" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "archived" t.integer "plan_id" t.integer "archived_by" @@ -44,10 +47,9 @@ t.string "title" t.text "description" t.boolean "published" - t.integer "user_id" t.integer "organisation_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "locale" t.boolean "is_default" end @@ -61,8 +63,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| @@ -70,8 +72,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| @@ -82,8 +84,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| @@ -93,15 +95,15 @@ t.datetime "created_at" end - add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true - add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id" - add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type" + add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true, using: :btree + add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree + add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree create_table "guidance_groups", force: :cascade do |t| t.string "name" t.integer "organisation_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 @@ -111,13 +113,13 @@ t.integer "guidance_group_id", null: false end - add_index "guidance_in_group", ["guidance_id", "guidance_group_id"], name: "index_guidance_in_group_on_guidance_id_and_guidance_group_id" + add_index "guidance_in_group", ["guidance_id", "guidance_group_id"], name: "index_guidance_in_group_on_guidance_id_and_guidance_group_id", using: :btree 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 @@ -129,10 +131,99 @@ t.boolean "default_language" end - create_table "option_warnings", force: :cascade do |t| - t.integer "organisation_id" - t.integer "option_id" + create_table "new_answers", force: :cascade do |t| t.text "text" + t.integer "new_plan_id" + t.integer "user_id" + t.integer "new_question_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "new_answers_question_options", id: false, force: :cascade do |t| + t.integer "new_answer_id", null: false + t.integer "question_option_id", null: false + end + + add_index "new_answers_question_options", ["new_answer_id", "question_option_id"], name: "answer_question_option_index", using: :btree + add_index "new_answers_question_options", ["question_option_id", "new_answer_id"], name: "question_option_answer_index", using: :btree + + create_table "new_phases", force: :cascade do |t| + t.string "title" + t.text "description" + t.integer "number" + t.integer "template_id" + t.datetime "created_at" + t.datetime "updated_at" + t.string "slug" + t.integer "vid" + t.boolean "modifiable" + end + + create_table "new_plans", force: :cascade do |t| + t.integer "project_id" + t.string "title" + t.integer "template_id" + t.datetime "created_at" + t.datetime "updated_at" + t.string "slug" + t.string "grant_number" + t.string "identifier" + t.text "description" + t.string "principal_investigator" + t.string "principal_investigator_identifier" + t.string "data_contact" + t.string "funder_name" + end + + create_table "new_questions", force: :cascade do |t| + t.text "text" + t.text "default_value" + t.text "guidance" + t.integer "number" + t.integer "new_section_id" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "question_format_id" + t.boolean "option_comment_display", default: true + t.boolean "modifiable" + t.integer "question_id" + end + + create_table "new_questions_themes", id: false, force: :cascade do |t| + t.integer "new_question_id", null: false + t.integer "theme_id", null: false + end + + add_index "new_questions_themes", ["new_question_id", "theme_id"], name: "question_theme_index", using: :btree + add_index "new_questions_themes", ["theme_id", "new_question_id"], name: "theme_question_index", using: :btree + + create_table "new_sections", force: :cascade do |t| + t.string "title" + t.text "description" + t.integer "number" + t.datetime "created_at" + t.datetime "updated_at" + t.boolean "published" + t.integer "new_phase_id" + t.boolean "modifiable" + end + + create_table "new_suggested_answers", force: :cascade do |t| + t.integer "new_question_id" + t.integer "organisation_id" + t.text "text" + t.boolean "is_example" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "notes", force: :cascade do |t| + t.integer "user_id" + t.text "text" + t.boolean "archived" + t.integer "new_answer_id" + t.integer "archived_by" t.datetime "created_at" t.datetime "updated_at" end @@ -142,8 +233,8 @@ t.string "text" t.integer "number" t.boolean "is_default" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "org_token_permissions", force: :cascade do |t| @@ -156,8 +247,8 @@ create_table "organisation_types", force: :cascade do |t| t.string "name" t.text "description" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "organisations", force: :cascade do |t| @@ -165,15 +256,14 @@ t.string "abbreviation" t.string "target_url" t.integer "organisation_type_id" - t.string "domain" t.string "wayfless_entity" - t.integer "stylesheet_file_id" - 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" t.text "banner_text" + t.string "logo_file_name" t.integer "region_id" t.integer "language_id" t.string "logo_uid" @@ -181,25 +271,34 @@ t.string "contact_email" end + create_table "perms", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "perms", ["name"], name: "index_perms_on_name", using: :btree + add_index "perms", ["name"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree + create_table "phases", force: :cascade do |t| t.string "title" t.text "description" t.integer "number" t.integer "dmptemplate_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "slug" end - add_index "phases", ["dmptemplate_id"], name: "index_phases_on_dmptemplate_id" - add_index "phases", ["slug"], name: "index_phases_on_slug", unique: true + add_index "phases", ["dmptemplate_id"], name: "index_phases_on_dmptemplate_id", using: :btree + add_index "phases", ["slug"], name: "index_phases_on_slug", unique: true, using: :btree create_table "plan_sections", force: :cascade do |t| t.integer "user_id" t.integer "section_id" t.integer "plan_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.datetime "release_time" end @@ -207,8 +306,8 @@ t.boolean "locked" t.integer "project_id" t.integer "version_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "project_groups", force: :cascade do |t| @@ -216,8 +315,8 @@ t.boolean "project_editor" t.integer "user_id" t.integer "project_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "project_administrator" end @@ -226,13 +325,13 @@ t.integer "guidance_group_id", null: false end - add_index "project_guidance", ["project_id", "guidance_group_id"], name: "index_project_guidance_on_project_id_and_guidance_group_id" + add_index "project_guidance", ["project_id", "guidance_group_id"], name: "index_project_guidance_on_project_id_and_guidance_group_id", using: :btree create_table "projects", force: :cascade do |t| t.string "title" t.integer "dmptemplate_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "slug" t.integer "organisation_id" t.string "grant_number" @@ -244,11 +343,21 @@ t.string "funder_name" end - add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true + add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true, using: :btree create_table "question_formats", force: :cascade do |t| t.string "title" t.text "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "question_options", force: :cascade do |t| + t.integer "new_question_id" + t.integer "option_id" + t.string "text" + t.integer "number" + t.boolean "is_default" t.datetime "created_at" t.datetime "updated_at" end @@ -258,12 +367,11 @@ t.text "default_value" t.text "guidance" t.integer "number" - t.integer "parent_id" t.integer "dependency_id" t.text "dependency_text" t.integer "section_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "question_format_id" t.boolean "option_comment_display", default: true end @@ -273,7 +381,7 @@ t.integer "theme_id", null: false end - add_index "questions_themes", ["question_id", "theme_id"], name: "index_questions_themes_on_question_id_and_theme_id" + add_index "questions_themes", ["question_id", "theme_id"], name: "index_questions_themes_on_question_id_and_theme_id", using: :btree create_table "region_groups", force: :cascade do |t| t.integer "super_region_id" @@ -287,25 +395,23 @@ end create_table "roles", force: :cascade do |t| - t.string "name" + t.boolean "creator" + t.boolean "editor" + t.boolean "administrator" + t.integer "user_id" + t.integer "new_plan_id" t.datetime "created_at" t.datetime "updated_at" - t.boolean "role_in_plans" - t.integer "resource_id" - t.string "resource_type" end - add_index "roles", ["name"], name: "index_roles_on_name" - add_index "roles", ["name"], name: "index_roles_on_name_and_resource_type_and_resource_id" - create_table "sections", force: :cascade do |t| t.string "title" t.text "description" t.integer "number" t.integer "version_id" t.integer "organisation_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "published" end @@ -314,32 +420,47 @@ 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 + add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree 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| t.integer "question_id" t.integer "organisation_id" t.text "text" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "is_example" + end + + create_table "templates", force: :cascade do |t| + t.string "title" + t.text "description" + t.boolean "published" + t.integer "organisation_id" + t.string "locale" + t.boolean "is_default" t.datetime "created_at" t.datetime "updated_at" - t.boolean "is_example" + t.integer "version" + t.integer "visibility" + t.integer "customization_of" + t.integer "dmptemplate_id" end 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 @@ -355,28 +476,12 @@ t.datetime "updated_at" end - create_table "user_statuses", force: :cascade do |t| - t.string "name" - t.text "description" - t.datetime "created_at" - t.datetime "updated_at" - end - - create_table "user_types", force: :cascade do |t| - t.string "name" - t.text "description" - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "users", force: :cascade do |t| t.string "firstname" t.string "surname" t.string "email", default: "", null: false t.string "orcid_id" t.string "shibboleth_id" - t.integer "user_type_id" - t.integer "user_status_id" t.datetime "created_at" t.datetime "updated_at" t.string "encrypted_password", default: "" @@ -405,17 +510,17 @@ 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_roles", id: false, force: :cascade do |t| + create_table "users_perms", id: false, force: :cascade do |t| t.integer "user_id" - t.integer "role_id" + t.integer "perm_id" end - add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id" + add_index "users_perms", ["user_id", "perm_id"], name: "index_users_perms_on_user_id_and_perm_id", using: :btree create_table "versions", force: :cascade do |t| t.string "title" @@ -423,10 +528,10 @@ t.boolean "published" t.integer "number" t.integer "phase_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - add_index "versions", ["phase_id"], name: "index_versions_on_phase_id" + add_index "versions", ["phase_id"], name: "index_versions_on_phase_id", using: :btree end