class Sift::Filter
Filter
describes the way a parameter maps to a database column and the type information helpful for validating input.
Attributes
custom_validate[R]
default[R]
parameter[R]
scope_params[R]
Public Class Methods
new(param, type, internal_name, default, custom_validate = nil, scope_params = [], tap = ->(value, _params) { value }
click to toggle source
# File lib/sift/filter.rb, line 7 def initialize(param, type, internal_name, default, custom_validate = nil, scope_params = [], tap = ->(value, _params) { value }) @parameter = Parameter.new(param, type, internal_name) @default = default @custom_validate = custom_validate @scope_params = scope_params @tap = tap raise ArgumentError, "scope_params must be an array of symbols" unless valid_scope_params?(scope_params) raise "unknown filter type: #{type}" unless type_validator.valid_type? end
Public Instance Methods
always_active?()
click to toggle source
rubocop:enable Lint/UnusedMethodArgument
# File lib/sift/filter.rb, line 35 def always_active? false end
apply!(collection, value:, active_sorts_hash:, params: {})
click to toggle source
rubocop:disable Lint/UnusedMethodArgument
# File lib/sift/filter.rb, line 22 def apply!(collection, value:, active_sorts_hash:, params: {}) if not_processable?(value) collection elsif should_apply_default?(value) default.call(collection) else parameterized_values = parameterize(value) processed_values = @tap.present? ? @tap.call(parameterized_values, params) : parameterized_values handler.call(collection, processed_values, params, scope_params) end end
param()
click to toggle source
# File lib/sift/filter.rb, line 51 def param parameter.param end
type()
click to toggle source
# File lib/sift/filter.rb, line 47 def type parameter.type end
type_validator()
click to toggle source
# File lib/sift/filter.rb, line 43 def type_validator @type_validator ||= Sift::TypeValidator.new(param, type) end
validation(_sort)
click to toggle source
# File lib/sift/filter.rb, line 17 def validation(_sort) type_validator.validate end
validation_field()
click to toggle source
# File lib/sift/filter.rb, line 39 def validation_field parameter.param end
Private Instance Methods
handler()
click to toggle source
# File lib/sift/filter.rb, line 79 def handler parameter.handler end
mapped_scope_params(params)
click to toggle source
# File lib/sift/filter.rb, line 69 def mapped_scope_params(params) scope_params.each_with_object({}) do |scope_param, hash| hash[scope_param] = params.fetch(scope_param) end end
not_processable?(value)
click to toggle source
# File lib/sift/filter.rb, line 61 def not_processable?(value) value.nil? && default.nil? end
parameterize(value)
click to toggle source
# File lib/sift/filter.rb, line 57 def parameterize(value) ValueParser.new(value: value, type: parameter.type, options: parameter.parse_options).parse end
should_apply_default?(value)
click to toggle source
# File lib/sift/filter.rb, line 65 def should_apply_default?(value) value.nil? && !default.nil? end
supports_ranges?()
click to toggle source
# File lib/sift/filter.rb, line 83 def supports_ranges? parameter.supports_ranges? end
valid_scope_params?(scope_params)
click to toggle source
# File lib/sift/filter.rb, line 75 def valid_scope_params?(scope_params) scope_params.is_a?(Array) && scope_params.all? { |symbol| symbol.is_a?(Symbol) } end