Sha256: 5bc6d57868542a4fd2fa65d02f532bb5ee89405ca16150b1cf3ef97197afbff9

Contents?: true

Size: 1.37 KB

Versions: 25

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # A presenter to render statistics in the homepage.
    class InitiativeStatsPresenter < SimpleDelegator
      def initiative
        __getobj__.fetch(:initiative)
      end

      def comments_count
        Rails.cache.fetch(
          "initiative/#{initiative.id}/comments_count",
          expires_in: Decidim::Initiatives.stats_cache_expiration_time
        ) do
          initiative.comments_count
        end
      end

      def meetings_count
        Rails.cache.fetch(
          "initiative/#{initiative.id}/meetings_count",
          expires_in: Decidim::Initiatives.stats_cache_expiration_time
        ) do
          Decidim::Meetings::Meeting.where(component: meetings_component).count
        end
      end

      def assistants_count
        Rails.cache.fetch(
          "initiative/#{initiative.id}/assistants_count",
          expires_in: Decidim::Initiatives.stats_cache_expiration_time
        ) do
          result = 0
          Decidim::Meetings::Meeting.where(component: meetings_component).each do |meeting|
            result += meeting.attendees_count || 0
          end

          result
        end
      end

      private

      def meetings_component
        @meetings_component ||= Decidim::Component.find_by(participatory_space: initiative, manifest_name: "meetings")
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-initiatives-0.29.1 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.4 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.9 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.29.0 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.3 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.8 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.29.0.rc4 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.29.0.rc3 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.29.0.rc2 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.29.0.rc1 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.2 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.7 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.1 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.6 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.0 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.5 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.0.rc5 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.28.0.rc4 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.4 app/presenters/decidim/initiatives/initiative_stats_presenter.rb
decidim-initiatives-0.27.3 app/presenters/decidim/initiatives/initiative_stats_presenter.rb