Browse Source

Add new questions

pull/968/head
Jack S 4 years ago
parent
commit
cd1e3846a3
  1. 19
      app/models/form/lettings/pages/created_by.rb
  2. 32
      app/models/form/lettings/pages/housing_provider.rb
  3. 32
      app/models/form/lettings/pages/managing_organisation.rb
  4. 48
      app/models/form/lettings/questions/created_by_id.rb
  5. 71
      app/models/form/lettings/questions/housing_provider.rb
  6. 77
      app/models/form/lettings/questions/managing_organisation.rb
  7. 55
      spec/models/form/lettings/pages/created_by_spec.rb
  8. 128
      spec/models/form/lettings/pages/housing_provider_spec.rb
  9. 130
      spec/models/form/lettings/pages/managing_organisation_spec.rb
  10. 82
      spec/models/form/lettings/questions/created_by_id_spec.rb
  11. 105
      spec/models/form/lettings/questions/housing_provider_spec.rb
  12. 143
      spec/models/form/lettings/questions/managing_organisation_spec.rb

19
app/models/form/lettings/pages/created_by.rb

@ -0,0 +1,19 @@
class Form::Lettings::Pages::CreatedBy < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "created_by"
@header = ""
@description = ""
@subsection = subsection
end
def questions
@questions ||= [
Form::Lettings::Questions::CreatedById.new(nil, nil, self),
]
end
def routed_to?(_log, current_user)
!!current_user&.support?
end
end

32
app/models/form/lettings/pages/housing_provider.rb

@ -0,0 +1,32 @@
class Form::Lettings::Pages::HousingProvider < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "housing_provider"
@header = ""
@description = ""
@subsection = subsection
end
def questions
@questions ||= [
Form::Lettings::Questions::HousingProvider.new(nil, nil, self),
]
end
# For an organisation owns and manages ONLY their own housing stock AND NOT uses agents to manage the properties AND NOT manages housing stock of other organisations?
# In "set up this lettings log" no extra question should be shown
def routed_to?(log, current_user)
return false unless current_user
return true if current_user.support?
return true unless current_user.organisation.holds_own_stock?
housing_providers = current_user.organisation.housing_providers
return false if housing_providers.count.zero?
return true if housing_providers.count > 1
log.update!(owning_organisation: housing_providers.first)
false
end
end

32
app/models/form/lettings/pages/managing_organisation.rb

@ -0,0 +1,32 @@
class Form::Lettings::Pages::ManagingOrganisation < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "managing_organisation"
@header = ""
@description = ""
@subsection = subsection
end
def questions
@questions ||= [
Form::Lettings::Questions::ManagingOrganisation.new(nil, nil, self),
]
end
# For an organisation owns and manages ONLY their own housing stock AND NOT uses agents to manage the properties AND NOT manages housing stock of other organisations?
# In "set up this lettings log" no extra question should be shown
def routed_to?(log, current_user)
return false unless current_user
return true if current_user.support?
return true unless current_user.organisation.holds_own_stock?
managing_agents = current_user.organisation.managing_agents
return false if managing_agents.count.zero?
return true if managing_agents.count > 1
log.update!(managing_organisation: managing_agents.first)
false
end
end

48
app/models/form/lettings/questions/created_by_id.rb

@ -0,0 +1,48 @@
class Form::Lettings::Questions::CreatedById < ::Form::Question
def initialize(id, hsh, page)
super
@id = "created_by_id"
@check_answer_label = "Log owner"
@header = "Which user are you creating this log for?"
@hint_text = ""
@type = "select"
@page = page
end
def answer_options
answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected?
User.select(:id, :name, :email).each_with_object(answer_opts) do |user, hsh|
hsh[user.id] = "#{user.name} (#{user.email})"
hsh
end
end
def displayed_answer_options(log, _user = nil)
return answer_options unless log.owning_organisation
user_ids = log.owning_organisation.users.pluck(:id) + [""]
answer_options.select { |k, _v| user_ids.include?(k) }
end
def label_from_value(value)
return unless value
answer_options[value]
end
def hidden_in_check_answers?(_log, current_user)
!current_user.support?
end
def derived?
true
end
private
def selected_answer_option_is_derived?(_log)
true
end
end

71
app/models/form/lettings/questions/housing_provider.rb

