From fe26291a5591f11b3dcb486ab324ada967349a2b Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Thu, 26 Jul 2007 17:52:04 +0000 Subject: [PATCH] ActionItem6: profile association made polymorphic --- app/models/enterprise.rb | 14 +++++++++++++- app/models/profile.rb | 2 +- app/models/user.rb | 5 +++-- db/migrate/002_create_profiles.rb | 3 ++- test/fixtures/profiles.yml | 6 ++++-- test/unit/user_test.rb | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a4cf25b..2a94d72 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,6 +1,18 @@ #A enterprise is a kind of profile. According to the system concept, only enterprises can offer priducts/services class Enterprise < ActiveRecord::Base - has_one :enterprise_profile, :class_name => Profile + after_create do |enterprise| + enterprise_profile = Profile.create(:identifier => enterprise.name) + end + + has_one :enterprise_profile, :class_name => Profile, :as => :profile_owner + + def name=(a_name) + enterprise_profile.name = a_name + end + + def name + enterprise_profile.name + end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 8fb6d00..d20d63b 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -19,7 +19,7 @@ class Profile < ActiveRecord::Base has_many :domains, :as => :owner belongs_to :virtual_community - belongs_to :user + belongs_to :profile_owner, :polymorphic => true # Sets the identifier for this profile. Raises an exception when called on a diff --git a/app/models/user.rb b/app/models/user.rb index 9572406..d930f6c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,9 +5,10 @@ require 'digest/sha1' class User < ActiveRecord::Base after_create do |user| - Profile.create!(:identifier => user.login, :user_id => user.id) + personal_profile = Profile.create!(:identifier => user.login) end - has_one :personal_profile, :class_name => Profile + + has_one :personal_profile, :class_name => Profile, :as => :profile_owner # Virtual attribute for the unencrypted password attr_accessor :password diff --git a/db/migrate/002_create_profiles.rb b/db/migrate/002_create_profiles.rb index 7abc264..c1a628f 100644 --- a/db/migrate/002_create_profiles.rb +++ b/db/migrate/002_create_profiles.rb @@ -4,7 +4,8 @@ class CreateProfiles < ActiveRecord::Migration t.column :name, :string t.column :identifier, :string t.column :virtual_community_id, :integer - t.column :user_id, :integer + t.column :profile_owner_id, :integer + t.column :profile_owner_type, :string t.column :template, :string, :default => "default" t.column :theme, :string, :default => "default" t.column :icon_theme, :string, :default => "default" diff --git a/test/fixtures/profiles.yml b/test/fixtures/profiles.yml index d199f77..79cf425 100644 --- a/test/fixtures/profiles.yml +++ b/test/fixtures/profiles.yml @@ -4,7 +4,8 @@ johndoe: name: 'John Doe' identifier: johndoe virtual_community_id: 1 - user_id: 1 + profile_owner_id: 1 + profile_owner_type: 'User' template: 'default' icon_theme: 'default' theme: 'default' @@ -13,7 +14,8 @@ joerandomhacker: name: 'Joe Random Hacker' identifier: joerandomhacker virtual_community_id: 1 - user_id: 2 + profile_owner_id: 2 + profile_owner_type: 'User' template: 'simple' icon_theme: 'simple' theme: 'simple' diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 1657d65..9a2784c 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?(['user_id = ?', user.id]) + assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = "User"', user.id]) assert_equal users_count + 1, User.count assert_equal profiles_count + 1, Profile.count -- libgit2 0.21.2