| Class | EnvironmentFinder |
| In: |
app/models/environment_finder.rb
|
| Parent: | Object |
# File app/models/environment_finder.rb, line 7
7: def find(asset, query = nil, options={}, finder_method = 'paginate')
8: @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
9: if @region && options[:within]
10: options[:origin] = [@region.lat, @region.lng]
11: else
12: options.delete(:within)
13: end
14:
15: product_category = options.delete(:product_category)
16:
17: date_range = options.delete(:date_range)
18:
19: # FIXME this test is in more than one place
20: if finder_method == 'paginate'
21: options = {:page => 1, :per_page => options.delete(:limit)}.merge(options)
22: end
23:
24: if query.blank?
25: # FIXME this test is in more than one place
26: if finder_method == 'paginate'
27: options = {:order => "#{asset_table(asset)}.name"}.merge(options)
28: end
29: if product_category && asset == :products
30: @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
31: elsif product_category && asset == :enterprises
32: @environment.send(asset).send(finder_method, :all, options.merge( :order => 'profiles.name', :joins => 'inner join products on (products.enterprise_id = profiles.id) inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
33: else
34: if (asset == :events) && date_range
35: @environment.send(asset).send(finder_method, :all, options.merge(:conditions => { :start_date => date_range}))
36: else
37: @environment.send(asset).send(finder_method, :all, options)
38: end
39: end
40: else
41: ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)}
42: if product_category && asset == :products
43: # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source
44: @environment.send(asset).find_by_contents(query, ferret_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id }))
45: elsif product_category && asset == :enterprises
46: @environment.send(asset).find_by_contents(query, ferret_options, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :include => 'products', :conditions => "product_categorizations.category_id = (#{product_category.id})"))
47: else
48: @environment.send(asset).find_by_contents(query, ferret_options, options)
49: end
50: end
51: end
# File app/models/environment_finder.rb, line 57
57: def product_categories_count(asset, product_categories_ids, objects_ids=nil)
58: conditions = ['product_categorizations.category_id in (?)', product_categories_ids]
59:
60: if asset == :products
61: if objects_ids
62: conditions[0] += ' and product_categorizations.product_id in (?)'
63: conditions << objects_ids
64: end
65: ProductCategory.find(:all, :select => 'categories.id, count(*) as total', :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id)', :group => 'categories.id', :conditions => conditions )
66: elsif asset == :enterprises
67: if objects_ids
68: conditions[0] += ' and products.enterprise_id in (?)'
69: conditions << objects_ids
70: end
71: ProductCategory.find(
72: :all,
73: :select => 'categories.id, count(distinct products.enterprise_id) as total',
74: :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id)',
75: :group => 'categories.id',
76: :conditions => conditions
77: )
78: else
79: raise ArgumentError, 'only products and enterprises supported'
80: end.inject({}) do |results,pc|
81: results[pc.id]= pc.total.to_i
82: results
83: end
84:
85: end
# File app/models/environment_finder.rb, line 53
53: def recent(asset, limit = nil)
54: find(asset, nil, :limit => limit)
55: end
# File app/models/environment_finder.rb, line 89
89: def asset_class(asset)
90: asset.to_s.singularize.camelize.constantize
91: end