Module: SearchMe::Search

Defined in:
lib/search_me/search.rb

Instance Method Summary collapse

Instance Method Details

#advanced_search(search_terms) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/search_me/search.rb', line 80

def advanced_search(search_terms)
  search_terms = sanitize_params!(search_terms) 
  @joiner = :and

  hash = default_hash
  search_terms.each do |type, attributes|
    search_attributes_hash!(attributes.keys,type,hash)
  end
  @this_search_attributes = hash

  conditions = @this_search_attributes.keys.map { |type|
    case type
    when :simple
      join(@this_search_attributes[type].map { |attribute|
        term = search_terms[type][attribute]
        if advanced_search_block_for?(type, attribute)
          call_advanced_search_block_for(type, attribute, term)
        else
          simple_search_where_condition(attribute, term)           
        end
      })
    when :belongs_to
      self.advanced_search_reflection_group(type,search_terms) {
        |reflection,objs|
        "#{reflection.name}_id IN (#{object_ids(objs).join(',')})"
      }
    when :has_one
      self.advanced_search_reflection_group(type,search_terms) {
        |reflection,objs|
        f_key = "#{name_for(reflection.active_record.name)}_id"

        "id IN (#{object_ids(objs, f_key).join(',')})"
      }
    when :has_many
      self.advanced_search_reflection_group(type,search_terms) {
        |reflection,objs|
        f_key = reflection.options
          .fetch(:foreign_key) { "#{self.to_s.underscore}_id" }

        "id IN (#{object_ids(objs, f_key).join(',')})"
      }
    when :has_many_through
      warn 'WARNING: has_many_through in development'
      self.advanced_search_reflection_group(type,search_terms) {
        |reflection,objs|

        many_f_key = "#{name_for(reflection.name)}_id"
        self_f_key = "#{name_for(self)}_id"
        related = through_klass_for_reflection(reflection)
          .where(f_key => objs.ids)

        "id IN (#{object_ids(related, self_f_key).join(',')})"
      }
    end
  }
  self.where(join(conditions))
end

#advanced_search_block_for?(type, attribute) ⇒ Boolean

Returns:

  • (Boolean)


255
256
257
258
# File 'lib/search_me/search.rb', line 255

def advanced_search_block_for?(type, attribute)
  !advanced_search_blocks[type].blank? && 
    !advanced_search_blocks[type][attribute].blank?
end

#advanced_search_blocksObject



7
8
9
# File 'lib/search_me/search.rb', line 7

def advanced_search_blocks
  @advanced_search_blocks ||= default_hash(:never_a_key_like_this)
end

#advanced_search_reflection_group(type, search_terms, &block) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/search_me/search.rb', line 208

def advanced_search_reflection_group(type, search_terms, &block)
  map_reflection_group(type, block) do |attributes,klass,reflection|
    attributes.map { |attribute|
      name = reflection.name
      term = search_terms[name][attribute]
      if advanced_search_block_for?(name, attribute)
        call_advanced_search_block_for(name, attribute, term)
      else
        klass.simple_search_where_condition(attribute, term)
      end
    }
  end
end

#alias_advanced_search(attribute, options = {}, &block) ⇒ Object



30
31
32
33
# File 'lib/search_me/search.rb', line 30

def alias_advanced_search(attribute, options = {}, &block)
  type = options.fetch(:type) { :simple }
  advanced_search_blocks[type][attribute] = block
end

#attr_search(*attributes) ⇒ Object

assuming the last attribute could be a hash



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

def attr_search(*attributes)  
  options       = attributes.last.is_a?(Hash) ? attributes.pop : {}
  type          = (options.fetch(:type) { :simple }).to_sym
  accepted_keys = [:simple] + self.reflections.keys.map(&:to_sym)
  
  unless accepted_keys.include?(type.to_sym)
    raise ArgumentError, 'incorect type given'
  end

  search_attributes_hash!(attributes, type, search_attributes)
