diff --git a/app/models/article.rb b/app/models/article.rb index a7434b3..de8d367 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -21,7 +21,7 @@ class Article < ActiveRecord::Base def add_category(c) if self.id - ArticleCategorization.create!(:category => c, :article => self) + ArticleCategorization.add_category_to_article(c, self) else pending_categorizations << c end @@ -37,7 +37,7 @@ class Article < ActiveRecord::Base after_create :create_pending_categorizations def create_pending_categorizations pending_categorizations.each do |item| - ArticleCategorization.create!(:category => item, :article => self) + ArticleCategorization.add_category_to_article(item, self) end pending_categorizations.clear end diff --git a/app/models/article_categorization.rb b/app/models/article_categorization.rb index 6fcbfd2..abdff5f 100644 --- a/app/models/article_categorization.rb +++ b/app/models/article_categorization.rb @@ -3,13 +3,12 @@ class ArticleCategorization < ActiveRecord::Base belongs_to :article belongs_to :category - after_create :associate_with_entire_hierarchy - def associate_with_entire_hierarchy - return if virtual + def self.add_category_to_article(category, article) + connection.execute("insert into articles_categories (category_id, article_id) values(#{category.id}, #{article.id})") c = category.parent - while !c.nil? && !self.class.find(:first, :conditions => {:article_id => article, :category_id => c}) - self.class.create!(:article => article, :category => c, :virtual => true) + while !c.nil? && !self.find(:first, :conditions => {:article_id => article, :category_id => c}) + connection.execute("insert into articles_categories (category_id, article_id, virtual) values(#{c.id}, #{article.id}, 1>0)") c = c.parent end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 29d6346..3b3db78 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -109,7 +109,7 @@ class Profile < ActiveRecord::Base def add_category(c) if self.id - ProfileCategorization.create!(:category => c, :profile => self) + ProfileCategorization.add_category_to_profile(c, self) else pending_categorizations << c end @@ -125,7 +125,7 @@ class Profile < ActiveRecord::Base after_create :create_pending_categorizations def create_pending_categorizations pending_categorizations.each do |item| - ProfileCategorization.create!(:category => item, :profile => self) + ProfileCategorization.add_category_to_profile(item, self) end pending_categorizations.clear end diff --git a/app/models/profile_categorization.rb b/app/models/profile_categorization.rb index ba33a92..5d3fa8f 100644 --- a/app/models/profile_categorization.rb +++ b/app/models/profile_categorization.rb @@ -3,13 +3,13 @@ class ProfileCategorization < ActiveRecord::Base belongs_to :profile belongs_to :category - after_create :associate_with_entire_hierarchy - def associate_with_entire_hierarchy - return if virtual + def self.add_category_to_profile(category, profile) + + connection.execute("insert into categories_profiles (category_id, profile_id) values(#{category.id}, #{profile.id})") c = category.parent - while !c.nil? && !self.class.find(:first, :conditions => {:profile_id => profile, :category_id => c}) - self.class.create!(:profile => profile, :category => c, :virtual => true) + while !c.nil? && !self.find(:first, :conditions => {:profile_id => profile, :category_id => c}) + connection.execute("insert into categories_profiles (category_id, profile_id, virtual) values(#{c.id}, #{profile.id}, 1>0)") c = c.parent end end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index d03099a..34f284a 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -484,7 +484,7 @@ class SearchControllerTest < Test::Unit::TestCase child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent) p = create_user('test_profile').person - p.categories << child + p.add_category child p.save! get :index, :category_path => ['parent-category'], :query => 'test_profile', :find_in => ['people'] @@ -563,10 +563,10 @@ class SearchControllerTest < Test::Unit::TestCase should 'display people with a given initial, under a specific category' do in_category_and_with_initial = create_user('fergunson').person - in_category_and_with_initial.categories << @category + in_category_and_with_initial.add_category @category in_category_but_without_initial = create_user('yanerson').person - in_category_but_without_initial.categories << @category + in_category_but_without_initial.add_category @category not_in_category_but_with_initial = create_user('fergy').person not_in_category_and_without_initial = create_user('xalanxalan').person @@ -580,8 +580,8 @@ class SearchControllerTest < Test::Unit::TestCase end should 'display communities with a given initial, under a specific category' do - c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default); c1.categories << @category - c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default); c2.categories << @category + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default); c1.add_category @category + c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default); c2.add_category @category c3 = Community.create!(:name => 'another beautiful community', :identifier => 'lalala', :environment => Environment.default); c4 = Community.create!(:name => 'damn beautiful community (another)', :identifier => 'lelele', :environment => Environment.default) @@ -595,8 +595,8 @@ class SearchControllerTest < Test::Unit::TestCase end should 'display enterprises with a given initial, under a specific category' do - ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1'); ent1.categories << @category - ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2'); ent1.categories << @category + ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1'); ent1.add_category @category + ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2'); ent1.add_category @category ent3 = Enterprise.create!(:name => 'aaaa1111', :identifier => 'teste1111') ent4 = Enterprise.create!(:name => 'ddddd', :identifier => 'teste2222') @@ -611,9 +611,9 @@ class SearchControllerTest < Test::Unit::TestCase should 'display articles with a given initial, under a specific category' do person = create_user('teste').person art1 = person.articles.build(:name => 'an article to be found'); art1.save! - art1.categories << @category + art1.add_category @category art2 = person.articles.build(:name => 'better article'); art2.save! - art2.categories << @category + art2.add_category @category art3 = person.articles.build(:name => 'another article to be found'); art3.save! art4 = person.articles.build(:name => 'damn article'); art4.save! diff --git a/test/unit/article_categorization_test.rb b/test/unit/article_categorization_test.rb index 0367e65..aeec46e 100644 --- a/test/unit/article_categorization_test.rb +++ b/test/unit/article_categorization_test.rb @@ -25,7 +25,7 @@ class ArticleCategorizationTest < Test::Unit::TestCase a = p.articles.create!(:name => 'test') assert_difference ArticleCategorization, :count, 2 do - ArticleCategorization.create!(:category => c2, :article => a) + ArticleCategorization.add_category_to_article(c2, a) end assert_equal 2, ArticleCategorization.find_all_by_article_id(a.id).size @@ -40,8 +40,8 @@ class ArticleCategorizationTest < Test::Unit::TestCase a = p.articles.create!(:name => 'test') assert_difference ArticleCategorization, :count, 3 do - ac = ArticleCategorization.create!(:category => c2, :article => a) - ac = ArticleCategorization.create!(:category => c3, :article => a) + ArticleCategorization.add_category_to_article(c2, a) + ArticleCategorization.add_category_to_article(c3, a) end end @@ -53,8 +53,8 @@ class ArticleCategorizationTest < Test::Unit::TestCase p = create_user('testuser').person a = p.articles.create!(:name => 'test') - ac = ArticleCategorization.create!(:category => c2, :article => a) - ac = ArticleCategorization.create!(:category => c3, :article => a) + ArticleCategorization.add_category_to_article(c2, a) + ArticleCategorization.add_category_to_article(c3, a) assert_difference ArticleCategorization, :count, -3 do ArticleCategorization.remove_all_for(a) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 35f2c50..f6a5269 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -175,14 +175,8 @@ class ArticleTest < Test::Unit::TestCase article = profile.articles.build(:name => 'withcategories') article.save! - assert_raise ActiveRecord::AssociationTypeMismatch do - article.categories << 1 - end - - assert_nothing_raised do - article.categories << c1 - article.categories << c2 - end + article.add_category c1 + article.add_category c2 assert_equivalent [c1,c2], article.categories(true) end diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index fcb9495..eff8d41 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -312,9 +312,9 @@ class CategoryTest < Test::Unit::TestCase should 'have people' do c = @env.categories.build(:name => 'my category'); c.save! p1 = create_user('testuser_1').person - p1.categories << c + p1.add_category c p2 = create_user('testuser_2').person - p2.categories << c + p2.add_category c assert_equal [p1, p2], c.people end diff --git a/test/unit/profile_categorization_test.rb b/test/unit/profile_categorization_test.rb index a59baf2..2827dc1 100644 --- a/test/unit/profile_categorization_test.rb +++ b/test/unit/profile_categorization_test.rb @@ -5,7 +5,7 @@ class ProfileCategorizationTest < ActiveSupport::TestCase should 'have profile and category' do person = create_user('test_user').person cat = Environment.default.categories.build(:name => 'a category'); cat.save! - person.categories << cat + person.add_category cat person.save! assert_includes person.categories, cat assert_includes cat.people, person @@ -19,7 +19,7 @@ class ProfileCategorizationTest < ActiveSupport::TestCase p = create_user('testuser').person assert_difference ProfileCategorization, :count, 2 do - ProfileCategorization.create!(:category => c2, :profile => p) + ProfileCategorization.add_category_to_profile(c2, p) end assert_equal 2, ProfileCategorization.find_all_by_profile_id(p.id).size @@ -33,8 +33,8 @@ class ProfileCategorizationTest < ActiveSupport::TestCase p = create_user('testuser').person assert_difference ProfileCategorization, :count, 3 do - ac = ProfileCategorization.create!(:category => c2, :profile => p) - ac = ProfileCategorization.create!(:category => c3, :profile => p) + ProfileCategorization.add_category_to_profile(c2, p) + ProfileCategorization.add_category_to_profile(c3, p) end end @@ -45,8 +45,8 @@ class ProfileCategorizationTest < ActiveSupport::TestCase p = create_user('testuser').person - ac = ProfileCategorization.create!(:category => c2, :profile => p) - ac = ProfileCategorization.create!(:category => c3, :profile => p) + ProfileCategorization.add_category_to_profile(c2, p) + ProfileCategorization.add_category_to_profile(c3, p) assert_difference ProfileCategorization, :count, -3 do ProfileCategorization.remove_all_for(p) diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 1cd265e..dd16276 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -390,7 +390,7 @@ class ProfileTest < Test::Unit::TestCase should 'have categories' do c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') cat = Environment.default.categories.build(:name => 'a category'); cat.save! - c.categories << cat + c.add_category cat c.save! assert_includes c.categories, cat end -- libgit2 0.21.2