Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJackson-Oslo committed Sep 26, 2024
1 parent 081e3be commit a7a4c54
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
7 changes: 4 additions & 3 deletions app/models/personal_data_on_space_in_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/models/personal_space_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions app/models/personal_space_lists_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down
6 changes: 6 additions & 0 deletions spec/models/personal_space_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a7a4c54

Please sign in to comment.