From 653848fc4e9a6f466a8418ffcc3de34f6b93786e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 8 Apr 2024 14:53:13 +0200 Subject: [PATCH] Extract method to get the stats key in stats This way we remove a bit of duplication and it'll be easier to change the `stats_cache` method. --- app/models/budget/stats.rb | 4 ++-- app/models/concerns/statisticable.rb | 4 ++++ app/models/poll/stats.rb | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index 4d62cd6de..04cc6d480 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -192,7 +192,7 @@ class Budget::Stats stats_cache(*stats_methods) - def stats_cache(key, &) - Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &) + def full_cache_key_for(key) + "budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}" end end diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 9d47720ee..8e4ea424c 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -224,4 +224,8 @@ module Statisticable I18n.t("stats.age_range", start: start, finish: finish) end end + + def stats_cache(key, &) + Rails.cache.fetch(full_cache_key_for(key), &) + end end diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index c449bf17d..0aeb93109 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -121,7 +121,7 @@ class Poll::Stats stats_cache(*stats_methods) - def stats_cache(key, &) - Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &) + def full_cache_key_for(key) + "polls_stats/#{poll.id}/#{key}/#{version}" end end