Sha256: 4d5fa44a8bf93bc99bf494c4adfd6d18b3b97946f6d90818411c715659e8e4e5

Contents?: true

Size: 1.2 KB

Versions: 40

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Decidim
  module Results
    # This class handles statistics of results. Needs a `result` in
    # order to find the stats.
    class ResultStatsCalculator
      # Public: Initializes the service.
      # result - The result from which to calculate the stats.
      def initialize(result)
        @result = result
      end

      def proposals_count
        proposals.count
      end

      def votes_count
        return 0 unless proposals
        proposals.sum { |proposal| proposal.votes.size }
      end

      def comments_count
        Decidim::Comments::Comment.where(root_commentable: proposals).count
      end

      def attendees_count
        meetings.where("attendees_count > 0").sum(:attendees_count)
      end

      def contributions_count
        meetings.where("contributions_count > 0").sum(:contributions_count)
      end

      def meetings_count
        meetings.count
      end

      private

      attr_reader :result

      def proposals
        @proposals ||= result.linked_resources(:proposals, "included_proposals")
      end

      def meetings
        @meetings ||= result.linked_resources(:meetings, "meetings_through_proposals")
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 2 rubygems

Version Path
decidim-results-0.6.8 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.8 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.7 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.7 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.6 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.6 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.5 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.5 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.4 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.4 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.3 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.3 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.2 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.2 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.1 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.1 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.6.0 app/services/decidim/results/result_stats_calculator.rb
decidim-0.6.0 decidim-results/app/services/decidim/results/result_stats_calculator.rb
decidim-results-0.5.1 app/services/decidim/results/result_stats_calculator.rb
decidim-0.5.1 decidim-results/app/services/decidim/results/result_stats_calculator.rb