Commit fafc7edd0b4786f839e664623d6de4cd31d5cfcc

Authored by MoisesMachado
1 parent b71e5cb9

ActionItem514: change products categorization to sync with a join table for peformance


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2197 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/product.rb
... ... @@ -19,6 +19,15 @@ class Product < ActiveRecord::Base
19 19 p.enterprise.product_updated if p.enterprise
20 20 end
21 21  
  22 + after_save do |p|
  23 + if (p.product_category && !ProductCategorization.find(:first, :conditions => {:category_id => p.product_category.id, :product_id => p.id})) || (!p.product_category)
  24 + ProductCategorization.remove_all_for(p)
  25 + if p.product_category
  26 + ProductCategorization.add_category_to_product(p.product_category, p)
  27 + end
  28 + end
  29 + end
  30 +
22 31 acts_as_searchable :fields => [ :name, :description, :category_full_name ]
23 32  
24 33 xss_terminate :only => [ :name, :description ]
... ...
app/models/product_categorization.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class ProductCategorization < ActiveRecord::Base
  2 + belongs_to :product_category, :foreign_key => 'category_id'
  3 + belongs_to :product
  4 +
  5 + extend Categorization
  6 +
  7 + class << self
  8 + alias :add_category_to_product :add_category_to_object
  9 + def object_id_column
  10 + :product_id
  11 + end
  12 + end
  13 +end
... ...
db/migrate/044_create_product_categorizations.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +class CreateProductCategorizations < ActiveRecord::Migration
  2 + def self.up
  3 +
  4 + create_table :product_categorizations do |t|
  5 + t.integer :category_id
  6 + t.integer :product_id
  7 + t.boolean :virtual, :default => false
  8 +
  9 + t.timestamps
  10 + end
  11 +
  12 +# FIXME:uncomment after implementation
  13 +# Product.find(:all).each do |p|
  14 +# ProductCategorization.add_category_to_product(p.product_category, p)
  15 +# end
  16 +
  17 + end
  18 +
  19 + def self.down
  20 + drop_table :product_categorizations
  21 + end
  22 +end
... ...
db/migrate/044_update_regions_to_become_states_and_cities.rb
... ... @@ -1,11 +0,0 @@
1   -class UpdateRegionsToBecomeStatesAndCities < ActiveRecord::Migration
2   - def self.up
3   - execute "update categories set type = 'State' where parent_id in (select id from categories where type = 'Region' and parent_id in (select id from categories where type = 'Region' and name = 'Nacional'))"
4   -
5   - execute "update categories set type = 'City' where parent_id in (select id from categories where type = 'State')"
6   - end
7   -
8   - def self.down
9   - execute "update categories set type = 'Region' where type = 'State' or type = 'City'"
10   - end
11   -end
db/schema.rb
... ... @@ -71,8 +71,8 @@ ActiveRecord::Schema.define(:version =&gt; 45) do
71 71 t.boolean "virtual", :default => false
72 72 end
73 73  
74   - add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id"
75 74 add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id"
  75 + add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id"
76 76  
77 77 create_table "blocks", :force => true do |t|
78 78 t.string "title"
... ... @@ -162,6 +162,14 @@ ActiveRecord::Schema.define(:version =&gt; 45) do
162 162 t.integer "height"
163 163 end
164 164  
  165 + create_table "product_categorizations", :force => true do |t|
  166 + t.integer "category_id"
  167 + t.integer "product_id"
  168 + t.boolean "virtual", :default => false
  169 + t.datetime "created_at"
  170 + t.datetime "updated_at"
  171 + end
  172 +
165 173 create_table "products", :force => true do |t|
166 174 t.integer "enterprise_id"
167 175 t.integer "product_category_id"
... ... @@ -222,8 +230,8 @@ ActiveRecord::Schema.define(:version =&gt; 45) do
222 230 t.datetime "created_at"
223 231 end
224 232  
225   - add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
226 233 add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
  234 + add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
227 235  
228 236 create_table "tags", :force => true do |t|
229 237 t.string "name"
... ...
test/unit/product_categorization_test.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ProductCategorizationTest < ActiveSupport::TestCase
  4 + # Replace this with your real tests.
  5 + def test_truth
  6 + assert true
  7 + end
  8 +end
... ...
test/unit/product_test.rb
... ... @@ -122,4 +122,50 @@ class ProductTest &lt; Test::Unit::TestCase
122 122 assert_same result, product.url
123 123 end
124 124  
  125 + should 'categorize also with product categorization' do
  126 + cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
  127 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  128 + p = ent.products.create!(:name => 'test product')
  129 + p.product_category = cat
  130 + p.save!
  131 +
  132 + assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat})
  133 + end
  134 +
  135 + should 'categorize parent cateogries with product categorization' do
  136 + parent_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
  137 + child_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default, :parent => parent_cat)
  138 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  139 + p = ent.products.create!(:name => 'test product')
  140 + p.product_category = child_cat
  141 + p.save!
  142 +
  143 + assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => parent_cat})
  144 + assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => child_cat})
  145 + end
  146 +
  147 + should 'change product categorization when product category changes' do
  148 + cat1 = ProductCategory.create(:name => 'test cat 1', :environment => Environment.default)
  149 + cat2 = ProductCategory.create(:name => 'test cat 2', :environment => Environment.default)
  150 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  151 + p = ent.products.create!(:name => 'test product', :product_category => cat1)
  152 +
  153 + p.product_category = cat2
  154 + p.save!
  155 +
  156 + assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat2}), 'must include the new category'
  157 + assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat1}), 'must exclude the old category'
  158 + end
  159 +
  160 + should 'remove categorization when product category is removed' do
  161 + cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
  162 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  163 + p = ent.products.create!(:name => 'test product', :product_category => cat)
  164 +
  165 + p.product_category = nil
  166 + p.save!
  167 +
  168 + assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat})
  169 + end
  170 +
125 171 end
... ...