Skip to content

Commit

Permalink
Clear stats cache after adding/removing PB supports/votes
Browse files Browse the repository at this point in the history
  • Loading branch information
microweb10 committed Mar 30, 2024
1 parent f65ea7c commit 9315907
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/budget/stats.rb
Expand Up @@ -181,6 +181,11 @@ def supports(supportable)
stats_cache(*stats_methods)

def stats_cache(key, &)
Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &)
if budget.balloting_or_later?
cache_key = "budgets_stats/#{budget.id}/#{budget.lines.cache_key_with_version}/#{key}/#{version}"
else
cache_key = "budgets_stats/#{budget.id}/#{supports(budget).cache_key_with_version}/#{key}/#{version}"
end
Rails.cache.fetch(cache_key, &)
end
end
41 changes: 41 additions & 0 deletions spec/system/admin/stats_spec.rb
Expand Up @@ -111,6 +111,27 @@
expect(page).to have_link "Go back", count: 1
end

scenario "Clear cache after new supports", :with_cache do
investment = create(:budget_investment, heading: heading_all_city)
create(:user, :level_two, votables: [investment])

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

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

create(:user, :level_two, votables: [investment])

refresh

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

scenario "hide final voting link" do
visit admin_stats_path
click_link "Participatory Budgets"
Expand Down Expand Up @@ -147,6 +168,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 9315907

Please sign in to comment.