From cce984908ce61216bc207207872f983b0e61fff1 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Fri, 19 Apr 2024 13:35:18 +0100 Subject: [PATCH] write helper method to support having the correct rent period checkboxes checked --- app/helpers/organisations_helper.rb | 10 +++++++ spec/helpers/organisations_helper_spec.rb | 35 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/helpers/organisations_helper.rb b/app/helpers/organisations_helper.rb index cf53bd244..fd056b9bd 100644 --- a/app/helpers/organisations_helper.rb +++ b/app/helpers/organisations_helper.rb @@ -37,4 +37,14 @@ module OrganisationsHelper end end end + + def rent_periods_with_checked_attr(checked_periods: nil) + all_rent_periods = RentPeriod.rent_period_mappings + rent_periods = all_rent_periods.map do |code, period| + period_copy = period.clone + period_copy[:checked] = true if checked_periods.nil? || checked_periods.include?(code) + [code, period_copy] + end + rent_periods.to_h + end end diff --git a/spec/helpers/organisations_helper_spec.rb b/spec/helpers/organisations_helper_spec.rb index f8afb3e04..f7a3653a4 100644 --- a/spec/helpers/organisations_helper_spec.rb +++ b/spec/helpers/organisations_helper_spec.rb @@ -20,4 +20,39 @@ RSpec.describe OrganisationsHelper do ) end end + + describe "rent_periods_with_checked_attr" do + let(:fake_rent_periods) do + { + "1" => { "value" => "Every minute" }, + "2" => { "value" => "Every decade" }, + } + end + + before do + allow(RentPeriod).to receive(:rent_period_mappings).and_return fake_rent_periods + end + + it "returns rent_period_mappings" do + actual = rent_periods_with_checked_attr + expect(actual.keys).to eq RentPeriod.rent_period_mappings.keys + end + + context "when checked_periods is nil" do + it "returns all rent periods with checked true" do + actual = rent_periods_with_checked_attr + checked_attrs = actual.values.map { |p| p[:checked] } + expect(checked_attrs).to all be true + end + end + + context "when checked_periods is not nil" do + it "returns the rent_periods with the correct values checked" do + checked_rent_period = "1" + actual = rent_periods_with_checked_attr(checked_periods: [checked_rent_period]) + expect(actual[checked_rent_period][:checked]).to be true + expect(actual["2"][:checked]).to be_falsey + end + end + end end