From b84f71d55b4447a851e10cf189801c95d5e05062 Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Sat, 2 Mar 2024 22:31:48 +0100 Subject: [PATCH] Move aggregating after a facility review is created to the facility review model, instead of controller - now that we only make one review at a time. --- app/controllers/facility_reviews_controller.rb | 3 --- app/models/facility_review.rb | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/facility_reviews_controller.rb b/app/controllers/facility_reviews_controller.rb index 3d7da449..676329d3 100644 --- a/app/controllers/facility_reviews_controller.rb +++ b/app/controllers/facility_reviews_controller.rb @@ -29,9 +29,6 @@ def create return create_failed unless FacilityReview.create!(relevant_reviews) - # NB: Under the current model, this needs to happen before we add the descriptions with update_space_facilities, - # as aggregating DESTROYS all space_facilities, including the descriptions (wtf) - @space.aggregate_facility_reviews(facilities: @affected_facilities) update_space_facilities(reviews_and_descriptions[:space_facilities_attributes].values) create_succeeded diff --git a/app/models/facility_review.rb b/app/models/facility_review.rb index eac8df8a..4dd6c7b6 100644 --- a/app/models/facility_review.rb +++ b/app/models/facility_review.rb @@ -13,6 +13,10 @@ class FacilityReview < ApplicationRecord validates :space, uniqueness: { scope: [:user, :facility] } + after_commit do + space.aggregate_facility_reviews(facilities: [facility]) + end + def experience_icon ICON_FOR_EXPERIENCE[experience] end