Commit beb338bc1d67f4fe2dcfd00eeec50c657c5c3f65

Authored by AntonioTerceiro
1 parent a4aab71d

ActionItem466: making the proper fixes for PostgreSQL, which is not as

forgiving as SQLite



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2076 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article.rb
@@ -21,7 +21,7 @@ class Article < ActiveRecord::Base @@ -21,7 +21,7 @@ class Article < ActiveRecord::Base
21 21
22 def add_category(c) 22 def add_category(c)
23 if self.id 23 if self.id
24 - ArticleCategorization.create!(:category => c, :article => self) 24 + ArticleCategorization.add_category_to_article(c, self)
25 else 25 else
26 pending_categorizations << c 26 pending_categorizations << c
27 end 27 end
@@ -37,7 +37,7 @@ class Article &lt; ActiveRecord::Base @@ -37,7 +37,7 @@ class Article &lt; ActiveRecord::Base
37 after_create :create_pending_categorizations 37 after_create :create_pending_categorizations
38 def create_pending_categorizations 38 def create_pending_categorizations
39 pending_categorizations.each do |item| 39 pending_categorizations.each do |item|
40 - ArticleCategorization.create!(:category => item, :article => self) 40 + ArticleCategorization.add_category_to_article(item, self)
41 end 41 end
42 pending_categorizations.clear 42 pending_categorizations.clear
43 end 43 end
app/models/article_categorization.rb
@@ -3,13 +3,12 @@ class ArticleCategorization &lt; ActiveRecord::Base @@ -3,13 +3,12 @@ class ArticleCategorization &lt; ActiveRecord::Base
3 belongs_to :article 3 belongs_to :article
4 belongs_to :category 4 belongs_to :category
5 5
6 - after_create :associate_with_entire_hierarchy  
7 - def associate_with_entire_hierarchy  
8 - return if virtual 6 + def self.add_category_to_article(category, article)
  7 + connection.execute("insert into articles_categories (category_id, article_id) values(#{category.id}, #{article.id})")
9 8
10 c = category.parent 9 c = category.parent
11 - while !c.nil? && !self.class.find(:first, :conditions => {:article_id => article, :category_id => c})  
12 - self.class.create!(:article => article, :category => c, :virtual => true) 10 + while !c.nil? && !self.find(:first, :conditions => {:article_id => article, :category_id => c})
  11 + connection.execute("insert into articles_categories (category_id, article_id, virtual) values(#{c.id}, #{article.id}, 1>0)")
13 c = c.parent 12 c = c.parent
14 end 13 end
15 end 14 end
app/models/profile.rb
@@ -109,7 +109,7 @@ class Profile &lt; ActiveRecord::Base @@ -109,7 +109,7 @@ class Profile &lt; ActiveRecord::Base
109 109
110 def add_category(c) 110 def add_category(c)
111 if self.id 111 if self.id
112 - ProfileCategorization.create!(:category => c, :profile => self) 112 + ProfileCategorization.add_category_to_profile(c, self)
113 else 113 else
114 pending_categorizations << c 114 pending_categorizations << c
115 end 115 end
@@ -125,7 +125,7 @@ class Profile &lt; ActiveRecord::Base @@ -125,7 +125,7 @@ class Profile &lt; ActiveRecord::Base
125 after_create :create_pending_categorizations 125 after_create :create_pending_categorizations
126 def create_pending_categorizations 126 def create_pending_categorizations
127 pending_categorizations.each do |item| 127 pending_categorizations.each do |item|
128 - ProfileCategorization.create!(:category => item, :profile => self) 128 + ProfileCategorization.add_category_to_profile(item, self)
129 end 129 end
130 pending_categorizations.clear 130 pending_categorizations.clear
131 end 131 end
app/models/profile_categorization.rb
@@ -3,13 +3,13 @@ class ProfileCategorization &lt; ActiveRecord::Base @@ -3,13 +3,13 @@ class ProfileCategorization &lt; ActiveRecord::Base
3 belongs_to :profile 3 belongs_to :profile
4 belongs_to :category 4 belongs_to :category
5 5
6 - after_create :associate_with_entire_hierarchy  
7 - def associate_with_entire_hierarchy  
8 - return if virtual 6 + def self.add_category_to_profile(category, profile)
  7 +
  8 + connection.execute("insert into categories_profiles (category_id, profile_id) values(#{category.id}, #{profile.id})")