end

#call_advanced_search_block_for(type, attribute, term) ⇒ Object



260
261
262
# File 'lib/search_me/search.rb', line 260

def call_advanced_search_block_for(type, attribute, term)
  advanced_search_blocks[type][attribute].call(term)
end

#constant_for(name) ⇒ Object



287
288
289
# File 'lib/search_me/search.rb', line 287

def constant_for(name)
  name.to_s.camelize.singularize.constantize
end

#default_hash(flat_key = :simple) ⇒ Object



11
12
13
14
15
# File 'lib/search_me/search.rb', line 11

def default_hash(flat_key = :simple)
  Hash.new { |h,k| 
    h[k] = (k == flat_key ? [] : Hash.new { |nh,nk| nh[nk] = [] })
  }
end

#indifferent_reflectionsObject



301
302
303
# File 'lib/search_me/search.rb', line 301

def indifferent_reflections
  ActiveSupport::HashWithIndifferentAccess.new(self.reflections)
end

#join(array) ⇒ Object



62
63
64
# File 'lib/search_me/search.rb', line 62

def join(array)
  array.delete_if { |condition| condition.blank? }.join(joiner)
end

#joinerObject



75
76
77
78
# File 'lib/search_me/search.rb', line 75

def joiner 
  @joiner ||= :or
  " #{@joiner.upcase} "
end

#klass_for_reflection(reflection) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/search_me/search.rb', line 272

def klass_for_reflection(reflection)
  if name = reflection.options[:class_name]
    constant_for(name)
  else
    constant_for(reflection.name)
  end
end

#map_reflection_group(type, outer_block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/search_me/search.rb', line 181

def map_reflection_group(type, outer_block)
  cond = @this_search_attributes[type].map {|reflection, attributes|
    reflection = indifferent_reflections[reflection]
    klass      = klass_for_reflection(reflection)

    reflection_condition = join(yield(attributes,klass,reflection))
      
    search_result = klass.where(reflection_condition)

    outer_block.call(reflection, search_result)
  }
  join(cond)
end

#name_for(constant, options = {}) ⇒ Object



280
281
282
283
284
285
# File 'lib/search_me/search.rb', line 280

def name_for(constant, options = {})
  plural = options.fetch(:plural) { false }
  name   = constant.to_s.underscore
  name   = name.singularize unless plural
  name
end

#object_ids(objects, column = nil) ⇒ Object



264
265
266
# File 'lib/search_me/search.rb', line 264

def object_ids(objects, column = nil)
  (column ? objects.map(&column.to_sym) : objects.ids) << -5318008 
end

#reflection_search(term) ⇒ Object



177
178
179
# File 'lib/search_me/search.rb', line 177

def reflection_search(term)
  self.where(reflection_search_condition(term))
end

#reflection_search_condition(term) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/search_me/search.rb', line 138

def reflection_search_condition(term)
  macro_groups = search_attributes
  @this_search_attributes = macro_groups

  condition = macro_groups.keys.map { |type|
    case type
    when :belongs_to
      self.search_reflection_group(type, term) { |reflection, objs|
        "#{reflection.name}_id IN (#{object_ids(objs).join(',')})"
      }
    when :has_many
      self.search_reflection_group(type, term) { |reflection, objs|
        f_key = reflection.options
          .fetch(:foreign_key) { "#{self.to_s.underscore}_id" }

        "id IN (#{object_ids(objs, f_key).join(',')})"
      }
    when :has_one
      self.search_reflection_group(type, term) { |reflection, objs|
        f_key = "#{name_for(reflection.active_record.name)}_id"

        "id IN (#{object_ids(objs, f_key).join(',')})"
      }
    when :has_many_through
      warn 'WARNING: has_many_through in development'
      self.search_reflection_group(type, term) { |reflection,objs|
        f_key = "#{name_for(reflection.name)}_id"
        self_f_key = "#{name_for(self)}_id"
        related = through_klass_for_reflection(reflection)
          .where(f_key => objs.ids)

        "id IN (#{object_ids(related, self_f_key).join(',')})"
      }
    end
  }
  join(condition)
