Sha256: 523d359246a74367e5fb7b2cf102d14aa76c702ecb4317dd46a92cc2ee73a9b8

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

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

      included do
        helper_method :order, :available_orders, :random_seed

        private

        # Gets how the plans 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
          end
        end

        def default_order
          "random"
        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(plans)
          case order
          when "random"
            plans.order_randomly(random_seed)
          when "recent"
            plans.order(created_at: :desc)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-plans-0.18.2 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.18.1 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.18.0 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.17.0 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.9 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.8 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.7 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.6 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.5 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.4 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.3 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.2 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.1 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.16.0 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.15.1 app/controllers/concerns/decidim/plans/orderable.rb
decidim-plans-0.15.0 app/controllers/concerns/decidim/plans/orderable.rb