9 9
10 c = category.parent 10 c = category.parent
11 - while !c.nil? && !self.class.find(:first, :conditions => {:profile_id => profile, :category_id => c})  
12 - self.class.create!(:profile => profile, :category => c, :virtual => true) 11 + while !c.nil? && !self.find(:first, :conditions => {:profile_id => profile, :category_id => c})
  12 + connection.execute("insert into categories_profiles (category_id, profile_id, virtual) values(#{c.id}, #{profile.id}, 1>0)")
13 c = c.parent 13 c = c.parent
14 end 14 end
15 end 15 end
test/functional/search_controller_test.rb
@@ -484,7 +484,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -484,7 +484,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
484 child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent) 484 child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent)
485 485
486 p = create_user('test_profile').person 486 p = create_user('test_profile').person
487 - p.categories << child 487 + p.add_category child
488 p.save! 488 p.save!
489 489
490 get :index, :category_path => ['parent-category'], :query => 'test_profile', :find_in => ['people'] 490 get :index, :category_path => ['parent-category'], :query => 'test_profile', :find_in => ['people']
@@ -563,10 +563,10 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -563,10 +563,10 @@ class SearchControllerTest &lt; Test::Unit::TestCase
563 should 'display people with a given initial, under a specific category' do 563 should 'display people with a given initial, under a specific category' do
564 564
565 in_category_and_with_initial = create_user('fergunson').person 565 in_category_and_with_initial = create_user('fergunson').person
566 - in_category_and_with_initial.categories << @category 566 + in_category_and_with_initial.add_category @category
567 567
568 in_category_but_without_initial = create_user('yanerson').person 568 in_category_but_without_initial = create_user('yanerson').person
569 - in_category_but_without_initial.categories << @category 569 + in_category_but_without_initial.add_category @category
570 570
571 not_in_category_but_with_initial = create_user('fergy').person 571 not_in_category_but_with_initial = create_user('fergy').person
572 not_in_category_and_without_initial = create_user('xalanxalan').person 572 not_in_category_and_without_initial = create_user('xalanxalan').person
@@ -580,8 +580,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -580,8 +580,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
580 end 580 end
581 581
582 should 'display communities with a given initial, under a specific category' do 582 should 'display communities with a given initial, under a specific category' do
583 - c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default); c1.categories << @category  
584 - c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default); c2.categories << @category 583 + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default); c1.add_category @category
  584 + c2 = Community.create!(:name => 'beautiful community (another)', :identifier => 'an_bea_comm', :environment => Environment.default); c2.add_category @category
585 585
586 c3 = Community.create!(:name => 'another beautiful community', :identifier => 'lalala', :environment => Environment.default); 586 c3 = Community.create!(:name => 'another beautiful community', :identifier => 'lalala', :environment => Environment.default);
587 c4 = Community.create!(:name => 'damn beautiful community (another)', :identifier => 'lelele', :environment => Environment.default) 587 c4 = Community.create!(:name => 'damn beautiful community (another)', :identifier => 'lelele', :environment => Environment.default)
@@ -595,8 +595,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -595,8 +595,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
595 end 595 end
596 596
597 should 'display enterprises with a given initial, under a specific category' do 597 should 'display enterprises with a given initial, under a specific category' do
598 - ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1'); ent1.categories << @category  
599 - ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2'); ent1.categories << @category 598 + ent1 = Enterprise.create!(:name => 'aaaaa', :identifier => 'teste1'); ent1.add_category @category
  599 + ent2 = Enterprise.create!(:name => 'bbbbb', :identifier => 'teste2'); ent1.add_category @category
