diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 038df317f..08ae06a8f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -36,6 +36,7 @@ class CaseLog < ApplicationRecord belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :created_by, class_name: "User", optional: true belongs_to :scheme, optional: true + belongs_to :location, optional: true scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_status, ->(status, _user = nil) { where status: } diff --git a/app/models/form/setup/questions/location.rb b/app/models/form/setup/questions/location.rb index 146e54ead..5e669254a 100644 --- a/app/models/form/setup/questions/location.rb +++ b/app/models/form/setup/questions/location.rb @@ -3,16 +3,29 @@ class Form::Setup::Questions::Location < ::Form::Question super @id = "location" @check_answer_label = "Location" - @header = "Which location used by is this log for?" + @header = "Which location is this log for?" @hint_text = "" @type = "radio" - @answer_options = location_answers @derived = true unless FeatureToggle.supported_housing_schemes_enabled? @page = page + @answer_options = answer_options end - def location_answers - {} + def answer_options + answer_opts = {} + return answer_opts unless ActiveRecord::Base.connected? + + Location.select(:id, :postcode).each_with_object(answer_opts) do |location, hsh| + hsh[location.id] = location.postcode + hsh + end + end + + def displayed_answer_options(case_log) + return {} unless case_log.scheme + + scheme_location_ids = Location.where("scheme_id = #{case_log.scheme.id}").map(&:id) + answer_options.select { |k, _v| scheme_location_ids.include?(k) } end def hidden_in_check_answers?(case_log, _current_user = nil) diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index 9e76238b2..65c259663 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -13,7 +13,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question return answer_opts unless ActiveRecord::Base.connected? Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh| - hsh[scheme.id] = scheme.service_name + hsh[scheme.id.to_s] = scheme.service_name hsh end end @@ -21,7 +21,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question def displayed_answer_options(case_log) return {} unless case_log.created_by - user_org_scheme_ids = Scheme.where("organisation_id = #{case_log.created_by.organisation_id}").map(&:id) + user_org_scheme_ids = Scheme.where(organisation_id: case_log.created_by.organisation_id).map(&:id).map(&:to_s) answer_options.select { |k, _v| user_org_scheme_ids.include?(k) } end diff --git a/db/migrate/20220630154441_add_location_to_case_log.rb b/db/migrate/20220630154441_add_location_to_case_log.rb new file mode 100644 index 000000000..e53b8d83a --- /dev/null +++ b/db/migrate/20220630154441_add_location_to_case_log.rb @@ -0,0 +1,5 @@ +class AddLocationToCaseLog < ActiveRecord::Migration[7.0] + def change + add_reference :case_logs, :location, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 56f4c9b08..a566333dc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do +ActiveRecord::Schema[7.0].define(version: 2022_06_30_154441) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,15 +191,17 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do t.integer "joint" t.bigint "created_by_id" t.integer "illness_type_0" - t.integer "retirement_value_check" t.integer "tshortfall_known" t.integer "sheltered" + t.integer "retirement_value_check" t.integer "pregnancy_value_check" t.integer "hhtype" t.integer "new_old" t.integer "vacdays" t.bigint "scheme_id" + t.bigint "location_id" t.index ["created_by_id"], name: "index_case_logs_on_created_by_id" + t.index ["location_id"], name: "index_case_logs_on_location_id" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" @@ -251,7 +253,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do create_table "logs_exports", force: :cascade do |t| t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } - t.datetime "started_at", null: false + t.datetime "started_at", precision: nil, null: false t.integer "base_number", default: 1, null: false t.integer "increment_number", default: 1, null: false t.boolean "empty_export", default: false, null: false @@ -367,6 +369,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + add_foreign_key "case_logs", "locations" add_foreign_key "case_logs", "schemes" add_foreign_key "locations", "schemes" add_foreign_key "schemes", "organisations" diff --git a/spec/models/form/setup/questions/location_spec.rb b/spec/models/form/setup/questions/location_spec.rb index faf350cbf..988a1b53d 100644 --- a/spec/models/form/setup/questions/location_spec.rb +++ b/spec/models/form/setup/questions/location_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Form::Setup::Questions::Location, type: :model do end it "has the correct header" do - expect(question.header).to eq("Which location used by #{scheme.service_name} is this log for?") + expect(question.header).to eq("Which location is this log for?") end it "has the correct check_answer_label" do