Sha256: baf7bdafe01b3f3b5dfebaf295d0bac087a19b9f6a8de16f9b9c645b3d67bfac

Contents?: true

Size: 1.5 KB

Versions: 16

Compression:

Stored size: 1.5 KB

Contents

require 'coercible'
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/hash'

module Admino
  module Query
    class Sorting
      attr_reader :params
      attr_reader :config
      attr_reader :query_i18n_key

      def initialize(config, params, query_i18n_key = nil)
        @config = config
        @params = ActiveSupport::HashWithIndifferentAccess.new(params)
      end

      def augment_scope(scope)
        if active_scope
          scope.send(active_scope, ascending? ? :asc : :desc)
        else
          scope
        end
      end

      def is_scope_active?(scope)
        active_scope == scope.to_sym
      end

      def ascending?
        if params[:sort_order] == 'desc'
          false
        elsif params[:sort_order].blank? && active_scope == default_scope
          default_direction_is_ascending?
        else
          true
        end
      end

      def active_scope
        if param_value && scopes.include?(param_value.to_sym)
          param_value.to_sym
        elsif default_scope
          default_scope
        else
          nil
        end
      end

      def default_scope
        config.default_scope
      end

      def default_direction
        config.default_direction
      end

      def default_direction_is_ascending?
        default_direction != :desc
      end

      def param_value
        params.fetch(param_name, nil)
      end

      def param_name
        :sorting
      end

      def scopes
        config.scopes
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
admino-0.0.22 lib/admino/query/sorting.rb
admino-0.0.21 lib/admino/query/sorting.rb
admino-0.0.20 lib/admino/query/sorting.rb
admino-0.0.19 lib/admino/query/sorting.rb
admino-0.0.18 lib/admino/query/sorting.rb
admino-0.0.17 lib/admino/query/sorting.rb
admino-0.0.16 lib/admino/query/sorting.rb
admino-0.0.15 lib/admino/query/sorting.rb
admino-0.0.14 lib/admino/query/sorting.rb
admino-0.0.13 lib/admino/query/sorting.rb
admino-0.0.12 lib/admino/query/sorting.rb
admino-0.0.11 lib/admino/query/sorting.rb
admino-0.0.10 lib/admino/query/sorting.rb
admino-0.0.9 lib/admino/query/sorting.rb
admino-0.0.8 lib/admino/query/sorting.rb
admino-0.0.7 lib/admino/query/sorting.rb