Module: EasySearchForm::SearchClassMethods

Defined in:
lib/easy_search_form.rb

Instance Method Summary collapse

Instance Method Details

#search(search) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easy_search_form.rb', line 17

def search(search)
  if search
    query = []
    values = []
    @search_options.each do |opt|
      condition_type = nil

      if opt.instance_of?(Hash)
        case opt[opt.keys.first]
        when :equals
          condition_type = "="
        else
          condition_type = "LIKE"
        end

        query << "#{opt.keys.first} #{condition_type} ?"
      else
        query << "#{opt} LIKE ?"
      end

      if condition_type.nil? or condition_type == "LIKE"
        values << "%#{search}%"
      else
        values << search
      end
    end

    conditions = [query.join(" OR ")]
    conditions += values

    where(conditions)
  else
    where(nil)
  end
end