600 ent3 = Enterprise.create!(:name => 'aaaa1111', :identifier => 'teste1111') 600 ent3 = Enterprise.create!(:name => 'aaaa1111', :identifier => 'teste1111')
601 ent4 = Enterprise.create!(:name => 'ddddd', :identifier => 'teste2222') 601 ent4 = Enterprise.create!(:name => 'ddddd', :identifier => 'teste2222')
602 602
@@ -611,9 +611,9 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -611,9 +611,9 @@ class SearchControllerTest &lt; Test::Unit::TestCase
611 should 'display articles with a given initial, under a specific category' do 611 should 'display articles with a given initial, under a specific category' do
612 person = create_user('teste').person 612 person = create_user('teste').person
613 art1 = person.articles.build(:name => 'an article to be found'); art1.save! 613 art1 = person.articles.build(:name => 'an article to be found'); art1.save!
614 - art1.categories << @category 614 + art1.add_category @category
615 art2 = person.articles.build(:name => 'better article'); art2.save! 615 art2 = person.articles.build(:name => 'better article'); art2.save!
616 - art2.categories << @category 616 + art2.add_category @category
617 617
618 art3 = person.articles.build(:name => 'another article to be found'); art3.save! 618 art3 = person.articles.build(:name => 'another article to be found'); art3.save!
619 art4 = person.articles.build(:name => 'damn article'); art4.save! 619 art4 = person.articles.build(:name => 'damn article'); art4.save!
test/unit/article_categorization_test.rb
@@ -25,7 +25,7 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase @@ -25,7 +25,7 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase
25 a = p.articles.create!(:name => 'test') 25 a = p.articles.create!(:name => 'test')
26 26
27 assert_difference ArticleCategorization, :count, 2 do 27 assert_difference ArticleCategorization, :count, 2 do
28 - ArticleCategorization.create!(:category => c2, :article => a) 28 + ArticleCategorization.add_category_to_article(c2, a)
29 end 29 end
30 30
31 assert_equal 2, ArticleCategorization.find_all_by_article_id(a.id).size 31 assert_equal 2, ArticleCategorization.find_all_by_article_id(a.id).size
@@ -40,8 +40,8 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase @@ -40,8 +40,8 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase
40 a = p.articles.create!(:name => 'test') 40 a = p.articles.create!(:name => 'test')
41 41
42 assert_difference ArticleCategorization, :count, 3 do 42 assert_difference ArticleCategorization, :count, 3 do
43 - ac = ArticleCategorization.create!(:category => c2, :article => a)  
44 - ac = ArticleCategorization.create!(:category => c3, :article => a) 43 + ArticleCategorization.add_category_to_article(c2, a)
  44 + ArticleCategorization.add_category_to_article(c3, a)
45 end 45 end
46 end 46 end
47 47
@@ -53,8 +53,8 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase @@ -53,8 +53,8 @@ class ArticleCategorizationTest &lt; Test::Unit::TestCase
53 p = create_user('testuser').person 53 p = create_user('testuser').person
54 a = p.articles.create!(:name => 'test') 54 a = p.articles.create!(:name => 'test')
55 55
56 - ac = ArticleCategorization.create!(:category => c2, :article => a)  
57 - ac = ArticleCategorization.create!(:category => c3, :article => a) 56 + ArticleCategorization.add_category_to_article(c2, a)
  57 + ArticleCategorization.add_category_to_article(c3, a)
58 58
59 assert_difference ArticleCategorization, :count, -3 do 59 assert_difference ArticleCategorization, :count, -3 do
60 ArticleCategorization.remove_all_for(a) 60 ArticleCategorization.remove_all_for(a)
test/unit/article_test.rb
@@ -175,14 +175,8 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -175,14 +175,8 @@ class ArticleTest &lt; Test::Unit::TestCase
175 article = profile.articles.build(:name => 'withcategories') 175 article = profile.articles.build(:name => 'withcategories')
176 article.save! 176 article.save!
177 177
178 - assert_raise ActiveRecord::AssociationTypeMismatch do  
179 - article.categories << 1  
180 - end  
181 -  
182 - assert_nothing_raised do  
183 - article.categories << c1  
184 - article.categories << c2  
185 - end 178 + article.add_category c1
  179 + article.add_category c2
