Browse Source
* Introduce scheme and location pages * Derive single scheme location * Reset location when a scheme is changed Co-authored-by: Dushan Despotovic <dushan@madetech.com> Co-authored-by: Katrina Kosiak <katrina@madetech.com>pull/710/head v0.1.24
Stéphane Meny
3 years ago
committed by
GitHub
28 changed files with 382 additions and 35 deletions
@ -0,0 +1,19 @@
|
||||
class Form::Setup::Pages::Location < ::Form::Page |
||||
def initialize(_id, hsh, subsection) |
||||
super("location", hsh, subsection) |
||||
@header = "" |
||||
@description = "" |
||||
@questions = questions |
||||
@depends_on = [{ |
||||
"supported_housing_schemes_enabled?" => true, |
||||
"needstype" => 2, |
||||
"scheme_has_multiple_locations?" => true, |
||||
}] |
||||
end |
||||
|
||||
def questions |
||||
[ |
||||
Form::Setup::Questions::LocationId.new(nil, nil, self), |
||||
] |
||||
end |
||||
end |
@ -0,0 +1,18 @@
|
||||
class Form::Setup::Pages::Scheme < ::Form::Page |
||||
def initialize(_id, hsh, subsection) |
||||
super("scheme", hsh, subsection) |
||||
@header = "" |
||||
@description = "" |
||||
@questions = questions |
||||
@depends_on = [{ |
||||
"supported_housing_schemes_enabled?" => true, |
||||
"needstype" => 2, |
||||
}] |
||||
end |
||||
|
||||
def questions |
||||
[ |
||||
Form::Setup::Questions::SchemeId.new(nil, nil, self), |
||||
] |
||||
end |
||||
end |
@ -0,0 +1,38 @@
|
||||
class Form::Setup::Questions::LocationId < ::Form::Question |
||||
def initialize(_id, hsh, page) |
||||
super("location_id", hsh, page) |
||||
@check_answer_label = "Location" |
||||
@header = "Which location is this log for?" |
||||
@hint_text = "" |
||||
@type = "radio" |
||||
@derived = true unless FeatureToggle.supported_housing_schemes_enabled? |
||||
@answer_options = answer_options |
||||
end |
||||
|
||||
def answer_options |
||||
answer_opts = {} |
||||
return answer_opts unless ActiveRecord::Base.connected? |
||||
|
||||
Location.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| |
||||
hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } |
||||
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).map(&:to_s) |
||||
answer_options.select { |k, _v| scheme_location_ids.include?(k) } |
||||
end |
||||
|
||||
def hidden_in_check_answers?(case_log, _current_user = nil) |
||||
!supported_housing_selected?(case_log) |
||||
end |
||||
|
||||
private |
||||
|
||||
def supported_housing_selected?(case_log) |
||||
case_log.needstype == 2 |
||||
end |
||||
end |
@ -0,0 +1,40 @@
|
||||
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" |
||||
@answer_options = answer_options |
||||
@derived = true unless FeatureToggle.supported_housing_schemes_enabled? |
||||
end |
||||
|
||||
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.to_s] = scheme.service_name |
||||
hsh |
||||
end |
||||
end |
||||
|
||||
def displayed_answer_options(case_log) |
||||
return {} unless case_log.created_by |
||||
|
||||
user_org_scheme_ids = Scheme.select(:id).where(organisation_id: case_log.created_by.organisation_id).map(&:id) |
||||
answer_options.select do |k, _v| |
||||
user_org_scheme_ids.include?(k.to_i) |
||||
end |
||||
end |
||||
|
||||
def hidden_in_check_answers?(case_log, _current_user = nil) |
||||
!supported_housing_selected?(case_log) |
||||
end |
||||
|
||||
private |
||||
|
||||
def supported_housing_selected?(case_log) |
||||
case_log.needstype == 2 |
||||
end |
||||
end |
@ -0,0 +1,5 @@
|
||||
class AddLocationToCaseLog < ActiveRecord::Migration[7.0] |
||||
def change |
||||
add_reference :case_logs, :location, foreign_key: true |
||||
end |
||||
end |
@ -0,0 +1,8 @@
|
||||
class RenameLocationName < ActiveRecord::Migration[7.0] |
||||
def change |
||||
change_table :locations, bulk: true do |t| |
||||
t.rename :address_line1, :name |
||||
t.remove :address_line2, type: :string |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,37 @@
|
||||
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_id]) |
||||
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, |
||||
"needstype" => 2, |
||||
"scheme_has_multiple_locations?" => true, |
||||
}]) |
||||
end |
||||
end |
@ -0,0 +1,33 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Pages::Scheme, 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[scheme_id]) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(page.id).to eq("scheme") |
||||
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([{ "needstype" => 2, "supported_housing_schemes_enabled?" => true }]) |
||||
end |
||||
end |
@ -0,0 +1,37 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Questions::LocationId, 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_id") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("Which location 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).not_to be_derived |
||||
end |
||||
|
||||
it "has the correct answer_options" do |
||||
expect(question.answer_options).to eq({}) |
||||
end |
||||
end |
@ -0,0 +1,58 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Questions::SchemeId, 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("scheme_id") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("What scheme is this log for?") |
||||
end |
||||
|
||||
it "has the correct check_answer_label" do |
||||
expect(question.check_answer_label).to eq("Scheme name") |
||||
end |
||||
|
||||
it "has the correct type" do |
||||
expect(question.type).to eq("select") |
||||
end |
||||
|
||||
it "has the correct hint_text" do |
||||
expect(question.hint_text).to eq("Enter scheme name or postcode") |
||||
end |
||||
|
||||
it "has the correct conditional_for" do |
||||
expect(question.conditional_for).to be_nil |
||||
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(:case_log) { FactoryBot.create(:case_log, created_by: user) } |
||||
|
||||
before do |
||||
FactoryBot.create(:scheme, organisation_id: organisation_2.id) |
||||
end |
||||
|
||||
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do |
||||
expected_answer = { scheme.id.to_s => scheme.service_name } |
||||
expect(question.displayed_answer_options(case_log)).to eq(expected_answer) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue