From 3c5b43aff99ab5d2dcc8d636cafb801e90291903 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 15 Nov 2022 15:20:11 +0000 Subject: [PATCH] Add location location_deactivations relationship --- app/models/location.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index 2df620292..a051c0fe0 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -4,6 +4,7 @@ class Location < ApplicationRecord validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" + has_many :location_deactivations, class_name: "LocationDeactivation" has_paper_trail @@ -11,7 +12,7 @@ class Location < ApplicationRecord auto_strip_attributes :name - attr_accessor :add_another_location, :deactivation_date_type, :run_deactivation_validations + attr_accessor :add_another_location, :deactivation_date_type, :deactivation_date, :run_deactivation_validations scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } @@ -374,8 +375,9 @@ class Location < ApplicationRecord end def status - return :active if deactivation_date.blank? - return :deactivating_soon if Time.zone.now < deactivation_date + return :active if location_deactivations.count == 0 + return :deactivating_soon if Time.zone.now < location_deactivations.first.deactivation_date + return :deactivated :deactivated end