diff --git a/app/models/affiliation.rb b/app/models/affiliation.rb index c3f0c6c..8e84a00 100644 --- a/app/models/affiliation.rb +++ b/app/models/affiliation.rb @@ -1,4 +1,4 @@ class Affiliation < ActiveRecord::Base belongs_to :person - belongs_to :organization + belongs_to :profile end diff --git a/app/models/organization.rb b/app/models/organization.rb index 273cbf2..99c2063 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,6 +1,4 @@ class Organization < Profile has_one :organization_info - has_many :affiliations - has_many :people, :through => :affiliations has_many :validated_enterprises, :class_name => 'enterprise' end diff --git a/app/models/person.rb b/app/models/person.rb index 538a972..a67968d 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,9 +1,9 @@ class Person < Profile - ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :source => 'organization'} + ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'} belongs_to :user has_many :affiliations - has_many :organizations, :through => :affiliations + has_many :profiles, :through => :affiliations has_many :enterprises, ENTERPRISE has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false]) has_many :active_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', true]) @@ -11,4 +11,6 @@ class Person < Profile has_many :friends, :class_name => 'Person', :through => :friendships has_many :person_friendships has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' + + validates_presence_of :user_id end diff --git a/app/models/profile.rb b/app/models/profile.rb index 0a2e9ed..d1621f1 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -25,6 +25,9 @@ class Profile < ActiveRecord::Base has_many :domains, :as => :owner belongs_to :virtual_community + has_many :affiliations + has_many :people, :through => :affiliations + # Sets the identifier for this profile. Raises an exception when called on a # existing profile (since profiles cannot be renamed) diff --git a/db/migrate/007_create_affiliations.rb b/db/migrate/007_create_affiliations.rb index 35f37b3..0d34d56 100644 --- a/db/migrate/007_create_affiliations.rb +++ b/db/migrate/007_create_affiliations.rb @@ -2,7 +2,7 @@ class CreateAffiliations < ActiveRecord::Migration def self.up create_table :affiliations do |t| t.column :person_id, :integer - t.column :organization_id, :integer + t.column :profile_id, :integer end end diff --git a/db/migrate/009_create_organizations.rb b/db/migrate/009_create_organizations.rb deleted file mode 100644 index 1a3f2fc..0000000 --- a/db/migrate/009_create_organizations.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateOrganizations < ActiveRecord::Migration - def self.up - create_table :organizations do |t| - end - end - - def self.down - drop_table :organizations - end -end diff --git a/test/fixtures/comatose_pages.yml b/test/fixtures/comatose_pages.yml index d1ec923..f56068b 100644 --- a/test/fixtures/comatose_pages.yml +++ b/test/fixtures/comatose_pages.yml @@ -1,3 +1,4 @@ root: id: 1 title: Root page + slug: '' diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 3ad8731..94a72f7 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1,37 +1,63 @@ require File.dirname(__FILE__) + '/../test_helper' class PersonTest < Test::Unit::TestCase - fixtures :profiles, :users + fixtures :profiles, :users, :comatose_pages - def test_can_have_user - p = Person.new(:name => 'John', :identfier => 'john') - p = profiles(:johndoe) - assert_kind_of User, p.user + def test_person_must_come_form_the_cration_of_an_user + p = Person.new(:name => 'John', :identifier => 'john') + assert !p.valid? + p.user = User.create(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') + assert !p.valid? + p = User.create(:login => 'johnz', :email => 'johnz@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + assert p.valid? end - def test_may_have_no_user - p = profiles(:john_and_joe) - assert_nil p.user - assert p.valid? + def test_can_associate_to_a_profile + pr = Profile.new(:identifier => 'profile', :name => 'profile') + assert pr.save + pe = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person + assert pe.save + pe.profiles << pr + assert pe.profiles.include?(pr) end - def test_only_one_profile_per_user - p1 = profiles(:johndoe) - assert_equal users(:johndoe), p1.user - - p2 = Person.new - p2.user = users(:johndoe) - assert !p2.valid? - assert p2.errors.invalid?(:user_id) + def test_can_belongs_to_an_enterprise + e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise') + assert e.save + p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person + assert p.save + p.profiles << e + assert p.enterprises.include?(e) end - def test_several_profiles_without_user - p1 = profiles(:john_and_joe) - assert p1.valid? - assert_nil p1.user + def test_can_belongs_to_an_enterprise + e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise') + assert e.save + p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person + assert p.save + p.profiles << e + assert p.enterprises.include?(e) + end + + def test_can_have_user + u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') + p = Person.new(:name => 'John', :identifier => 'john') + u.person = p + assert u.save + assert_kind_of User, p.user + assert_equal 'John', u.person.name + end + def test_only_one_person_per_user + u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') + assert u.save + + p1 = u.person + assert_equal u, p1.user + p2 = Person.new + p2.user = u assert !p2.valid? - assert !p2.errors.invalid?(:user_id) + assert p2.errors.invalid?(:user_id) end end -- libgit2 0.21.2