Commit da6076018abc658525b8974202868ff573db3d1f

Authored by MoisesMachado
1 parent c2c7c3db

ActionItem6: changed the affiliation relation to be between person and profile


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@317 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/affiliation.rb
1 class Affiliation < ActiveRecord::Base 1 class Affiliation < ActiveRecord::Base
2 belongs_to :person 2 belongs_to :person
3 - belongs_to :organization 3 + belongs_to :profile
4 end 4 end
app/models/organization.rb
1 class Organization < Profile 1 class Organization < Profile
2 has_one :organization_info 2 has_one :organization_info
3 - has_many :affiliations  
4 - has_many :people, :through => :affiliations  
5 has_many :validated_enterprises, :class_name => 'enterprise' 3 has_many :validated_enterprises, :class_name => 'enterprise'
6 end 4 end
app/models/person.rb
1 class Person < Profile 1 class Person < Profile
2 - ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :source => 'organization'} 2 + ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'}
3 3
4 belongs_to :user 4 belongs_to :user
5 has_many :affiliations 5 has_many :affiliations
6 - has_many :organizations, :through => :affiliations 6 + has_many :profiles, :through => :affiliations
7 has_many :enterprises, ENTERPRISE 7 has_many :enterprises, ENTERPRISE
8 has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false]) 8 has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false])
9 has_many :active_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', true]) 9 has_many :active_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', true])
@@ -11,4 +11,6 @@ class Person &lt; Profile @@ -11,4 +11,6 @@ class Person &lt; Profile
11 has_many :friends, :class_name => 'Person', :through => :friendships 11 has_many :friends, :class_name => 'Person', :through => :friendships
12 has_many :person_friendships 12 has_many :person_friendships
13 has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' 13 has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
  14 +
  15 + validates_presence_of :user_id
14 end 16 end
app/models/profile.rb
@@ -25,6 +25,9 @@ class Profile &lt; ActiveRecord::Base @@ -25,6 +25,9 @@ class Profile &lt; ActiveRecord::Base
25 25
26 has_many :domains, :as => :owner 26 has_many :domains, :as => :owner
27 belongs_to :virtual_community 27 belongs_to :virtual_community
  28 + has_many :affiliations
  29 + has_many :people, :through => :affiliations
  30 +
28 31
29 # Sets the identifier for this profile. Raises an exception when called on a 32 # Sets the identifier for this profile. Raises an exception when called on a
30 # existing profile (since profiles cannot be renamed) 33 # existing profile (since profiles cannot be renamed)
db/migrate/007_create_affiliations.rb
@@ -2,7 +2,7 @@ class CreateAffiliations &lt; ActiveRecord::Migration @@ -2,7 +2,7 @@ class CreateAffiliations &lt; ActiveRecord::Migration
2 def self.up 2 def self.up
3 create_table :affiliations do |t| 3 create_table :affiliations do |t|
4 t.column :person_id, :integer 4 t.column :person_id, :integer
5 - t.column :organization_id, :integer 5 + t.column :profile_id, :integer
6 end 6 end
7 end 7 end
8 8
db/migrate/009_create_organizations.rb
@@ -1,10 +0,0 @@ @@ -1,10 +0,0 @@
1 -class CreateOrganizations < ActiveRecord::Migration  
2 - def self.up  
3 - create_table :organizations do |t|  
4 - end  
5 - end  
6 -  
7 - def self.down  
8 - drop_table :organizations  
9 - end  
10 -end  
test/fixtures/comatose_pages.yml
1 root: 1 root:
2 id: 1 2 id: 1
3 title: Root page 3 title: Root page
  4 + slug: ''
test/unit/person_test.rb
1 require File.dirname(__FILE__) + '/../test_helper' 1 require File.dirname(__FILE__) + '/../test_helper'
2 2
3 class PersonTest < Test::Unit::TestCase 3 class PersonTest < Test::Unit::TestCase
4 - fixtures :profiles, :users 4 + fixtures :profiles, :users, :comatose_pages
5 5
6 - def test_can_have_user  
7 - p = Person.new(:name => 'John', :identfier => 'john')  
8 - p = profiles(:johndoe)  
9 - assert_kind_of User, p.user 6 + def test_person_must_come_form_the_cration_of_an_user
  7 + p = Person.new(:name => 'John', :identifier => 'john')
  8 + assert !p.valid?
  9 + p.user = User.create(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
  10 + assert !p.valid?
  11 + p = User.create(:login => 'johnz', :email => 'johnz@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
  12 + assert p.valid?
10 end 13 end
11 14
12 - def test_may_have_no_user  
13 - p = profiles(:john_and_joe)  
14 - assert_nil p.user  
15 - assert p.valid? 15 + def test_can_associate_to_a_profile
  16 + pr = Profile.new(:identifier => 'profile', :name => 'profile')
  17 + assert pr.save
  18 + pe = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
  19 + assert pe.save
  20 + pe.profiles << pr
  21 + assert pe.profiles.include?(pr)
16 end 22 end
17 23
18 - def test_only_one_profile_per_user  
19 - p1 = profiles(:johndoe)  
20 - assert_equal users(:johndoe), p1.user  
21 -  
22 - p2 = Person.new  
23 - p2.user = users(:johndoe)  
24 - assert !p2.valid?  
25 - assert p2.errors.invalid?(:user_id) 24 + def test_can_belongs_to_an_enterprise
  25 + e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise')
  26 + assert e.save
  27 + p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
  28 + assert p.save
  29 + p.profiles << e
  30 + assert p.enterprises.include?(e)
26 end 31 end
27 32
28 - def test_several_profiles_without_user  
29 - p1 = profiles(:john_and_joe)  
30 - assert p1.valid?  
31 - assert_nil p1.user 33 + def test_can_belongs_to_an_enterprise
  34 + e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise')
  35 + assert e.save
  36 + p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
  37 + assert p.save
  38 + p.profiles << e
  39 + assert p.enterprises.include?(e)
  40 + end
  41 +
  42 + def test_can_have_user
  43 + u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
  44 + p = Person.new(:name => 'John', :identifier => 'john')
  45 + u.person = p
  46 + assert u.save
  47 + assert_kind_of User, p.user
  48 + assert_equal 'John', u.person.name
  49 + end
32 50
  51 + def test_only_one_person_per_user
  52 + u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
  53 + assert u.save
  54 +
  55 + p1 = u.person
  56 + assert_equal u, p1.user
  57 +
33 p2 = Person.new 58 p2 = Person.new
  59 + p2.user = u
34 assert !p2.valid? 60 assert !p2.valid?
35 - assert !p2.errors.invalid?(:user_id) 61 + assert p2.errors.invalid?(:user_id)
36 end 62 end
37 end 63 end