Module: SimpleSearch::ActiveRecord::ClassMethods

Defined in:
lib/simple_search/active_record.rb

Instance Method Summary collapse

Instance Method Details

#filtered_params(params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_search/active_record.rb', line 34

def filtered_params(params)
  where_params = {}
  other_params = {}
     params.each do |key,value|
    if ["group_by","page_by","page", "order_by"].include?(key)
      other_params[key]=value
    elsif value != ''
      matches = /(.*)_([a-z]+)$/.match(key)
      if matches!=nil && matches.length==3
        all, col, op = matches.to_a
        operands = SimpleSearch::OP_MAP.keys
        if operands.include?(op.to_sym)
          where_params[key]=value
        end
      end
    end
  end
  [other_params, where_params]
end

#simplesearch(params = {}) ⇒ Object Also known as: ss



21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_search/active_record.rb', line 21

def simplesearch(params={})

  arel = self.scoped unless self.is_a?(::ActiveRecord::Relation)
        other_params, where_params=filtered_params(params)  # validate params
  arel = with_ss_where(arel,where_params)
  arel = with_ss_group(arel,other_params['group_by'])
  arel = with_ss_order(arel,other_params['order_by'])
  arel = with_ss_limit_offset(arel,other_params)
  arel
end

#with_ss_group(arel, group_by) ⇒ Object



83
84
85
86
# File 'lib/simple_search/active_record.rb', line 83

def with_ss_group(arel,group_by)          
  arel= arel.group(group_by) if group_by
  arel
end

#with_ss_limit_offset(arel, params) ⇒ Object



93
94
95
96
97
98
# File 'lib/simple_search/active_record.rb', line 93

def with_ss_limit_offset(arel,params)          
  page_by  = params['page_by'].to_i
     page_num = params['page'] ?  params['page'].to_i : ( page_by>0 ? 1 : 0 )
  arel = arel.limit(page_by).offset( (page_num-1)*page_by ) if page_by>0 && page_num>0
  arel
end

#with_ss_order(arel, order_by) ⇒ Object



88
89
90
91
# File 'lib/simple_search/active_record.rb', line 88

def with_ss_order(arel,order_by)          
  arel= arel.order(order_by) if order_by
  arel
end

#with_ss_where(arel, params = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simple_search/active_record.rb', line 54

def with_ss_where(arel,params={})          
  params.each do |key,value|
          matches = /(.*)_([a-z]+)$/.match(key)
    all, col, op = matches.to_a
          where_col =  col =~ /\./ ? col : "#{arel.table_name}.#{col}" #if no table name, the add it
    where_value = case op.to_sym
      when :gt,:ge,:lt,:le,:eq,:ne  then value
      when :sw       then "#{value}%"  # LIKE
      when :ew       then "%#{value}"  # LIKE
      when :ct,:contains,:like then "%#{value}%" # LIKE
      when :nc,:notcontains,:notlike then "%#{value}%" # NOT LIKE
      when :is, :isnot, :it
        if    ["NULL","null"].include?(value)   then nil
        elsif ["TRUE","true"].include?(value)   then true
        elsif ["FALSE","false"].include?(value) then false
        end
    end
          if op.to_sym==:in
arel = arel.where("#{where_col}"=>value.split(','))
          elsif [:bt, :between].include? op.to_sym
      first,second = value.split('..')
arel = arel.where("#{where_col}"=>Range.new(first,second))
          else
arel = arel.where("#{where_col} #{SimpleSearch::OP_MAP[op.to_sym]} ?", where_value)
          end
  end
  arel
end