Sha256: 5e246d6c5eb9b9f01e6ea814407925818a9f2910ab3026c26650a2717bc5b16a

Contents?: true

Size: 1.41 KB

Versions: 58

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  module Budgets
    # Common logic to sorting 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 = []
            available_orders << "random" if voting_open? || !votes_are_visible?
            available_orders << "most_voted" if votes_are_visible?
            available_orders += %w(highest_cost lowest_cost)
            available_orders
          end
        end

        def default_order
          available_orders.first
        end

        def votes_are_visible?
          current_settings.show_votes?
        end

        def reorder(projects)
          case order
          when "highest_cost"
            projects.order(budget_amount: :desc)
          when "lowest_cost"
            projects.order(budget_amount: :asc)
          when "most_voted"
            if votes_are_visible?
              ids = projects.sort_by(&:confirmed_orders_count).map(&:id).reverse
              projects.ordered_ids(ids)
            else
              projects
            end
          when "random"
            projects.order_randomly(random_seed)
          else
            projects
          end
        end
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
decidim-budgets-0.29.1 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.4 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.27.9 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.29.0 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.3 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.27.8 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.29.0.rc4 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.29.0.rc3 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.29.0.rc2 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.29.0.rc1 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.2 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.27.7 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.1 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.27.6 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.26.10 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.26.9 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.0 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.27.5 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.0.rc5 app/controllers/concerns/decidim/budgets/orderable.rb
decidim-budgets-0.28.0.rc4 app/controllers/concerns/decidim/budgets/orderable.rb