environment_finder.rb
1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
class EnvironmentFinder
def initialize env
@environment = env
end
def find(asset, query = nil, options={})
@region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
if @region && options[:within]
options[:origin] = [@region.lat, @region.lng]
else
options.delete(:within)
end
product_category = options.delete(:product_category)
product_category_ids = product_category.map_traversal(&:id) if product_category
if query.blank?
if product_category && asset == :products
@environment.send(asset).find(:all, options.merge({:order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]}))
elsif product_category && asset == :enterprises
@environment.send(asset).find_by_contents("extra_data_for_index:#{product_category.name}", {}, options.merge( {:order => 'created_at desc, id desc'} ) )
else
@environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) )
end
else
if product_category && asset == :products
# SECURITY no risk of SQL injection, since product_category_ids comes from trusted source
@environment.send(asset).find_by_contents(query, {}, options.merge({:conditions => 'product_category_id in (%s)' % product_category_ids.join(',') }))
elsif product_category && asset == :enterprises
@environment.send(asset).find_by_contents(query + " +extra_data_for_index:#{product_category.name}", {}, options)
else
@environment.send(asset).find_by_contents(query, {}, options)
end
end
end
def recent(asset, limit = nil)
find(asset, nil, :limit => limit)
end
def find_by_initial(asset, initial)
@environment.send(asset).find_by_initial(initial)
end
def count(asset)
@environment.send(asset).count
end
end