end

#sanitize_params!(params) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/search_me/search.rb', line 291

def sanitize_params!(params)
  params.to_hash.symbolize_keys!.each { |k,v| 
    if v.is_a?(Hash)
      params[k] = v = v.to_hash
      sanitize_params!(v) and v.symbolize_keys!
    end
    params.delete(k) if v.blank? && !(v == false)
  }
end

#search(term) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/search_me/search.rb', line 66

def search(term)
  @joiner = :or
  condition = join([
    simple_search_condition(term), 
    reflection_search_condition(term)
  ])
  self.where(condition)
end

#search_attributesObject



3
4
5
# File 'lib/search_me/search.rb', line 3

def search_attributes 
  @search_attributes ||= default_hash
end

#search_attributes_hash!(attributes, type, hash = default_hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/search_me/search.rb', line 35

def search_attributes_hash!(attributes, type, hash = default_hash)
  if type == :simple
    hash[:simple] += attributes
    hash[:simple] = hash[:simple].uniq
  else
    reflection  = indifferent_reflections[type]
    macro       = reflection.macro
    klass       = klass_for_reflection(reflection)

    if macro == :has_many
      macro = :has_many_through if reflection.options[:through]
    end

    hash[macro][type] += attributes
    hash[macro][type] = hash[macro][type].uniq

    unless klass.kind_of?(SearchMe::Search)
      klass.extend(SearchMe::Search)
    end
  end
  hash = sanitize_params!(hash)
end

#search_me(attribute, term) ⇒ Object



58
59
60
# File 'lib/search_me/search.rb', line 58

def search_me(attribute, term)
  self.where(simple_search_where_condition(attribute, term))
end

#search_reflection_group(type, term, &block) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/search_me/search.rb', line 195

def search_reflection_group(type, term, &block)
  map_reflection_group(type, block) do |attributes,klass,reflection|
    attributes.map { |attribute|
      name = reflection.name
      if advanced_search_block_for?(name, attribute)
        call_advanced_search_block_for(name, attribute, term)
      else
        klass.simple_search_where_condition(attribute, term)
      end
    }
  end
end

#simple_search(term) ⇒ Object



229
230
231
# File 'lib/search_me/search.rb', line 229

def simple_search(term)
  self.where(simple_search_condition(term))
end

#simple_search_condition(term) ⇒ Object



222
223
224
225
226
227
# File 'lib/search_me/search.rb', line 222

def simple_search_condition(term)
  condition = search_attributes[:simple].map { |attribute|
    simple_search_where_condition(attribute, term)
  }
  join(condition)
end

#simple_search_where_condition(attribute, term) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/search_me/search.rb', line 233

def simple_search_where_condition(attribute, term)
  table_column = "#{self.table_name}.#{attribute}"
  column = self.columns.find { |col| col.name == attribute.to_s }

  case column.type
  when :string, :integer, :text, :float, :decimal
    "CAST(#{table_column} AS CHAR) LIKE '%#{term}%'"
  when :boolean
    term = {
      true => "= 't'", false => "= 'f'", nil => 'IS NULL',
      1 => "= 't'", 0 => "= 'f'", '1' => "= 't'", '0' => "= 'f'"
    }.fetch(term) {
      good_args = term.keys.map(:inspect).join(',')
      error = "boolean column term must be #{good_args}"
      raise ArgumentError, error
    } 
    "#{table_column} #{term}"
  else
    warn "#{column.type} type is not supported by SearchMe::Search"
  end
end

#through_klass_for_reflection(reflection) ⇒ Object



268
269
270
# File 'lib/search_me/search.rb', line 268

def through_klass_for_reflection(reflection)
  constant_for(reflection.options[:through])
end