Class: Admino::Query::Sorting
- Inherits:
-
Object
- Object
- Admino::Query::Sorting
- Defined in:
- lib/admino/query/sorting.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #active_scope ⇒ Object
- #ascending? ⇒ Boolean
- #augment_scope(scope) ⇒ Object
- #available_scopes ⇒ Object
- #default_direction ⇒ Object
- #default_direction_is_ascending? ⇒ Boolean
- #default_scope ⇒ Object
-
#initialize(config, params) ⇒ Sorting
constructor
A new instance of Sorting.
- #is_scope_active?(scope) ⇒ Boolean
- #param_name ⇒ Object
- #param_value ⇒ Object
- #scope_name ⇒ Object
Constructor Details
#initialize(config, params) ⇒ Sorting
Returns a new instance of Sorting.
11 12 13 14 |
# File 'lib/admino/query/sorting.rb', line 11 def initialize(config, params) @config = config @params = ActiveSupport::HashWithIndifferentAccess.new(params) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/admino/query/sorting.rb', line 9 def config @config end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/admino/query/sorting.rb', line 8 def params @params end |
Instance Method Details
#active_scope ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/admino/query/sorting.rb', line 38 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 |
#ascending? ⇒ Boolean
28 29 30 31 32 33 34 35 36 |
# File 'lib/admino/query/sorting.rb', line 28 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 |
#augment_scope(scope) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/admino/query/sorting.rb', line 16 def augment_scope(scope) if active_scope scope.send(active_scope, ascending? ? :asc : :desc) else scope end end |
#available_scopes ⇒ Object
68 69 70 |
# File 'lib/admino/query/sorting.rb', line 68 def available_scopes config.scopes end |
#default_direction ⇒ Object
52 53 54 |
# File 'lib/admino/query/sorting.rb', line 52 def default_direction config.default_direction end |
#default_direction_is_ascending? ⇒ Boolean
56 57 58 |
# File 'lib/admino/query/sorting.rb', line 56 def default_direction_is_ascending? default_direction != :desc end |
#default_scope ⇒ Object
48 49 50 |
# File 'lib/admino/query/sorting.rb', line 48 def default_scope config.default_scope end |
#is_scope_active?(scope) ⇒ Boolean
24 25 26 |
# File 'lib/admino/query/sorting.rb', line 24 def is_scope_active?(scope) active_scope == scope end |
#param_name ⇒ Object
64 65 66 |
# File 'lib/admino/query/sorting.rb', line 64 def param_name :sorting end |
#param_value ⇒ Object
60 61 62 |
# File 'lib/admino/query/sorting.rb', line 60 def param_value params.fetch(param_name, nil) end |
#scope_name ⇒ Object
72 73 74 |
# File 'lib/admino/query/sorting.rb', line 72 def scope_name config.name end |