Don't use the cache in admin budget stats
In commite51e03446, we started using the same code to show stats in the public area and in the admin area. However, in doing so we introduced a bug, since stats in the public area are only shown after a certain part of the process has finished, meaning the stats appearing on the page never change (in theory), so it's perfectly fine to cache them. However, in the admin area stats can be accessed while the process is still ongoing, so caching the stats will lead to the wrong results being displayed. We've thought about expiring the cache when new supports or ballot lines are added; however, that means the methods calculating the stats for the supporting phase would expire when supports are added/removed but the methods calculating the stats for the voting phase would expire when ballot lines are added/removed. It gets even more complex because the `headings` method calculates stats for both the supporting and the voting phases. So, since loading stats in the admin section is fast even without the cache because they only load very basic statistics, we're taking the simple approach of disabling the cache in this case, so everything works the same way it did before commite51e03446. Co-authored-by: Javi Martín <javim@elretirao.net>
This commit is contained in:
committed by
Javi Martín
parent
a4461a1a56
commit
16f844595d
@@ -3,7 +3,7 @@ module Statisticable
|
||||
PARTICIPATIONS = %w[gender age geozone].freeze
|
||||
|
||||
included do
|
||||
attr_reader :resource
|
||||
attr_reader :resource, :cache
|
||||
end
|
||||
|
||||
class_methods do
|
||||
@@ -42,8 +42,9 @@ module Statisticable
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(resource)
|
||||
def initialize(resource, cache: true)
|
||||
@resource = resource
|
||||
@cache = cache
|
||||
end
|
||||
|
||||
def generate
|
||||
@@ -226,7 +227,11 @@ module Statisticable
|
||||
end
|
||||
end
|
||||
|
||||
def stats_cache(key, &)
|
||||
Rails.cache.fetch(full_cache_key_for(key), expires_at: Date.current.end_of_day, &)
|
||||
def stats_cache(key, &block)
|
||||
if cache
|
||||
Rails.cache.fetch(full_cache_key_for(key), expires_at: Date.current.end_of_day, &block)
|
||||
else
|
||||
block.call
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user