environment_finder.rb
2.32 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
51
52
53
54
55
56
57
58
59
60
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
options = {:page => 1, :per_page => options.delete(:limit)}.merge(options)
if query.blank?
options = {:order => 'created_at desc, id desc'}.merge(options)
if product_category && asset == :products
@environment.send(asset).paginate(:all, options.merge(:conditions => ['product_category_id in (?)', product_category_ids]))
elsif product_category && asset == :enterprises
@environment.send(asset).paginate(:all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :conditions => ['products.product_category_id in (?)', product_category_ids]))
else
@environment.send(asset).paginate(:all, options)
end
else
ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)}
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, ferret_options, 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}", ferret_options, options)
else
@environment.send(asset).find_by_contents(query, ferret_options, 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, query = '', options = {})
# because will_paginate needs a page
options = {:page => 1}.merge(options)
if query.blank?
# SLOW
find(asset, query, options).total_entries
else
find(asset, query, options).total_hits
end
end
end