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) ⇒ MatchesFilter

Returns a new instance of MatchesFilter.



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

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

Instance Method Details

#apply(query, value) ⇒ Object



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

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