Sha256: 52e061ed92a4c68bf3f5d60707f36d352298770a1bd81cb5fdcac5252a1ce6e3

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

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

      included do
        helper_method :order, :available_orders, :random_seed

        private

        # Gets how the proposals should be ordered based on the choice made by the user.
        def order
          @order ||= detect_order(params[:order]) || default_order
        end

        # Available orders based on enabled settings
        def available_orders
          @available_orders ||= begin
            available_orders = %w(random recent)
            available_orders << "most_contributed"
            available_orders
          end
        end

        def default_order
          detect_order("most_contributed")
        end

        # Returns: A random float number between -1 and 1 to be used as a random seed at the database.
        def random_seed
          @random_seed ||= (params[:random_seed] ? params[:random_seed].to_f : (rand * 2 - 1))
        end

        def detect_order(candidate)
          available_orders.detect { |order| order == candidate }
        end

        def reorder(drafts)
          case order
          when "random"
            drafts.order_randomly(random_seed)
          when "most_contributed"
            drafts.order(contributions_count: :desc)
          when "recent"
            drafts.order(created_at: :desc)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
decidim-proposals-0.18.1 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.17.2 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.18.0 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.17.1 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.16.1 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.17.0 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.16.0 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.15.2 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.15.1 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.15.0 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.14.4 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.14.3 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.14.2 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb
decidim-proposals-0.14.1 app/controllers/concerns/decidim/proposals/collaborative_orderable.rb