Sha256: b3cf3bba30b1862bd2def412e8e01a43cf3024a160ef2ee2ff5759c96afd89e5

Contents?: true

Size: 1.16 KB

Versions: 52

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Decidim
  module Accountability
    # 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

      delegate :count, to: :proposals, prefix: true

      def votes_count
        return 0 unless proposals

        proposals.sum { |proposal| proposal.votes.size }
      end

      def comments_count
        proposals.sum(:comments_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

      delegate :count, to: :meetings, prefix: true

      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

52 entries across 52 versions & 1 rubygems

Version Path
decidim-accountability-0.28.4 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.9 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.3 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.8 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.2 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.7 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.6 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.26.10 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.26.9 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.5 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.0.rc5 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.28.0.rc4 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.26.8 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.4 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.3 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.26.7 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.26.5 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.27.2 app/services/decidim/accountability/result_stats_calculator.rb