Sha256: 488c40df4bbaef1badd03ece12aa55efeb77633bb42ba8e117c10c5259320c34

Contents?: true

Size: 1.19 KB

Versions: 21

Compression:

Stored size: 1.19 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
        Decidim::Comments::Comment.where(commentable: proposals.to_a).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

21 entries across 21 versions & 1 rubygems

Version Path
decidim-accountability-0.18.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.17.2 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.18.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.17.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.16.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.17.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.16.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.15.2 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.15.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.15.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.14.4 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.14.3 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.14.2 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.14.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.13.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.12.2 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.13.0 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.12.1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.13.0.pre1 app/services/decidim/accountability/result_stats_calculator.rb
decidim-accountability-0.12.0 app/services/decidim/accountability/result_stats_calculator.rb