From a7a4c5428ee39498c60c8bcb08b8acdd8a466a7f Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 26 Sep 2024 10:08:07 +0200 Subject: [PATCH] Fix broken tests --- app/models/personal_data_on_space_in_list.rb | 7 ++++--- app/models/personal_space_list.rb | 2 +- app/models/personal_space_lists_space.rb | 6 ++++-- spec/models/personal_space_list_spec.rb | 6 ++++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/models/personal_data_on_space_in_list.rb b/app/models/personal_data_on_space_in_list.rb index 6b39b5a5..96e7cc50 100644 --- a/app/models/personal_data_on_space_in_list.rb +++ b/app/models/personal_data_on_space_in_list.rb @@ -2,13 +2,14 @@ class PersonalDataOnSpaceInList < ApplicationRecord belongs_to :space - belongs_to :personal_space_list, touch: true + belongs_to :personal_space_list belongs_to :personal_space_lists_space, query_constraints: [:space_id, :personal_space_list_id], optional: true enum contact_status: { not_contacted: 0, said_no: 1, said_maybe: 2, said_yes: 3 } + after_create :update_personal_space_list_counters + after_update :update_personal_space_list_counters after_destroy :update_personal_space_list_counters - after_save :update_personal_space_list_counters ICON_FOR_CONTACT_STATUS = { "not_contacted" => "unknown", @@ -20,7 +21,7 @@ class PersonalDataOnSpaceInList < ApplicationRecord private def update_personal_space_list_counters - personal_space_list.update_counter_caches + personal_space_list.update_counter_caches if personal_space_list.present? end end diff --git a/app/models/personal_space_list.rb b/app/models/personal_space_list.rb index 3331465b..0993e1b3 100644 --- a/app/models/personal_space_list.rb +++ b/app/models/personal_space_list.rb @@ -52,7 +52,7 @@ def space_contacted_count end def update_counter_caches - update( + update!( space_count: this_lists_personal_data_on_spaces.count, space_not_contacted_count: this_lists_personal_data_on_spaces.not_contacted.count, space_said_no_count: this_lists_personal_data_on_spaces.said_no.count, diff --git a/app/models/personal_space_lists_space.rb b/app/models/personal_space_lists_space.rb index 230256ea..612f1c3f 100644 --- a/app/models/personal_space_lists_space.rb +++ b/app/models/personal_space_lists_space.rb @@ -3,14 +3,16 @@ class PersonalSpaceListsSpace < ApplicationRecord self.primary_key = [:space_id, :personal_space_list_id] - belongs_to :personal_space_list, touch: true + belongs_to :personal_space_list belongs_to :space has_many :personal_data_on_space_in_lists, query_constraints: [:space_id, :personal_space_list_id], dependent: nil # Keep it around in case the space is added again after_create :set_up_personal_data_on_space_in_list - after_commit :update_personal_space_list_counters + after_create :update_personal_space_list_counters + after_update :update_personal_space_list_counters + after_destroy :update_personal_space_list_counters def set_up_personal_data_on_space_in_list PersonalDataOnSpaceInList.find_or_create_by(personal_space_list:, space:) diff --git a/spec/models/personal_space_list_spec.rb b/spec/models/personal_space_list_spec.rb index 2cf1cfc6..971dd54f 100644 --- a/spec/models/personal_space_list_spec.rb +++ b/spec/models/personal_space_list_spec.rb @@ -93,6 +93,7 @@ contact_status: "said_no" ) + users_space_list.reload expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(1) expect(users_space_list.space_said_no_count).to eq(1) @@ -103,6 +104,7 @@ contact_status: "said_maybe" ) + users_space_list.reload expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(1) expect(users_space_list.space_said_no_count).to eq(0) @@ -113,6 +115,7 @@ contact_status: "said_yes" ) + users_space_list.reload expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(1) expect(users_space_list.space_said_no_count).to eq(0) @@ -135,6 +138,7 @@ contact_status: "said_no" ) + users_space_list.reload expect(users_space_list.space_count).to eq(2) expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(1) @@ -143,6 +147,7 @@ # Removing the space changes the counts users_space_list.remove_space(space_to_add_to_list) + users_space_list.reload expect(users_space_list.space_count).to eq(1) expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(0) @@ -151,6 +156,7 @@ # Re-adding the space remembers the state and count we had users_space_list.add_space(space_to_add_to_list) + users_space_list.reload expect(users_space_list.space_count).to eq(2) expect(users_space_list.space_not_contacted_count).to eq(1) expect(users_space_list.space_contacted_count).to eq(1)