186 180
187 assert_equivalent [c1,c2], article.categories(true) 181 assert_equivalent [c1,c2], article.categories(true)
188 end 182 end
test/unit/category_test.rb
@@ -312,9 +312,9 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -312,9 +312,9 @@ class CategoryTest &lt; Test::Unit::TestCase
312 should 'have people' do 312 should 'have people' do
313 c = @env.categories.build(:name => 'my category'); c.save! 313 c = @env.categories.build(:name => 'my category'); c.save!
314 p1 = create_user('testuser_1').person 314 p1 = create_user('testuser_1').person
315 - p1.categories << c 315 + p1.add_category c
316 p2 = create_user('testuser_2').person 316 p2 = create_user('testuser_2').person
317 - p2.categories << c 317 + p2.add_category c
318 assert_equal [p1, p2], c.people 318 assert_equal [p1, p2], c.people
319 end 319 end
320 320
test/unit/profile_categorization_test.rb
@@ -5,7 +5,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -5,7 +5,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
5 should 'have profile and category' do 5 should 'have profile and category' do
6 person = create_user('test_user').person 6 person = create_user('test_user').person
7 cat = Environment.default.categories.build(:name => 'a category'); cat.save! 7 cat = Environment.default.categories.build(:name => 'a category'); cat.save!
8 - person.categories << cat 8 + person.add_category cat
9 person.save! 9 person.save!
10 assert_includes person.categories, cat 10 assert_includes person.categories, cat
11 assert_includes cat.people, person 11 assert_includes cat.people, person
@@ -19,7 +19,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -19,7 +19,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
19 p = create_user('testuser').person 19 p = create_user('testuser').person
20 20
21 assert_difference ProfileCategorization, :count, 2 do 21 assert_difference ProfileCategorization, :count, 2 do
22 - ProfileCategorization.create!(:category => c2, :profile => p) 22 + ProfileCategorization.add_category_to_profile(c2, p)
23 end 23 end
24 24
25 assert_equal 2, ProfileCategorization.find_all_by_profile_id(p.id).size 25 assert_equal 2, ProfileCategorization.find_all_by_profile_id(p.id).size
@@ -33,8 +33,8 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -33,8 +33,8 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
33 p = create_user('testuser').person 33 p = create_user('testuser').person
34 34
35 assert_difference ProfileCategorization, :count, 3 do 35 assert_difference ProfileCategorization, :count, 3 do
36 - ac = ProfileCategorization.create!(:category => c2, :profile => p)  
37 - ac = ProfileCategorization.create!(:category => c3, :profile => p) 36 + ProfileCategorization.add_category_to_profile(c2, p)
  37 + ProfileCategorization.add_category_to_profile(c3, p)
38 end 38 end
39 end 39 end
40 40
@@ -45,8 +45,8 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -45,8 +45,8 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
45 45
46 p = create_user('testuser').person 46 p = create_user('testuser').person
47 47
48 - ac = ProfileCategorization.create!(:category => c2, :profile => p)  
49 - ac = ProfileCategorization.create!(:category => c3, :profile => p) 48 + ProfileCategorization.add_category_to_profile(c2, p)
  49 + ProfileCategorization.add_category_to_profile(c3, p)
50 50
51 assert_difference ProfileCategorization, :count, -3 do 51 assert_difference ProfileCategorization, :count, -3 do
52 ProfileCategorization.remove_all_for(p) 52 ProfileCategorization.remove_all_for(p)
test/unit/profile_test.rb
@@ -390,7 +390,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -390,7 +390,7 @@ class ProfileTest &lt; Test::Unit::TestCase
390 should 'have categories' do 390 should 'have categories' do
391 c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') 391 c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile')
392 cat = Environment.default.categories.build(:name => 'a category'); cat.save! 392 cat = Environment.default.categories.build(:name => 'a category'); cat.save!
393 - c.categories << cat 393 + c.add_category cat
394 c.save! 394 c.save!
395 assert_includes c.categories, cat 395 assert_includes c.categories, cat
396 end 396 end