From d8b52907245fe8b50b66249c7dc3af81fea60abb Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Thu, 26 Jul 2007 18:55:01 +0000 Subject: [PATCH] ActionItem6: tests broken by the change of the profile ownership association fixed --- app/models/enterprise.rb | 2 +- app/models/profile.rb | 6 +++--- app/models/user.rb | 4 ++-- test/unit/profile_test.rb | 14 +++++++------- test/unit/user_test.rb | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 2a94d72..2a7c2a3 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -5,7 +5,7 @@ class Enterprise < ActiveRecord::Base enterprise_profile = Profile.create(:identifier => enterprise.name) end - has_one :enterprise_profile, :class_name => Profile, :as => :profile_owner + has_one :enterprise_profile, :class_name => 'Profile', :as => :profile_owner def name=(a_name) enterprise_profile.name = a_name diff --git a/app/models/profile.rb b/app/models/profile.rb index d20d63b..bde2c30 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -35,10 +35,10 @@ class Profile < ActiveRecord::Base validates_format_of :identifier, :with => IDENTIFIER_FORMAT validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS - # A user cannot have more than one profile, but many profiles can exist + # A profile_owner cannot have more than one profile, but many profiles can exist # without being associated to a particular user. - validates_uniqueness_of :user_id, :if => (lambda do |profile| - ! profile.user_id.nil? + validates_uniqueness_of :profile_owner_id, :scope => :profile_owner_type, :if => (lambda do |profile| + ! profile.profile_owner_id.nil? end) # creates a new Profile. By default, it is attached to the default diff --git a/app/models/user.rb b/app/models/user.rb index d930f6c..455d5ab 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,10 +5,10 @@ require 'digest/sha1' class User < ActiveRecord::Base after_create do |user| - personal_profile = Profile.create!(:identifier => user.login) + Profile.create!(:identifier => user.login, :profile_owner_id => user.id, :profile_owner_type => 'User') end - has_one :personal_profile, :class_name => Profile, :as => :profile_owner + has_one :personal_profile, :class_name => 'Profile', :as => :profile_owner # Virtual attribute for the unencrypted password attr_accessor :password diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 10f15e7..689e2f2 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -41,33 +41,33 @@ class ProfileTest < Test::Unit::TestCase def test_can_have_user p = profiles(:johndoe) - assert_kind_of User, p.user + assert_kind_of User, p.profile_owner end def test_may_have_no_user p = profiles(:john_and_joe) - assert_nil p.user + assert_nil p.profile_owner assert p.valid? end def test_only_one_profile_per_user p1 = profiles(:johndoe) - assert_equal users(:johndoe), p1.user + assert_equal users(:johndoe), p1.profile_owner p2 = Profile.new - p2.user = users(:johndoe) + p2.profile_owner = users(:johndoe) assert !p2.valid? - assert p2.errors.invalid?(:user_id) + assert p2.errors.invalid?(:profile_owner_id) end def test_several_profiles_without_user p1 = profiles(:john_and_joe) assert p1.valid? - assert_nil p1.user + assert_nil p1.profile_owner p2 = Profile.new assert !p2.valid? - assert !p2.errors.invalid?(:user_id) + assert !p2.errors.invalid?(:profile_owner_id) end def test_cannot_rename diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 9a2784c..eb663ae 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -74,7 +74,7 @@ class UserTest < Test::Unit::TestCase user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') - assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = "User"', user.id]) + assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = ?', user.id, 'User']) assert_equal users_count + 1, User.count assert_equal profiles_count + 1, Profile.count -- libgit2 0.21.2