Commit fe26291a5591f11b3dcb486ab324ada967349a2b
1 parent
4548a0ce
Exists in
master
and in
22 other branches
ActionItem6: profile association made polymorphic
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@176 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
24 additions
and
8 deletions
Show diff stats
app/models/enterprise.rb
| 1 | 1 | #A enterprise is a kind of profile. According to the system concept, only enterprises can offer priducts/services |
| 2 | 2 | class Enterprise < ActiveRecord::Base |
| 3 | 3 | |
| 4 | - has_one :enterprise_profile, :class_name => Profile | |
| 4 | + after_create do |enterprise| | |
| 5 | + enterprise_profile = Profile.create(:identifier => enterprise.name) | |
| 6 | + end | |
| 7 | + | |
| 8 | + has_one :enterprise_profile, :class_name => Profile, :as => :profile_owner | |
| 9 | + | |
| 10 | + def name=(a_name) | |
| 11 | + enterprise_profile.name = a_name | |
| 12 | + end | |
| 13 | + | |
| 14 | + def name | |
| 15 | + enterprise_profile.name | |
| 16 | + end | |
| 5 | 17 | |
| 6 | 18 | end | ... | ... |
app/models/profile.rb
| ... | ... | @@ -19,7 +19,7 @@ class Profile < ActiveRecord::Base |
| 19 | 19 | |
| 20 | 20 | has_many :domains, :as => :owner |
| 21 | 21 | belongs_to :virtual_community |
| 22 | - belongs_to :user | |
| 22 | + belongs_to :profile_owner, :polymorphic => true | |
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | # Sets the identifier for this profile. Raises an exception when called on a | ... | ... |
app/models/user.rb
| ... | ... | @@ -5,9 +5,10 @@ require 'digest/sha1' |
| 5 | 5 | class User < ActiveRecord::Base |
| 6 | 6 | |
| 7 | 7 | after_create do |user| |
| 8 | - Profile.create!(:identifier => user.login, :user_id => user.id) | |
| 8 | + personal_profile = Profile.create!(:identifier => user.login) | |
| 9 | 9 | end |
| 10 | - has_one :personal_profile, :class_name => Profile | |
| 10 | + | |
| 11 | + has_one :personal_profile, :class_name => Profile, :as => :profile_owner | |
| 11 | 12 | |
| 12 | 13 | # Virtual attribute for the unencrypted password |
| 13 | 14 | attr_accessor :password | ... | ... |
db/migrate/002_create_profiles.rb
| ... | ... | @@ -4,7 +4,8 @@ class CreateProfiles < ActiveRecord::Migration |
| 4 | 4 | t.column :name, :string |
| 5 | 5 | t.column :identifier, :string |
| 6 | 6 | t.column :virtual_community_id, :integer |
| 7 | - t.column :user_id, :integer | |
| 7 | + t.column :profile_owner_id, :integer | |
| 8 | + t.column :profile_owner_type, :string | |
| 8 | 9 | t.column :template, :string, :default => "default" |
| 9 | 10 | t.column :theme, :string, :default => "default" |
| 10 | 11 | t.column :icon_theme, :string, :default => "default" | ... | ... |
test/fixtures/profiles.yml
| ... | ... | @@ -4,7 +4,8 @@ johndoe: |
| 4 | 4 | name: 'John Doe' |
| 5 | 5 | identifier: johndoe |
| 6 | 6 | virtual_community_id: 1 |
| 7 | - user_id: 1 | |
| 7 | + profile_owner_id: 1 | |
| 8 | + profile_owner_type: 'User' | |
| 8 | 9 | template: 'default' |
| 9 | 10 | icon_theme: 'default' |
| 10 | 11 | theme: 'default' |
| ... | ... | @@ -13,7 +14,8 @@ joerandomhacker: |
| 13 | 14 | name: 'Joe Random Hacker' |
| 14 | 15 | identifier: joerandomhacker |
| 15 | 16 | virtual_community_id: 1 |
| 16 | - user_id: 2 | |
| 17 | + profile_owner_id: 2 | |
| 18 | + profile_owner_type: 'User' | |
| 17 | 19 | template: 'simple' |
| 18 | 20 | icon_theme: 'simple' |
| 19 | 21 | theme: 'simple' | ... | ... |
test/unit/user_test.rb
| ... | ... | @@ -74,7 +74,7 @@ class UserTest < Test::Unit::TestCase |
| 74 | 74 | |
| 75 | 75 | user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') |
| 76 | 76 | |
| 77 | - assert Profile.exists?(['user_id = ?', user.id]) | |
| 77 | + assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = "User"', user.id]) | |
| 78 | 78 | |
| 79 | 79 | assert_equal users_count + 1, User.count |
| 80 | 80 | assert_equal profiles_count + 1, Profile.count | ... | ... |