Class: Queryko::Filters::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/queryko/filters/search.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#as, #column_name, #feature, #field, #query_object, #table_name

Instance Method Summary collapse

Methods inherited from Base

#call, #param_key_format

Constructor Details

#initialize(options = {}, feature) ⇒ Search

Returns a new instance of Search.



5
6
7
8
9
# File 'lib/queryko/filters/search.rb', line 5

def initialize(options = {}, feature)
  @cond = options.fetch(:cond) { :like }
  @token_format = options[:token_format] || '%token%'
  super(options, feature)
end

Instance Attribute Details

#condObject (readonly)

Returns the value of attribute cond.



4
5
6
# File 'lib/queryko/filters/search.rb', line 4

def cond
  @cond
end

#token_formatObject (readonly)

Returns the value of attribute token_format.



4
5
6
# File 'lib/queryko/filters/search.rb', line 4

def token_format
  @token_format
end

Instance Method Details

#format_query_params(token) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/queryko/filters/search.rb', line 18

def format_query_params(token)
  case cond.to_sym
  when :like
    ['LIKE', token_format.gsub('token', token)]
  when :eq
    ['=', token]
  when :neq
    ['IS NOT', token]
  end
end

#perform(collection, token, query_object = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/queryko/filters/search.rb', line 11

def perform(collection, token, query_object = nil)
  query_cond, query_token = format_query_params(token)
  table_property = "#{table_name}.#{column_name}"

  collection.where("#{table_property} #{query_cond} ?", query_token)
end