1module HomeHelper |
|
2 class ActionedPetitionsDecorator |
|
3 FACETS = [ |
|
4 :awaiting_response, |
|
5 :with_response, |
|
6 :awaiting_debate, |
|
7 :with_debated_outcome, |
|
8 :not_debated |
|
9 ]
|
|
11 delegate :each, to: :actioned |
|
|
13 def empty? |
14 actioned.all? { |_, actioned| actioned[:count].zero? } |
|
15 end |
|
|
17 def [](state) |
18 actioned.fetch(state, 0) |
|
19 end |
|
|
21 def with_result |
22 {:with_response => actioned[:with_response], :with_debated_outcome => actioned[:with_debated_outcome]} |
|
23 end |
|
25 private
|
|
|
27 def actioned |
28 @actioned ||= generate_actioned |
|
29 end |
|
|
31 def generate_actioned(limit = 3) |
32 scope, facets = Petition.visible, Petition.facet_definitions |
|
34 FACETS.each_with_object({}) do |action, actioned| |
|
35 facet = scope.instance_exec(&facets[action])
|
|
36 actioned[action] = { count: facet.count, list: facet.limit(limit) } |
|
37 end |
|
38 end |
|
39 end |
|
|
41 def any_actioned_petitions? |
42 !actioned_petitions_decorator.empty?
|
|
43 end |
|
|
45 def actioned_petitions(&block) |
46 actioned = actioned_petitions_decorator
|
|
47 yield actioned unless actioned.empty? |
|
48 end |
|
|
50 def explanation_petitions(&block) |
51 yield actioned_petitions_decorator |
|
52 end |
|
|
54 def actioned_petitions_decorator |
55 @_actioned_petitions_decorator ||= ActionedPetitionsDecorator.new |
|
56 end |
|
57 private :actioned_petitions_decorator |
|
|
59 def no_petitions_yet? |
60 return @_no_petitions_yet if defined?(@_no_petitions_yet) |
|
61 @_no_petitions_yet = Petition.visible.empty? |
|
62 end |
|
|
64 def petition_count(key, count) |
65 t(:"#{key}.html", scope: :"petitions.counts", count: count, formatted_count: number_with_delimiter(count)) |
|
66 end |
|
|
68 def trending_petitions |
69 unless Site.disable_trending_petitions? |
|
70 petitions = fetch_trending_petitions
|
|
71 yield petitions unless petitions.empty? |
|
72 end |
|
73 end |
|
|
75 def fetch_trending_petitions |
76 Rails.cache.fetch(:trending_petitions, expires_in: 5.minutes) do |
|
77 signature_id = Signature.arel_table[:id] |
|
78 signature_count = signature_id.count.as("signature_count_in_period") |
|
79 Petition.trending.pluck(:id, :action, signature_count.to_sql) |
|
80 end |
|
81 end |
|
82 private :fetch_trending_petitions |
|
83end |