diff --git a/app/models/organisation.rb b/app/models/organisation.rb index e5c26eec4..b55f371bd 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -38,13 +38,21 @@ class Organisation < ApplicationRecord end def local_authorities - organisation_las.pluck(:ons_code).map { |ons_code| ons_code } + organisation_las.pluck(:ons_code) end def local_authority_names local_authorities.map { |ons_code| LocalAuthority.ons_code_mappings[ons_code] } end + def rent_periods + organisation_rent_periods.pluck(:rent_period) + end + + def rent_period_labels + rent_periods.map { |period| RentPeriod.rent_period_mappings[period.to_s]["value"] } + end + def display_attributes [ { name: "name", value: name, editable: true }, @@ -52,6 +60,7 @@ class Organisation < ApplicationRecord { name: "telephone_number", value: phone, editable: true }, { name: "type", value: "Org type", editable: false }, { name: "local_authorities_operated_in", value: local_authority_names, editable: false, format: :bullet }, + { name: "rent_periods", value: rent_period_labels, editable: false, format: :bullet }, { name: "holds_own_stock", value: holds_own_stock.to_s.humanize, editable: false }, { name: "other_stock_owners", value: other_stock_owners, editable: false }, { name: "managing_agents", value: managing_agents, editable: false }, diff --git a/app/models/rent_period.rb b/app/models/rent_period.rb new file mode 100644 index 000000000..77e154f4c --- /dev/null +++ b/app/models/rent_period.rb @@ -0,0 +1,5 @@ +class RentPeriod + def self.rent_period_mappings + FormHandler.instance.current_form.get_question("period", nil).answer_options + end +end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index a60d5574a..1a64145b1 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -58,13 +58,22 @@ RSpec.describe Organisation, type: :model do end context "when the organisation only uses specific rent periods" do + let(:rent_period_mappings) do + { "2" => { "value" => "Weekly for 52 weeks" }, "3" => { "value" => "Every 2 weeks" } } + end + before do - FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 1) FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 2) + FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 3) + allow(RentPeriod).to receive(:rent_period_mappings).and_return(rent_period_mappings) end it "has rent periods associated" do - expect(organisation.organisation_rent_periods.pluck("rent_period")).to eq([1, 2]) + expect(organisation.rent_periods).to eq([2, 3]) + end + + it "maps the rent periods to display values" do + expect(organisation.rent_period_labels).to eq(["Weekly for 52 weeks", "Every 2 weeks"]) end end diff --git a/spec/models/rent_period_spec.rb b/spec/models/rent_period_spec.rb new file mode 100644 index 000000000..55252977a --- /dev/null +++ b/spec/models/rent_period_spec.rb @@ -0,0 +1,16 @@ +require "rails_helper" + +RSpec.describe RentPeriod, type: :model do + describe "rent period mapping" do + let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") } + + before do + allow(FormHandler.instance).to receive(:current_form).and_return(form) + end + + it "maps rent period id to display names" do + expect(described_class.rent_period_mappings).to be_a(Hash) + expect(described_class.rent_period_mappings["2"]).to eq({ "value" => "Weekly for 52 weeks" }) + end + end +end