Sha256: dbe6891be2eff041d37a902353a54b1ef26868ccc8c10ab7da7aea2af70a5735

Contents?: true

Size: 1.93 KB

Versions: 34

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Decidim
  module Assemblies
    # A presenter to render statistics in the homepage.
    class AssemblyStatsPresenter < Rectify::Presenter
      attribute :assembly, Decidim::Assembly
      include IconHelper

      # Public: Render a collection of primary stats.
      def highlighted
        highlighted_stats = feature_stats(priority: StatsRegistry::HIGH_PRIORITY)
        highlighted_stats = highlighted_stats.concat(feature_stats(priority: StatsRegistry::MEDIUM_PRIORITY))
        highlighted_stats = highlighted_stats.reject(&:empty?)
        highlighted_stats = highlighted_stats.reject { |_manifest, _name, data| data.zero? }
        grouped_highlighted_stats = highlighted_stats.group_by { |stats| stats.first.name }

        safe_join(
          grouped_highlighted_stats.map do |_manifest_name, stats|
            content_tag :div, class: "process_stats-item" do
              safe_join(
                stats.each_with_index.map do |stat, index|
                  render_stats_data(stat[0], stat[1], stat[2], index)
                end
              )
            end
          end
        )
      end

      private

      def feature_stats(conditions)
        Decidim.feature_manifests.map do |feature_manifest|
          feature_manifest.stats.filter(conditions).with_context(published_features).map { |name, data| [feature_manifest, name, data] }.flatten
        end
      end

      def render_stats_data(feature_manifest, name, data, index)
        safe_join([
                    index.zero? ? manifest_icon(feature_manifest) : " /&nbsp".html_safe,
                    content_tag(:span, "#{number_with_delimiter(data)} " + I18n.t(name, scope: "decidim.assemblies.statistics"),
                                class: "#{name} process_stats-text")
                  ])
      end

      def published_features
        @published_features ||= Feature.where(participatory_space: assembly).published
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
decidim-assemblies-0.10.1 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.10.0 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.9.3 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.9.2 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.9.1 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.9.0 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.8.4 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.8.3 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.8.2 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.8.1 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.8.0 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.7.4 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.7.3 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.7.2 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.7.1 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.7.0 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.6.8 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-0.6.8 decidim-assemblies/app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-assemblies-0.6.7 app/presenters/decidim/assemblies/assembly_stats_presenter.rb
decidim-0.6.7 decidim-assemblies/app/presenters/decidim/assemblies/assembly_stats_presenter.rb