diff --git a/app/models/form/setup/pages/location.rb b/app/models/form/setup/pages/location.rb index 149d59d18..2f58c9353 100644 --- a/app/models/form/setup/pages/location.rb +++ b/app/models/form/setup/pages/location.rb @@ -7,14 +7,14 @@ class Form::Setup::Pages::Location < ::Form::Page # Only display if there is more than one location @depends_on = [{ "supported_housing_schemes_enabled?" => true, - scheme.locations.size > 1 => true + # scheme.locations.size > 1 => true }] @derived = true end def questions [ - Form::Setup::Questions::SchemeId.new(nil, nil, self) + Form::Setup::Questions::Location.new(nil, nil, self) ] end end diff --git a/app/models/form/setup/questions/location.rb b/app/models/form/setup/questions/location.rb new file mode 100644 index 000000000..6327fbceb --- /dev/null +++ b/app/models/form/setup/questions/location.rb @@ -0,0 +1,18 @@ +class Form::Setup::Questions::Location < ::Form::Question + def initialize(id, hsh, page) + super + @id = "location" + @check_answer_label = "Location" + @header = "Which location used by is this log for?" + @hint_text = "" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @derived = true unless FeatureToggle.supported_housing_schemes_enabled? + @page = page + end + + ANSWER_OPTIONS = { + "1" => { "value" => "General needs" }, + "2" => { "value" => "Supported housing" }, + }.freeze +end diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index 2df8219c6..02571b916 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -1,6 +1,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question def initialize(id, hsh, page) super("scheme_id", hsh, page) + @check_answer_label = "Scheme name" @header = "What scheme is this log for?" @hint_text = "Enter scheme name or postcode" @type = "select" @@ -10,13 +11,19 @@ class Form::Setup::Questions::SchemeId < ::Form::Question def answer_options answer_opts = {} 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 end end + 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) + answer_options.select { |k, _v| user_org_scheme_ids.include?(k) } + end + + private def selected_answer_option_is_derived?(_case_log) diff --git a/spec/models/form/setup/pages/location_spec.rb b/spec/models/form/setup/pages/location_spec.rb new file mode 100644 index 000000000..82118f236 --- /dev/null +++ b/spec/models/form/setup/pages/location_spec.rb @@ -0,0 +1,36 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Pages::Location, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[location]) + end + + it "has the correct id" do + expect(page.id).to eq("location") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has the correct depends_on" do + expect(page.depends_on).to eq([{ + "supported_housing_schemes_enabled?" => true, + scheme.locations.size > 1 => true + }]) + end +end diff --git a/spec/models/form/setup/questions/location_spec.rb b/spec/models/form/setup/questions/location_spec.rb new file mode 100644 index 000000000..809e24049 --- /dev/null +++ b/spec/models/form/setup/questions/location_spec.rb @@ -0,0 +1,40 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Questions::Location, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("location") + end + + xit "has the correct header" do + expect(question.header).to eq("Which location used by #{scheme.service_name} is this log for?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Location") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is marked as derived" do + expect(question.derived?).to be true + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "1" => { "value" => "General needs" }, + "2" => { "value" => "Supported housing" }, + }) + end +end diff --git a/spec/models/form/setup/questions/scheme_id_spec.rb b/spec/models/form/setup/questions/scheme_id_spec.rb index 75bfd827a..f96e70680 100644 --- a/spec/models/form/setup/questions/scheme_id_spec.rb +++ b/spec/models/form/setup/questions/scheme_id_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do end it "has the correct check_answer_label" do - expect(question.check_answer_label).to eq("Rent type") + expect(question.check_answer_label).to eq("Scheme name") end it "has the correct type" do @@ -35,18 +35,22 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do expect(question.conditional_for).to be_nil end - it "has the correct answer_options" do - expect(question.answer_options).to eq({ - "1" => { "value" => "Affordable Rent" }, - "2" => { "value" => "London Affordable Rent" }, - "4" => { "value" => "London Living Rent" }, - "3" => { "value" => "Rent to Buy" }, - "0" => { "value" => "Social Rent" }, - "5" => { "value" => "Other intermediate rent product" }, - }) - end - it "is not marked as derived" do expect(question.derived?).to be false end + + context "When a user is signed in" do + let(:organisation) { FactoryBot.create(:organisation)} + let(:organisation_2) { FactoryBot.create(:organisation)} + let(:user) { FactoryBot.create(:user, organisation_id: organisation.id) } + let(:scheme) { FactoryBot.create(:scheme, organisation_id: organisation.id ) } + let!(:scheme_2) { FactoryBot.create(:scheme, organisation_id: organisation_2.id ) } + let(:case_log) { FactoryBot.create(:case_log, created_by: user)} + + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { scheme.id => "#{scheme.service_name}" } + expect(question.displayed_answer_options(case_log)).to eq(expected_answer) + end + end end