Browse Source

WIP

pull/705/head
Dushan Despotovic 3 years ago
parent
commit
083f4c6915
  1. 4
      app/models/form/setup/pages/location.rb
  2. 18
      app/models/form/setup/questions/location.rb
  3. 9
      app/models/form/setup/questions/scheme_id.rb
  4. 36
      spec/models/form/setup/pages/location_spec.rb
  5. 40
      spec/models/form/setup/questions/location_spec.rb
  6. 28
      spec/models/form/setup/questions/scheme_id_spec.rb

4
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 # Only display if there is more than one location
@depends_on = [{ @depends_on = [{
"supported_housing_schemes_enabled?" => true, "supported_housing_schemes_enabled?" => true,
scheme.locations.size > 1 => true # scheme.locations.size > 1 => true
}] }]
@derived = true @derived = true
end end
def questions def questions
[ [
Form::Setup::Questions::SchemeId.new(nil, nil, self) Form::Setup::Questions::Location.new(nil, nil, self)
] ]
end end
end end

18
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

9
app/models/form/setup/questions/scheme_id.rb

@ -1,6 +1,7 @@
class Form::Setup::Questions::SchemeId < ::Form::Question class Form::Setup::Questions::SchemeId < ::Form::Question
def initialize(id, hsh, page) def initialize(id, hsh, page)
super("scheme_id", hsh, page) super("scheme_id", hsh, page)
@check_answer_label = "Scheme name"
@header = "What scheme is this log for?" @header = "What scheme is this log for?"
@hint_text = "Enter scheme name or postcode" @hint_text = "Enter scheme name or postcode"
@type = "select" @type = "select"
@ -10,13 +11,19 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
def answer_options def answer_options
answer_opts = {} answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh| Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh|
hsh[scheme.id] = scheme.service_name hsh[scheme.id] = scheme.service_name
hsh hsh
end end
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 private
def selected_answer_option_is_derived?(_case_log) def selected_answer_option_is_derived?(_case_log)

36
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

40
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

28
spec/models/form/setup/questions/scheme_id_spec.rb

@ -20,7 +20,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
end end
it "has the correct check_answer_label" do 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 end
it "has the correct type" do 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 expect(question.conditional_for).to be_nil
end 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 it "is not marked as derived" do
expect(question.derived?).to be false expect(question.derived?).to be false
end 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 end

Loading…
Cancel
Save