@ -0,0 +1,71 @@
class Form::Lettings::Questions::HousingProvider < ::Form::Question
attr_accessor :current_user, :log
def initialize(id, hsh, page)
super
@id = "owning_organisation_id"
@check_answer_label = "Housing provider"
@header = "Which organisation owns this property?"
@type = "select"
@page = page
end
def answer_options
answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected?
return answer_opts unless current_user
if !current_user.support? && current_user.organisation.holds_own_stock?
answer_opts[current_user.organisation.id] = "#{current_user.organisation.name} (Your organisation)"
end
housing_providers.select(:id, :name).each_with_object(answer_opts) do |organisation, hsh|
hsh[organisation.id] = organisation.name
hsh
end
end
def displayed_answer_options(log, user = nil)
@current_user = user
@log = log
answer_options
end
def label_from_value(value)
return unless value
answer_options[value]
end
def derived?
true
end
def hidden_in_check_answers?(_log, user = nil)
@current_user = user
return false unless @current_user
# Hide when less than 2 housing providers
housing_providers.count < 2
end
def enabled
true
end
private
def selected_answer_option_is_derived?(_log)
true
end
def housing_providers
@housing_providers ||= if current_user.support?
Organisation.all
else
current_user.organisation.housing_providers
end
end
end

77
app/models/form/lettings/questions/managing_organisation.rb

@ -0,0 +1,77 @@
class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question
attr_accessor :current_user, :log
def initialize(id, hsh, page)
super
@id = "managing_organisation_id"
@check_answer_label = "Managing agent"
@header = "Which organisation manages this letting?"
@type = "select"
@answer_options = answer_options
@page = page
end
def answer_options
opts = { "" => "Select an option" }
return opts unless ActiveRecord::Base.connected?
return opts unless current_user
return opts unless log
if current_user.support?
if log.owning_organisation.holds_own_stock?
opts[log.owning_organisation.id] = "#{log.owning_organisation.name} (Owning organisation)"
end
elsif current_user.organisation.holds_own_stock?
opts[current_user.organisation.id] = "#{current_user.organisation.name} (Your organisation)"
end
managing_organisations.select(:id, :name).each_with_object(opts) do |organisation, hsh|
hsh[organisation.id] = organisation.name
hsh
end
end
def displayed_answer_options(log, user)
@current_user = user
@log = log
answer_options
end
def label_from_value(value)
return unless value
answer_options[value]
end
def derived?
true
end
def hidden_in_check_answers?(_log, user = nil)
@current_user = user
return false unless @current_user
# Hide when less than 2 managing_agents
managing_organisations.count < 2
end
def enabled
true
end
private
def selected_answer_option_is_derived?(_log)
true
end
def managing_organisations
@managing_organisations ||= if current_user.support?
log.owning_organisation
else
current_user.organisation
end.managing_agents
end
end

55
spec/models/form/lettings/pages/created_by_spec.rb

@ -0,0 +1,55 @@
require "rails_helper"
RSpec.describe Form::Lettings::Pages::CreatedBy, 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) }
let(:form) { instance_double(Form) }
let(:lettings_log) { instance_double(LettingsLog) }
describe "#routed_to?" do
context "when nil" do
it "is not shown" do
expect(page.routed_to?(nil, nil)).to eq(false)
end
end
context "when support" do
it "is shown" do
expect(page.routed_to?(nil, create(:user, :support))).to eq(true)
end
end
context "when not support" do
it "is not shown" do
expect(page.routed_to?(nil, create(:user, :data_coordinator))).to eq(false)
end
end
end
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[created_by_id])
end
it "has the correct id" do
expect(page.id).to eq("created_by")
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 be nil
end
end

128
spec/models/form/lettings/pages/housing_provider_spec.rb

