Sha256: 50686a86df90019e6be46a38c6c0a7c28b44feaef248c3d818ad83daa387327d

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 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

      def initialize(config, params)
        @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
      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 && available_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_is_ascending?
        config.default_direction != :desc
      end

      def param_value
        params.fetch(param_name, nil)
      end

      def param_name
        :sorting
      end

      def available_scopes
        config.scopes
      end

      def scope_name
        config.name
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
admino-0.0.2 lib/admino/query/sorting.rb