Browse Source

update lettings logs

pull/981/head
Kat 4 years ago
parent
commit
78b5055a18
  1. 2
      app/controllers/locations_controller.rb
  2. 1
      app/models/lettings_log.rb
  3. 14
      spec/requests/locations_controller_spec.rb

2
app/controllers/locations_controller.rb

@ -164,7 +164,7 @@ private
def confirm_deactivation
if @location.update(deactivation_date: params[:location][:deactivation_date])
# update the logs
@location.lettings_logs.filter_by_before_startdate( params[:location][:deactivation_date]).update(location: nil)
flash[:notice] = "#{@location.name} has been deactivated"
end
redirect_to scheme_locations_path(@scheme)

1
app/models/lettings_log.rb

@ -38,6 +38,7 @@ class LettingsLog < Log
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) }
scope :search_by, lambda { |param|
filter_by_location_postcode(param)
.or(filter_by_tenant_code(param))

14
spec/requests/locations_controller_spec.rb

@ -1239,6 +1239,8 @@ RSpec.describe LocationsController, type: :request do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) }
let!(:lettings_log_to_deactivate) { FactoryBot.create(:lettings_log, scheme:, location:, startdate: Time.utc(2022, 10, 11) ) }
let!(:active_lettings_log) { FactoryBot.create(:lettings_log, scheme:, location:, startdate: Time.utc(2022, 10, 9) ) }
let(:startdate) { Time.utc(2021, 1, 2) }
let(:deactivation_date) { Time.utc(2022, 10, 10) }
@ -1267,7 +1269,7 @@ RSpec.describe LocationsController, type: :request do
end
context "when confirming deactivation" do
let(:params) { { location: { deactivation_date: Time.utc(2022, 10, 10), confirm: true } } }
let(:params) { { location: { deactivation_date: deactivation_date, confirm: true } } }
it "updates existing location with valid deactivation date and renders location page" do
follow_redirect!
@ -1276,6 +1278,16 @@ RSpec.describe LocationsController, type: :request do
location.reload
expect(location.deactivation_date).to eq(deactivation_date)
end
it "updates lettings logs with a startdate later than deactivation_date" do
lettings_log_to_deactivate.reload
expect(lettings_log_to_deactivate.location).to eq(nil)
end
it "does not update lettings logs with a startdate earlier than deactivation_date" do
active_lettings_log.reload
expect(active_lettings_log.location).to eq(location)
end
end
context "when the date is not selected" do

Loading…
Cancel
Save