diff --git a/app/models/organisation.rb b/app/models/organisation.rb index bb636624c..d0b6b8299 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -60,8 +60,8 @@ class Organisation < ApplicationRecord end def rent_period_labels - labels = rent_periods.map { |period| RentPeriod.rent_period_mappings[period.to_s]["value"] } - labels.presence || %w[All] + labels = rent_periods.map { |period| RentPeriod.rent_period_mappings.dig(period.to_s, "value") } + labels.compact.presence || %w[All] end def data_protection_confirmed? diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index cf9afe170..1dd61f4b2 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -124,11 +124,14 @@ RSpec.describe Organisation, type: :model do before do FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 2) FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 3) + + # Unmapped and ignored by `rent_period_labels` + FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 10) allow(RentPeriod).to receive(:rent_period_mappings).and_return(rent_period_mappings) end it "has rent periods associated" do - expect(organisation.rent_periods).to eq([2, 3]) + expect(organisation.rent_periods).to eq([2, 3, 10]) end it "maps the rent periods to display values" do