class Sift::Sort
Sort
provides the same interface as a filter, but instead of applying a `where` to the collection it applies an `order`.
Constants
- WHITELIST_TYPES
Attributes
parameter[R]
scope_params[R]
Public Class Methods
new(param, type, internal_name = param, scope_params = [])
click to toggle source
# 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
Public Instance Methods
always_active?()
click to toggle source
rubocop:enable Metrics/PerceivedComplexity rubocop:enable Lint/UnusedMethodArgument
# File lib/sift/sort.rb, line 56 def always_active? true end
apply!(collection, value:, active_sorts_hash:, params: {})
click to toggle source
rubocop:disable Lint/UnusedMethodArgument rubocop:disable Metrics/PerceivedComplexity
# 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
default()
click to toggle source
# File lib/sift/sort.rb, line 25 def default # TODO: we can support defaults here later false end
param()
click to toggle source
# File lib/sift/sort.rb, line 75 def param parameter.param end
type()
click to toggle source
# File lib/sift/sort.rb, line 71 def type parameter.type end
validation(sort)
click to toggle source
# File lib/sift/sort.rb, line 64 def validation(sort) { inclusion: { in: SubsetComparator.new(sort) }, allow_nil: true } end
validation_field()
click to toggle source
# File lib/sift/sort.rb, line 60 def validation_field :sort end
Private Instance Methods
individual_sort_hash(active_sorts_hash)
click to toggle source
# File lib/sift/sort.rb, line 95 def individual_sort_hash(active_sorts_hash) if active_sorts_hash.include?(param) { internal_name => active_sorts_hash[param] } else {} end end
internal_name()
click to toggle source
# File lib/sift/sort.rb, line 103 def internal_name parameter.internal_name end
mapped_scope_params(direction, params)
click to toggle source
# File lib/sift/sort.rb, line 81 def mapped_scope_params(direction, params) scope_params.map do |scope_param| if scope_param == :direction direction elsif scope_param.is_a?(Proc) scope_param.call elsif params.include?(scope_param) params[scope_param] else scope_param end end end