@ -0,0 +1,128 @@
require "rails_helper"
RSpec.describe Form::Lettings::Pages::HousingProvider, 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) }
let(:form) { instance_double(Form) }
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[owning_organisation_id])
end
it "has the correct id" do
expect(page.id).to eq("housing_provider")
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 be nil
end
describe "#routed_to?" do
let(:log) { create(:lettings_log, owning_organisation_id: nil) }
context "when user nil" do
it "is not shown" do
expect(page.routed_to?(log, nil)).to eq(false)
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, nil) }.not_to change(log.reload, :owning_organisation).from(nil)
end
end
context "when support" do
let(:user) { create(:user, :support) }
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
end
end
context "when not support" do
context "when does not hold own stock" do
let(:user) do
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false))
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
end
end
context "when holds own stock" do
let(:user) do
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true))
end
context "with 0 housing_providers" do
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
end
end
context "with >1 housing_providers" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation)
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
end
end
context "with 1 housing_providers" do
let(:housing_provider) { create(:organisation) }
before do
create(
:organisation_relationship,
:owning,
child_organisation: user.organisation,
parent_organisation: housing_provider,
)
end
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
it "updates owning_organisation_id" do
expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(housing_provider)
end
end
end
end
end
end

130
spec/models/form/lettings/pages/managing_organisation_spec.rb

@ -0,0 +1,130 @@
require "rails_helper"
RSpec.describe Form::Lettings::Pages::ManagingOrganisation, 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) }
let(:form) { instance_double(Form) }
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[managing_organisation_id])
end
it "has the correct id" do
expect(page.id).to eq("managing_organisation")
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 be nil
end
describe "#routed_to?" do
let(:log) { create(:lettings_log) }
let(:organisation) { create(:organisation) }
context "when user nil" do
it "is not shown" do
expect(page.routed_to?(log, nil)).to eq(false)
end
it "does not update managing_organisation_id" do
expect { page.routed_to?(log, nil) }.not_to change(log.reload, :managing_organisation)
end
end
context "when support" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, :support, organisation:) }
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update managing_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation)
end
end
context "when not support" do
context "when does not hold own stock" do
let(:user) do
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false))
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update managing_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation)
end
end
context "when holds own stock" do
let(:user) do
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true))
end
context "with 0 managing_agents" do
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
it "does not update managing_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation)
end
end
context "with >1 managing_agents" do
before do
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "does not update managing_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation)
end
end
context "with 1 managing_agents" do
let(:managing_agent) { create(:organisation) }
before do
create(
:organisation_relationship,
:managing,
child_organisation: managing_agent,
parent_organisation: user.organisation,
)
end
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
it "updates managing_organisation_id" do
expect { page.routed_to?(log, user) }.to change(log.reload, :managing_organisation).to(managing_agent)
end
end
end
end
end
end

82
spec/models/form/lettings/questions/created_by_id_spec.rb

@ -0,0 +1,82 @@
require "rails_helper"
RSpec.describe Form::Lettings::Questions::CreatedById, 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) }
let(:subsection) { instance_double(Form::Subsection) }
let(:form) { instance_double(Form) }
let(:user_1) { FactoryBot.create(:user, name: "first user") }
let(:user_2) { FactoryBot.create(:user, name: "second user") }
let!(:expected_answer_options) do
{
"" => "Select an option",
user_1.id => "#{user_1.name} (#{user_1.email})",
user_2.id => "#{user_2.name} (#{user_2.email})",
}
end
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("created_by_id")
end
it "has the correct header" do
expect(question.header).to eq("Which user are you creating this log for?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Log owner")
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("")
end
it "has the correct answer options" do
expect(question.answer_options).to eq(expected_answer_options)
end
it "is marked as derived" do
expect(question.derived?).to be true
end
context "when the current user is support" do
let(:support_user) { FactoryBot.build(:user, :support) }
it "is shown in check answers" do
expect(question.hidden_in_check_answers?(nil, support_user)).to be false
end
end
context "when the current user is not support" do
let(:user) { FactoryBot.build(:user) }
it "is not shown in check answers" do
expect(question.hidden_in_check_answers?(nil, user)).to be true
end
end
context "when the owning organisation is already set" do
let(:lettings_log) { FactoryBot.create(:lettings_log, owning_organisation: user_2.organisation) }
let(:expected_answer_options) do
{
"" => "Select an option",
user_2.id => "#{user_2.name} (#{user_2.email})",
}
end
it "only displays users that belong to that organisation" do
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer_options)
end
end
end

105
spec/models/form/lettings/questions/housing_provider_spec.rb

