diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index f96c41c88..d473bb2b7 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -21,6 +21,8 @@ class LettingsLog < Log include Validations::DateValidations include Validations::FinancialValidations + default_scope { where(visible: true) } + has_paper_trail validates_with LettingsLogValidator @@ -38,6 +40,9 @@ class LettingsLog < Log belongs_to :location, optional: true belongs_to :managing_organisation, class_name: "Organisation", optional: true + scope :visible, -> { where(visible: true) } + scope :invisible, -> { where(visible: false) } + scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } diff --git a/db/migrate/20230329091101_add_visible_to_lettings_logs.rb b/db/migrate/20230329091101_add_visible_to_lettings_logs.rb new file mode 100644 index 000000000..21f37d237 --- /dev/null +++ b/db/migrate/20230329091101_add_visible_to_lettings_logs.rb @@ -0,0 +1,7 @@ +class AddVisibleToLettingsLogs < ActiveRecord::Migration[7.0] + def change + add_column :lettings_logs, :visible, :boolean, null: false, default: true + + add_index :lettings_logs, :visible + end +end diff --git a/db/schema.rb b/db/schema.rb index 225129dfd..0d646598a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_03_20_084057) do +ActiveRecord::Schema[7.0].define(version: 2023_03_29_091101) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -287,6 +287,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_20_084057) do t.string "town_or_city" t.string "county" t.integer "carehome_charges_value_check" + t.boolean "visible", default: true, null: false t.index ["bulk_upload_id"], name: "index_lettings_logs_on_bulk_upload_id" t.index ["created_by_id"], name: "index_lettings_logs_on_created_by_id" t.index ["location_id"], name: "index_lettings_logs_on_location_id" @@ -295,6 +296,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_20_084057) do t.index ["owning_organisation_id"], name: "index_lettings_logs_on_owning_organisation_id" t.index ["scheme_id"], name: "index_lettings_logs_on_scheme_id" t.index ["updated_by_id"], name: "index_lettings_logs_on_updated_by_id" + t.index ["visible"], name: "index_lettings_logs_on_visible" end create_table "local_authorities", force: :cascade do |t| diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index ca4ff7e85..dcc506733 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -39,6 +39,19 @@ RSpec.describe LettingsLog do expect(lettings_log).to be_lettings end + describe "default_scope" do + let!(:visible_log) { create(:lettings_log) } + let!(:invisible_log) { create(:lettings_log, visible: false) } + + it "includes visible logs" do + expect(described_class.all).to include(visible_log) + end + + it "excludes non visible logs" do + expect(described_class.all).not_to include(invisible_log) + end + end + describe "#form" do let(:lettings_log) { build(:lettings_log, created_by: created_by_user) } let(:lettings_log_2) { build(:lettings_log, startdate: Time.zone.local(2022, 1, 1), created_by: created_by_user) }