Sha256: 0f62d45690d2c69c03392d7a7ac8f55572aaaab62aaa1559b26cbb053032756c

Contents?: true

Size: 1.97 KB

Versions: 22

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  module Proposals
    # Common logic to ordering resources
    module Orderable
      extend ActiveSupport::Concern

      included do
        include Decidim::Orderable

        private

        # Available orders based on enabled settings
        def available_orders
          @available_orders ||= begin
            available_orders = %w(random recent)
            available_orders << "most_voted" if most_voted_order_available?
            available_orders << "most_endorsed" if current_settings.endorsements_enabled?
            available_orders << "most_commented" if component_settings.comments_enabled?
            available_orders << "most_followed" << "with_more_authors"
            available_orders
          end
        end

        def default_order
          if order_by_votes?
            detect_order("most_voted")
          else
            "random"
          end
        end

        def most_voted_order_available?
          current_settings.votes_enabled? && !current_settings.votes_hidden?
        end

        def order_by_votes?
          most_voted_order_available? && current_settings.votes_blocked?
        end

        def reorder(proposals)
          case order
          when "most_commented"
            proposals.left_joins(:comments).group(:id).order(Arel.sql("COUNT(decidim_comments_comments.id) DESC"))
          when "most_endorsed"
            proposals.order(endorsements_count: :desc)
          when "most_followed"
            proposals.left_joins(:follows).group(:id).order(Arel.sql("COUNT(decidim_follows.id) DESC"))
          when "most_voted"
            proposals.order(proposal_votes_count: :desc)
          when "random"
            proposals.order_randomly(random_seed)
          when "recent"
            proposals.order(published_at: :desc)
          when "with_more_authors"
            proposals.order(coauthorships_count: :desc)
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
decidim-proposals-0.25.2 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.1 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.0 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.0.rc4 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.0.rc3 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.0.rc2 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.25.0.rc1 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.3 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.6 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.2 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.5 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.1 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.0 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.0.rc2 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.4 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.24.0.rc1 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.3 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.2 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.1 app/controllers/concerns/decidim/proposals/orderable.rb
decidim-proposals-0.23.1.rc1 app/controllers/concerns/decidim/proposals/orderable.rb