Class: Filterameter::Filters::MatchesFilter

Inherits:
Object
  • Object
show all
Includes:
Errors, AttributeValidator
Defined in:
lib/filterameter/filters/matches_filter.rb

Overview

# Matches Filter

Class MatchesFilter uses arel’s ‘matches` to generate a LIKE query.

Instance Attribute Summary

Attributes included from Errors

#errors

Instance Method Summary collapse

Methods included from Errors

#valid?

Constructor Details

#initialize(attribute_name, options, &converter) ⇒ MatchesFilter

Returns a new instance of MatchesFilter.



12
13
14
15
16
17
18
# File 'lib/filterameter/filters/matches_filter.rb', line 12

def initialize(attribute_name, options, &converter)
  @attribute_name = attribute_name
  @prefix = options.match_anywhere? ? '%' : nil
  @suffix = options.match_anywhere? || options.match_from_start? ? '%' : nil
  @case_sensitive = options.case_sensitive?
  @converter = converter
end

Instance Method Details

#apply(query, value) ⇒ Object



20
21
22
23
24
# File 'lib/filterameter/filters/matches_filter.rb', line 20

def apply(query, value)
  value = @converter.call(value) if @converter
  arel = query.arel_table[@attribute_name].matches("#{@prefix}#{value}#{@suffix}", false, @case_sensitive)
  query.where(arel)
end