diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index 15d45f3..21084a9 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -29,17 +29,19 @@ # where guidance is a hash with the text and the org name theme_guidance = {} - guidance_groups.each do |guidance_group| - guidance_group.guidances.where(published: true).each do |guidance| - guidance.themes.each do |theme| - title = theme.title - if !theme_guidance.has_key?(title) - theme_guidance[title] = Array.new + guidance_groups.includes(guidances:[:themes]).each do |guidance_group| + guidance_group.guidances.each do |guidance| + if guidance.published + guidance.themes.each do |theme| + title = theme.title + if !theme_guidance.has_key?(title) + theme_guidance[title] = Array.new + end + theme_guidance[title] << { + text: guidance.text, + org: guidance_group.name + ':' + } end - theme_guidance[title] << { - text: guidance.text, - org: guidance_group.name + ':' - } end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 57b504f..b26a62e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -41,7 +41,7 @@ else if perms.include? perm @user.perms << perm - if perm.name == Perm.use_api.id + if perm.id == Perm.use_api.id @user.keep_or_generate_token! end end diff --git a/app/models/plan.rb b/app/models/plan.rb index 1eee8a6..8688f34 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -159,7 +159,7 @@ # find all the themes in this plan # and get the guidance groups they belong to ggroups = [] - self.template.phases.each do |phase| + Template.includes(phases: [sections: [questions: [themes: [guidances: [guidance_group: :org]]]]]).find(self.template_id).phases.each do |phase| phase.sections.each do |section| section.questions.each do |question| question.themes.each do |theme| diff --git a/app/models/user.rb b/app/models/user.rb index c1350f6..f900030 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -53,7 +53,7 @@ ## # Scopes - default_scope { includes(:org, :perms, :plans) } + default_scope { includes(:org, :perms) } diff --git a/db/migrate/20170702012742_ensure_indexes_in_place.rb b/db/migrate/20170702012742_ensure_indexes_in_place.rb new file mode 100644 index 0000000..6bd9410 --- /dev/null +++ b/db/migrate/20170702012742_ensure_indexes_in_place.rb @@ -0,0 +1,40 @@ +class EnsureIndexesInPlace < ActiveRecord::Migration + def change + #users_perms + remove_index :users_perms, name: 'index_users_perms_on_user_id_and_perm_id' + add_index :users_perms, :user_id + #user_identifiers + add_index :user_identifiers, :user_id + #roles + add_index :roles, :user_id + add_index :roles, :plan_id + #org_token_permissions + add_index :org_token_permissions, :org_id + #users + add_index :users, :org_id + remove_index :users, :confirmation_token + remove_index :users, :invitation_token + remove_index :users, :reset_password_token + #notes + add_index :notes, :answer_id + #guidance_groups + add_index :guidance_groups, :org_id + #guidance + add_index :guidances, :guidance_group_id + #themes_in_guidance + add_index :themes_in_guidance, :theme_id + add_index :themes_in_guidance, :guidance_id + #annotations + add_index :annotations, :question_id + #question_themes + remove_index :questions_themes, name: 'question_theme_index' + remove_index :questions_themes, name: 'theme_question_index' + add_index :questions_themes, :question_id + #question_options + add_index :question_options, :question_id + #answers_question_options + remove_index :answers_question_options, name: 'answer_question_option_index' + remove_index :answers_question_options, name: 'question_option_answer_index' + add_index :answers_question_options, :answer_id + end +end diff --git a/db/schema.rb b/db/schema.rb index fd97cce..1c7f711 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170428083711) do +ActiveRecord::Schema.define(version: 20170702012742) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,6 +25,8 @@ t.datetime "updated_at" end + add_index "annotations", ["question_id"], name: "index_annotations_on_question_id", using: :btree + create_table "answers", force: :cascade do |t| t.text "text" t.integer "plan_id" @@ -40,8 +42,7 @@ t.integer "question_option_id", null: false end - add_index "answers_question_options", ["answer_id", "question_option_id"], name: "answer_question_option_index", using: :btree - add_index "answers_question_options", ["question_option_id", "answer_id"], name: "question_option_answer_index", using: :btree + add_index "answers_question_options", ["answer_id"], name: "index_answers_question_options_on_answer_id", using: :btree create_table "exported_plans", force: :cascade do |t| t.integer "plan_id" @@ -93,6 +94,8 @@ t.boolean "published" end + add_index "guidance_groups", ["org_id"], name: "index_guidance_groups_on_org_id", using: :btree + create_table "guidances", force: :cascade do |t| t.text "text" t.integer "guidance_group_id" @@ -102,6 +105,8 @@ t.boolean "published" end + add_index "guidances", ["guidance_group_id"], name: "index_guidances_on_guidance_group_id", using: :btree + create_table "identifier_schemes", force: :cascade do |t| t.string "name" t.string "description" @@ -129,6 +134,8 @@ t.datetime "updated_at" end + add_index "notes", ["answer_id"], name: "index_notes_on_answer_id", using: :btree + create_table "org_token_permissions", force: :cascade do |t| t.integer "org_id" t.integer "token_permission_type_id" @@ -136,6 +143,8 @@ t.datetime "updated_at" end + add_index "org_token_permissions", ["org_id"], name: "index_org_token_permissions_on_org_id", using: :btree + create_table "orgs", force: :cascade do |t| t.string "name" t.string "abbreviation" @@ -219,6 +228,8 @@ t.datetime "updated_at" end + add_index "question_options", ["question_id"], name: "index_question_options_on_question_id", using: :btree + create_table "questions", force: :cascade do |t| t.text "text" t.text "default_value" @@ -238,8 +249,7 @@ t.integer "theme_id", null: false end - add_index "questions_themes", ["question_id", "theme_id"], name: "question_theme_index", using: :btree - add_index "questions_themes", ["theme_id", "question_id"], name: "theme_question_index", using: :btree + add_index "questions_themes", ["question_id"], name: "index_questions_themes_on_question_id", using: :btree create_table "regions", force: :cascade do |t| t.string "abbreviation" @@ -256,6 +266,9 @@ t.integer "access", default: 0, null: false end + add_index "roles", ["plan_id"], name: "index_roles_on_plan_id", using: :btree + add_index "roles", ["user_id"], name: "index_roles_on_user_id", using: :btree + create_table "sections", force: :cascade do |t| t.string "title" t.text "description" @@ -319,6 +332,9 @@ t.integer "guidance_id" end + add_index "themes_in_guidance", ["guidance_id"], name: "index_themes_in_guidance_on_guidance_id", using: :btree + add_index "themes_in_guidance", ["theme_id"], name: "index_themes_in_guidance_on_theme_id", using: :btree + create_table "token_permission_types", force: :cascade do |t| t.string "token_type" t.text "text_description" @@ -334,14 +350,16 @@ t.integer "identifier_scheme_id" end + add_index "user_identifiers", ["user_id"], name: "index_user_identifiers_on_user_id", using: :btree + 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.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "encrypted_password", default: "" t.string "reset_password_token" t.datetime "reset_password_sent_at" @@ -367,17 +385,15 @@ t.integer "language_id" end - 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 + add_index "users", ["org_id"], name: "index_users_on_org_id", 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", using: :btree + add_index "users_perms", ["user_id"], name: "index_users_perms_on_user_id", using: :btree add_foreign_key "annotations", "orgs" add_foreign_key "annotations", "questions"