Commit 06a34206bff0ca3dcf39ea44cceb6ce89801bfee

Authored by MoisesMachado
1 parent 5f0f6ea5

ActionItem514: refactored envronment finder to search using product_categorization


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2199 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/environment_finder.rb
... ... @@ -13,7 +13,6 @@ class EnvironmentFinder
13 13 end
14 14  
15 15 product_category = options.delete(:product_category)
16   - product_category_ids = product_category.map_traversal(&:id) if product_category
17 16  
18 17 date_range = options.delete(:date_range)
19 18  
... ... @@ -21,9 +20,9 @@ class EnvironmentFinder
21 20 if query.blank?
22 21 options = {:order => 'created_at desc, id desc'}.merge(options)
23 22 if product_category && asset == :products
24   - @environment.send(asset).paginate(:all, options.merge(:conditions => ['product_category_id in (?)', product_category_ids]))
  23 + @environment.send(asset).paginate(:all, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
25 24 elsif product_category && asset == :enterprises
26   - @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]))
  25 + @environment.send(asset).paginate(:all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id]))
27 26 else
28 27 if (asset == :events) && date_range
29 28 @environment.send(asset).paginate(:all, options.merge(:conditions => { :start_date => date_range}))
... ... @@ -35,9 +34,9 @@ class EnvironmentFinder
35 34 ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)}
36 35 if product_category && asset == :products
37 36 # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source
38   - @environment.send(asset).find_by_contents(query, ferret_options, options.merge({:conditions => 'product_category_id in (%s)' % product_category_ids.join(',') }))
  37 + @environment.send(asset).find_by_contents(query, ferret_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id }))
39 38 elsif product_category && asset == :enterprises
40   - @environment.send(asset).find_by_contents(query, ferret_options, options.merge(:include => 'products', :conditions => "products.product_category_id in (#{product_category_ids})"))
  39 + @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})"))
41 40 else
42 41 @environment.send(asset).find_by_contents(query, ferret_options, options)
43 42 end
... ...
app/models/product.rb
1 1 class Product < ActiveRecord::Base
2 2 belongs_to :enterprise
3 3 belongs_to :product_category
  4 + has_many :product_categorizations
4 5  
5 6 validates_presence_of :name
6 7 validates_uniqueness_of :name, :scope => :enterprise_id
... ...