@ -0,0 +1,105 @@
require "rails_helper"
RSpec.describe Form::Lettings::Questions::HousingProvider, 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) }
let(:subsection) { instance_double(Form::Subsection) }
let(:form) { instance_double(Form) }
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("owning_organisation_id")
end
it "has the correct header" do
expect(question.header).to eq("Which organisation owns this property?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Housing provider")
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 be_nil
end
describe "answer options" do
let(:options) { { "" => "Select an option" } }
context "when current_user nil" do
it "shows default options" do
expect(question.answer_options).to eq(options)
end
end
context "when user not support and owns own stock" do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
let(:options) do
{
"" => "Select an option",
user.organisation.id => "#{user.organisation.name} (Your organisation)",
}
end
before do
question.current_user = user
end
it "shows housing providers with own org at the top" do
expect(question.answer_options).to eq(options)
end
end
context "when user support" do
before do
question.current_user = create(:user, :support)
end
let(:expected_opts) do
Organisation.all.each_with_object(options) do |organisation, hsh|
hsh[organisation.id] = organisation.name
hsh
end
end
it "shows all orgs" do
expect(question.answer_options).to eq(expected_opts)
end
end
end
it "is marked as derived" do
expect(question.derived?).to be true
end
describe "#hidden_in_check_answers?" do
let(:user) { create(:user) }
context "when housing providers >= 2" do
it "is shown in check answers" do
expect(question.hidden_in_check_answers?(nil, user)).to be true
end
end
context "when housing providers < 2" do
before do
create(:organisation_relationship, :owning, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation)
end
it "is not shown in check answers" do
expect(question.hidden_in_check_answers?(nil, user)).to be false
end
end
end
end

143
spec/models/form/lettings/questions/managing_organisation_spec.rb

@ -0,0 +1,143 @@
require "rails_helper"
RSpec.describe Form::Lettings::Questions::ManagingOrganisation, 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) }
let(:subsection) { instance_double(Form::Subsection) }
let(:form) { instance_double(Form) }
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("managing_organisation_id")
end
it "has the correct header" do
expect(question.header).to eq("Which organisation manages this letting?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Managing agent")
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 be_nil
end
describe "#displayed_answer_options" do
let(:options) { { "" => "Select an option" } }
context "when current_user nil" do
let(:log) { create(:lettings_log) }
it "shows default options" do
expect(question.displayed_answer_options(log, nil)).to eq(options)
end
end
context "when log nil" do
let(:user) { create(:user) }
it "shows default options" do
expect(question.displayed_answer_options(nil, user)).to eq(options)
end
end
context "when user not support and owns own stock" do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
let(:log) { create(:lettings_log) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) }
let(:options) do
{
"" => "Select an option",
user.organisation.id => "#{user.organisation.name} (Your organisation)",
org_rel1.child_organisation.id => org_rel1.child_organisation.name,
org_rel2.child_organisation.id => org_rel2.child_organisation.name,
}
end
it "shows managing agents with own org at the top" do
expect(question.displayed_answer_options(log, user)).to eq(options)
end
end
context "when support user and org does not own own stock" do
let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: false) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let(:options) do
{
"" => "Select an option",
org_rel1.child_organisation.id => org_rel1.child_organisation.name,
org_rel2.child_organisation.id => org_rel2.child_organisation.name,
}
end
it "shows owning org managing agents with hint" do
expect(question.displayed_answer_options(log, user)).to eq(options)
end
end
context "when support user and org does own stock" do
let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: true) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) }
let(:options) do
{
"" => "Select an option",
log_owning_org.id => "#{log_owning_org.name} (Owning organisation)",
org_rel1.child_organisation.id => org_rel1.child_organisation.name,
org_rel2.child_organisation.id => org_rel2.child_organisation.name,
}
end
it "shows owning org managing agents
" do
expect(question.displayed_answer_options(log, user)).to eq(options)
end
end
end
it "is marked as derived" do
expect(question.derived?).to be true
end
describe "#hidden_in_check_answers?" do
let(:user) { create(:user) }
context "when managing agents >= 2" do
it "is shown in check answers" do
expect(question.hidden_in_check_answers?(nil, user)).to be true
end
end
context "when managing agents < 2" do
before do
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
create(:organisation_relationship, :managing, parent_organisation: user.organisation)
end
it "is not shown in check answers" do
expect(question.hidden_in_check_answers?(nil, user)).to be false
end
end
end
end
Loading…
Cancel
Save