Class: Decidim::StatsPresenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/presenters/decidim/stats_presenter.rb

Overview

A general presenter to render statistics in participatory spaces.

Instance Method Summary collapse

Instance Method Details

#card_collectionObject



45
46
47
48
49
50
51
52
# File 'app/presenters/decidim/stats_presenter.rb', line 45

def card_collection
  card_stats = Decidim.stats.only([:followers_count, :votings_count])
                      .filter(priority: StatsRegistry::HIGH_PRIORITY)
                      .with_context(participatory_space)
                      .map { |stat_title, stat_number| [participatory_space_sym, stat_title, stat_number] }

  statistics(card_stats.group_by(&:first))
end

#collectionObject

Public: returns a collection of stats (Hash) for the Participatory Space Home.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/presenters/decidim/stats_presenter.rb', line 32

def collection
  highlighted_stats = participatory_space_participants_stats
  highlighted_stats.concat(participatory_space_followers_stats(priority: StatsRegistry::HIGH_PRIORITY))
  highlighted_stats.concat(component_stats(priority: StatsRegistry::HIGH_PRIORITY))
  highlighted_stats.concat(component_stats(priority: StatsRegistry::MEDIUM_PRIORITY))
  highlighted_stats.concat(comments_stats(participatory_space_sym))
  highlighted_stats = highlighted_stats.reject(&:empty?)
  highlighted_stats = highlighted_stats.reject { |_stat_manifest, _stat_title, stat_number| stat_number.zero? }
  grouped_highlighted_stats = highlighted_stats.group_by(&:first)

  statistics(grouped_highlighted_stats)
end

#comments_stats(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'app/presenters/decidim/stats_presenter.rb', line 6

def comments_stats(name)
  comments = Decidim.component_manifests.map do |component_manifest|
    component_manifest.stats.only([:comments_count])
                      .filter({ tag: :comments })
                      .with_context(published_components)
                      .map { |_name, value| value }.sum
  end
  comments_count = comments.inject(0, :+) { |sum, value| sum + value }
  [[name, :comments_count, comments_count]]
end

#statistics(grouped_stats) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/presenters/decidim/stats_presenter.rb', line 17

def statistics(grouped_stats)
  statistics = {}

  grouped_stats.each do |_manifest_name, stats|
    stats.each do |_space_manifest, component_manifest, count|
      next if count.zero?

      statistics[component_manifest] ||= 0
      statistics[component_manifest] += count
    end
  end
  statistics.map { |key, number| { stat_title: key, stat_number: number } }
end