Sha256: 9ec763dc19c5f2d64d6f1ecf6aff36359a6c3bd37ddb4ac18f507108a8cab16b

Contents?: true

Size: 845 Bytes

Versions: 14

Compression:

Stored size: 845 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # Common logic to ordering resources
  module Orderable
    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

      def detect_order(candidate)
        available_orders.detect { |order| order == candidate }
      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 ||= session.fetch(:random_seed, (rand * 2 - 1)).to_f
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.5 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.4 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.3 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.2 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.1 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.1.rc1 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.23.0 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.22.0 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.21.0 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.20.1 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.20.0 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.19.1 app/controllers/concerns/decidim/orderable.rb
decidim-core-0.19.0 app/controllers/concerns/decidim/orderable.rb