Skip to content

Commit

Permalink
Clear stats cache after adding/removing PB votes
Browse files Browse the repository at this point in the history
  • Loading branch information
microweb10 committed Mar 28, 2024
1 parent f65ea7c commit 391b536
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/budget/ballot/line.rb
Expand Up @@ -16,6 +16,8 @@ class Line < ApplicationRecord
scope :by_investment, ->(investment_id) { where(investment_id: investment_id) }

before_validation :set_denormalized_ids
after_create :clear_stats_cache
after_destroy :clear_stats_cache

def check_enough_resources
ballot.lock!
Expand Down Expand Up @@ -43,6 +45,10 @@ def set_denormalized_ids
self.group_id ||= investment&.group_id
self.budget_id ||= investment&.budget_id
end

def clear_stats_cache
Budget::Stats.new(budget).clear_cache
end
end
end
end
8 changes: 8 additions & 0 deletions app/models/budget/stats.rb
Expand Up @@ -92,6 +92,14 @@ def headings
groups
end

def clear_cache
cache_keys = Rails.cache.instance_variable_get("@data")&.keys || []
budget_cache_keys = cache_keys.select { |cache_key| cache_key.starts_with?("budgets_stats/#{budget.id}/") }
budget_cache_keys.each do |key|
Rails.cache.delete(key)
end
end

private

def phase_methods
Expand Down
20 changes: 20 additions & 0 deletions spec/system/admin/stats_spec.rb
Expand Up @@ -147,6 +147,26 @@
expect(page).to have_content "VOTES\n3"
expect(page).to have_content "PARTICIPANTS\n2"
end

scenario "Clear cache after new votes", :with_cache do
create(:user, ballot_lines: [investment])

visit admin_stats_path
click_link "Participatory Budgets"
within("#budget_#{budget.id}") do
click_link "Final voting"
end

expect(page).to have_content "VOTES\n1"
expect(page).to have_content "PARTICIPANTS\n1"

create(:user, ballot_lines: [investment])

refresh

expect(page).to have_content "VOTES\n2"
expect(page).to have_content "PARTICIPANTS\n2"
end
end
end

Expand Down

0 comments on commit 391b536

Please sign in to comment.