Class: Sift::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/sift/sort.rb

Overview

Sort provides the same interface as a filter, but instead of applying a ‘where` to the collection it applies an `order`.

Constant Summary collapse

WHITELIST_TYPES =
[:int,
:decimal,
:string,
:text,
:date,
:time,
:datetime,
:scope].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param, type, internal_name = param, scope_params = []) ⇒ Sort

Returns a new instance of Sort.



17
18
19
20
21
22
23
# File 'lib/sift/sort.rb', line 17

def initialize(param, type, internal_name = param, scope_params = [])
  raise "unknown filter type: #{type}" unless WHITELIST_TYPES.include?(type)
  raise "scope params must be an array" unless scope_params.is_a?(Array)

  @parameter = Parameter.new(param, type, internal_name)
  @scope_params = scope_params
end

Instance Attribute Details

#parameterObject (readonly)

Returns the value of attribute parameter.



6
7
8
# File 'lib/sift/sort.rb', line 6

def parameter
  @parameter
end

#scope_paramsObject (readonly)

Returns the value of attribute scope_params.



6
7
8
# File 'lib/sift/sort.rb', line 6

def scope_params
  @scope_params
end

Instance Method Details

#always_active?Boolean

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


56
57
58
# File 'lib/sift/sort.rb', line 56

def always_active?
  true
end

#apply!(collection, value:, active_sorts_hash:, params: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument rubocop:disable Metrics/PerceivedComplexity



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sift/sort.rb', line 32

def apply!(collection, value:, active_sorts_hash:, params: {})
  if type == :scope
    if active_sorts_hash.keys.include?(param)
      collection.public_send(internal_name, *mapped_scope_params(active_sorts_hash[param], params))
    elsif default.present?
      # Stubbed because currently Sift::Sort does not respect default
      # default.call(collection)
      collection
    else
      collection
    end
  elsif type == :string || type == :text
    if active_sorts_hash.keys.include?(param)
      collection.order("LOWER(#{internal_name}) #{individual_sort_hash(active_sorts_hash)[internal_name]}")
    else
      collection
    end
  else
    collection.order(individual_sort_hash(active_sorts_hash))
  end
end

#defaultObject



25
26
27
28
# File 'lib/sift/sort.rb', line 25

def default
  # TODO: we can support defaults here later
  false
end

#paramObject



75
76
77
# File 'lib/sift/sort.rb', line 75

def param
  parameter.param
end

#typeObject



71
72
73
# File 'lib/sift/sort.rb', line 71

def type
  parameter.type
end

#validation(sort) ⇒ Object



64
65
66
67
68
69
# File 'lib/sift/sort.rb', line 64

def validation(sort)
  {
    inclusion: { in: SubsetComparator.new(sort) },
    allow_nil: true
  }
end

#validation_fieldObject



60
61
62
# File 'lib/sift/sort.rb', line 60

def validation_field
  :sort
end