class Brita::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 = [])
click to toggle source
# File lib/brita/filter.rb, line 7 def initialize(param, type, internal_name, default, custom_validate = nil, scope_params = []) @parameter = Parameter.new(param, type, internal_name) @default = default @custom_validate = custom_validate @scope_params = scope_params 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/brita/filter.rb, line 32 def always_active? false end
apply!(collection, value:, active_sorts_hash:, params: {})
click to toggle source
rubocop:disable Lint/UnusedMethodArgument
# File lib/brita/filter.rb, line 21 def apply!(collection, value:, active_sorts_hash:, params: {}) if not_processable?(value) collection elsif should_apply_default?(value) default.call(collection) else handler.call(collection, parameterize(value), params, scope_params) end end
param()
click to toggle source
# File lib/brita/filter.rb, line 48 def param parameter.param end
type()
click to toggle source
# File lib/brita/filter.rb, line 44 def type parameter.type end
type_validator()
click to toggle source
# File lib/brita/filter.rb, line 40 def type_validator @type_validator ||= Brita::TypeValidator.new(param, type) end
validation(_sort)
click to toggle source
# File lib/brita/filter.rb, line 16 def validation(_sort) type_validator.validate end
validation_field()
click to toggle source
# File lib/brita/filter.rb, line 36 def validation_field parameter.param end
Private Instance Methods
handler()
click to toggle source
# File lib/brita/filter.rb, line 76 def handler parameter.handler end
mapped_scope_params(params)
click to toggle source
# File lib/brita/filter.rb, line 66 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/brita/filter.rb, line 58 def not_processable?(value) value.nil? && default.nil? end
parameterize(value)
click to toggle source
# File lib/brita/filter.rb, line 54 def parameterize(value) ValueParser.new(value: value, options: parameter.parse_options).parse end
should_apply_default?(value)
click to toggle source
# File lib/brita/filter.rb, line 62 def should_apply_default?(value) value.nil? && !default.nil? end
supports_ranges?()
click to toggle source
# File lib/brita/filter.rb, line 80 def supports_ranges? parameter.supports_ranges? end
valid_scope_params?(scope_params)
click to toggle source
# File lib/brita/filter.rb, line 72 def valid_scope_params?(scope_params) scope_params.is_a?(Array) && scope_params.all? { |symbol| symbol.is_a?(Symbol) } end