Commit d8b52907245fe8b50b66249c7dc3af81fea60abb

Authored by MoisesMachado
1 parent fe26291a

ActionItem6: tests broken by the change of the profile ownership association fixed

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@177 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/enterprise.rb
@@ -5,7 +5,7 @@ class Enterprise < ActiveRecord::Base @@ -5,7 +5,7 @@ class Enterprise < ActiveRecord::Base
5 enterprise_profile = Profile.create(:identifier => enterprise.name) 5 enterprise_profile = Profile.create(:identifier => enterprise.name)
6 end 6 end
7 7
8 - has_one :enterprise_profile, :class_name => Profile, :as => :profile_owner 8 + has_one :enterprise_profile, :class_name => 'Profile', :as => :profile_owner
9 9
10 def name=(a_name) 10 def name=(a_name)
11 enterprise_profile.name = a_name 11 enterprise_profile.name = a_name
app/models/profile.rb
@@ -35,10 +35,10 @@ class Profile < ActiveRecord::Base @@ -35,10 +35,10 @@ class Profile < ActiveRecord::Base
35 validates_format_of :identifier, :with => IDENTIFIER_FORMAT 35 validates_format_of :identifier, :with => IDENTIFIER_FORMAT
36 validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS 36 validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
37 37
38 - # A user cannot have more than one profile, but many profiles can exist 38 + # A profile_owner cannot have more than one profile, but many profiles can exist
39 # without being associated to a particular user. 39 # without being associated to a particular user.
40 - validates_uniqueness_of :user_id, :if => (lambda do |profile|  
41 - ! profile.user_id.nil? 40 + validates_uniqueness_of :profile_owner_id, :scope => :profile_owner_type, :if => (lambda do |profile|
  41 + ! profile.profile_owner_id.nil?
42 end) 42 end)
43 43
44 # creates a new Profile. By default, it is attached to the default 44 # creates a new Profile. By default, it is attached to the default
app/models/user.rb
@@ -5,10 +5,10 @@ require 'digest/sha1' @@ -5,10 +5,10 @@ require 'digest/sha1'
5 class User < ActiveRecord::Base 5 class User < ActiveRecord::Base
6 6
7 after_create do |user| 7 after_create do |user|
8 - personal_profile = Profile.create!(:identifier => user.login) 8 + Profile.create!(:identifier => user.login, :profile_owner_id => user.id, :profile_owner_type => 'User')
9 end 9 end
10 10
11 - has_one :personal_profile, :class_name => Profile, :as => :profile_owner 11 + has_one :personal_profile, :class_name => 'Profile', :as => :profile_owner
12 12
13 # Virtual attribute for the unencrypted password 13 # Virtual attribute for the unencrypted password
14 attr_accessor :password 14 attr_accessor :password
test/unit/profile_test.rb
@@ -41,33 +41,33 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -41,33 +41,33 @@ class ProfileTest &lt; Test::Unit::TestCase
41 41
42 def test_can_have_user 42 def test_can_have_user
43 p = profiles(:johndoe) 43 p = profiles(:johndoe)
44 - assert_kind_of User, p.user 44 + assert_kind_of User, p.profile_owner
45 end 45 end
46 46
47 def test_may_have_no_user 47 def test_may_have_no_user
48 p = profiles(:john_and_joe) 48 p = profiles(:john_and_joe)
49 - assert_nil p.user 49 + assert_nil p.profile_owner
50 assert p.valid? 50 assert p.valid?
51 end 51 end
52 52
53 def test_only_one_profile_per_user 53 def test_only_one_profile_per_user
54 p1 = profiles(:johndoe) 54 p1 = profiles(:johndoe)
55 - assert_equal users(:johndoe), p1.user 55 + assert_equal users(:johndoe), p1.profile_owner
56 56
57 p2 = Profile.new 57 p2 = Profile.new
58 - p2.user = users(:johndoe) 58 + p2.profile_owner = users(:johndoe)
59 assert !p2.valid? 59 assert !p2.valid?
60 - assert p2.errors.invalid?(:user_id) 60 + assert p2.errors.invalid?(:profile_owner_id)
61 end 61 end
62 62
63 def test_several_profiles_without_user 63 def test_several_profiles_without_user
64 p1 = profiles(:john_and_joe) 64 p1 = profiles(:john_and_joe)
65 assert p1.valid? 65 assert p1.valid?
66 - assert_nil p1.user 66 + assert_nil p1.profile_owner
67 67
68 p2 = Profile.new 68 p2 = Profile.new
69 assert !p2.valid? 69 assert !p2.valid?
70 - assert !p2.errors.invalid?(:user_id) 70 + assert !p2.errors.invalid?(:profile_owner_id)
71 end 71 end
72 72
73 def test_cannot_rename 73 def test_cannot_rename
test/unit/user_test.rb
@@ -74,7 +74,7 @@ class UserTest &lt; Test::Unit::TestCase @@ -74,7 +74,7 @@ class UserTest &lt; Test::Unit::TestCase
74 74
75 user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') 75 user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test')
76 76
77 - assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = "User"', user.id]) 77 + assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = ?', user.id, 'User'])
78 78
79 assert_equal users_count + 1, User.count 79 assert_equal users_count + 1, User.count
80 assert_equal profiles_count + 1, Profile.count 80 assert_equal profiles_count + 1, Profile.count