diff --git a/app/controllers/profile_admin/enterprise_controller.rb b/app/controllers/profile_admin/enterprise_controller.rb index 7d2e767..ac6c894 100644 --- a/app/controllers/profile_admin/enterprise_controller.rb +++ b/app/controllers/profile_admin/enterprise_controller.rb @@ -5,8 +5,8 @@ class EnterpriseController < ProfileAdminController # Redirects to show if there is only one action and to list otherwise def index - if @my_enterprises.size == 1 - redirect_to :action => 'show', :id => @my_enterprises[0] + if @person.enterprises.size == 1 + redirect_to :action => 'show', :id => @person.enterprises[0] else redirect_to :action => 'list' end @@ -14,12 +14,12 @@ class EnterpriseController < ProfileAdminController # Lists all enterprises def list - @enterprises = Enterprise.find(:all) - @my_enterprises + @enterprises = Enterprise.find(:all) - @person.enterprises end # Show details about an enterprise def show - @enterprise = @my_enterprises.find(params[:id]) + @enterprise = Enterprise.find(params[:id]) end # Make a form to the creation of an eterprise @@ -47,13 +47,13 @@ class EnterpriseController < ProfileAdminController # Provides an interface to editing the enterprise details def edit - @enterprise = @my_enterprises.find(params[:id]) + @enterprise = @person.enterprises(:id => params[:id])[0] @validation_entities = Organization.find(:all) - [@enterprise] end # Saves the changes made in an enterprise def update - @enterprise = @my_enterprises.find(params[:id]) + @enterprise = @person.enterprises(:id => params[:id])[0] if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info]) redirect_to :action => 'index' else @@ -66,13 +66,14 @@ class EnterpriseController < ProfileAdminController # Make the current user a new member of the enterprise def affiliate @enterprise = Enterprise.find(params[:id]) - @enterprise.people << @person + member_role = Role.find_by_name('member') || Role.create(:name => 'member') + @enterprise.affiliate(@person,member_role) redirect_to :action => 'index' end # Elimitates the enterprise of the system def destroy - @enterprise = @my_enterprises.find(params[:id]) + @enterprise = @person.enterprises(:id => params[:id])[0] if @enterprise @enterprise.destroy else diff --git a/app/models/affiliation.rb b/app/models/affiliation.rb deleted file mode 100644 index 3b4c497..0000000 --- a/app/models/affiliation.rb +++ /dev/null @@ -1,5 +0,0 @@ -# Affiliation it is a join model to hold affiliations of people to others profiles -class Affiliation < ActiveRecord::Base - belongs_to :person - belongs_to :profile -end diff --git a/app/models/person.rb b/app/models/person.rb index 65757d1..247418c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,25 +1,48 @@ # A person is the profile of an user holding all relationships with the rest of the system class Person < Profile - ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'} - belongs_to :user - has_many :affiliations, :dependent => :destroy - 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]) - has_many :friendships - has_many :friends, :class_name => 'Person', :through => :friendships - has_many :person_friendships - has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' + +# has_many :friendships +# has_many :friends, :class_name => 'Person', :through => :friendships +# has_many :person_friendships +# has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' + has_one :person_info has_many :role_assignments + has_many :memberships, :through => :role_assignments, :source => 'resource', :class_name => 'Enterprise' def has_permission?(perm, res=nil) role_assignments.any? {|ra| ra.has_permission?(perm, res)} end + def self.conditions_for_profiles(conditions, person) + new_conditions = sanitize_sql(['role_assignments.person_id = ?', person]) + new_conditions << ' AND ' + sanitize_sql(conditions) unless conditions.blank? + new_conditions + end + + def profiles(conditions = {}) + Profile.find( + :all, + :conditions => self.class.conditions_for_profiles(conditions, self), + :joins => "LEFT JOIN role_assignments ON profiles.id = role_assignments.resource_id AND role_assignments.resource_type = \"#{Profile.base_class.name}\"", + :select => 'profiles.*') + end + + + def enterprises(conditions = {}) + profiles( ({:type => 'Enterprise'}).merge(conditions)) + end + + def pending_enterprises + enterprises :active => false + end + + def active_enterprises + enterprises :active => true + end + def info person_info end diff --git a/app/models/profile.rb b/app/models/profile.rb index 01820ed..d66b2e4 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -33,10 +33,9 @@ class Profile < ActiveRecord::Base has_many :domains, :as => :owner belongs_to :virtual_community - has_many :affiliations, :dependent => :destroy - has_many :people, :through => :affiliations - has_many :role_assignment, :as => :resource + has_many :role_assignments, :as => :resource + has_many :people, :through => :role_assignments # Sets the identifier for this profile. Raises an exception when called on a @@ -103,4 +102,7 @@ class Profile < ActiveRecord::Base homepage.children.find(:all, :limit => limit, :order => 'created_on desc') end + def affiliate(person, role) + RoleAssignment.new(:person => person, :role => role, :resource => self).save + end end diff --git a/db/migrate/007_create_affiliations.rb b/db/migrate/007_create_affiliations.rb deleted file mode 100644 index 0d34d56..0000000 --- a/db/migrate/007_create_affiliations.rb +++ /dev/null @@ -1,12 +0,0 @@ -class CreateAffiliations < ActiveRecord::Migration - def self.up - create_table :affiliations do |t| - t.column :person_id, :integer - t.column :profile_id, :integer - end - end - - def self.down - drop_table :affiliations - end -end diff --git a/test/fixtures/affiliations.yml b/test/fixtures/affiliations.yml deleted file mode 100644 index 3088964..0000000 --- a/test/fixtures/affiliations.yml +++ /dev/null @@ -1,13 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 - person_id: 4 - profile_id: 5 -two: - id: 2 - person_id: 1 - profile_id: 5 -three: - id: 3 - person_id: 1 - profile_id: 6 diff --git a/test/fixtures/role_assignments.yml b/test/fixtures/role_assignments.yml index b49c4eb..36fc30c 100644 --- a/test/fixtures/role_assignments.yml +++ b/test/fixtures/role_assignments.yml @@ -1,5 +1,19 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html one: id: 1 + person_id: 4 + role_id: 1 + resource_id: 5 + resource_type: 'Profile' two: id: 2 + person_id: 1 + role_id: 1 + resource_id: 5 + resource_type: 'Profile' +three: + id: 3 + person_id: 1 + role_id: 1 + resource_id: 6 + resource_type: 'Profile' diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index b49c4eb..d18e60e 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -1,5 +1,7 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html one: id: 1 + name: 'member' two: id: 2 + name: 'owner' diff --git a/test/unit/affiliation_test.rb b/test/unit/affiliation_test.rb deleted file mode 100644 index c7013ff..0000000 --- a/test/unit/affiliation_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class AffiliationTest < Test::Unit::TestCase - fixtures :affiliations - - # Replace this with your real tests. - def test_truth - assert true - end -end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 45769ec..e2b4e11 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -17,7 +17,8 @@ class PersonTest < Test::Unit::TestCase assert pr.save pe = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person assert pe.save - pe.profiles << pr + member_role = Role.create(:name => 'member') + pr.affiliate(pe, member_role) assert pe.profiles.include?(pr) end @@ -26,16 +27,8 @@ class PersonTest < Test::Unit::TestCase 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_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 + member_role = Role.create(:name => 'member') + e.affiliate(p, member_role) assert p.enterprises.include?(e) end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 077a5ac..94032b8 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -79,7 +79,10 @@ class ProfileTest < Test::Unit::TestCase def test_can_have_affiliated_people pr = Profile.create(:name => 'composite_profile', :identifier => 'composite') pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person - pr.people << pe + + member_role = Role.new(:name => 'member') + assert member_role.save + assert pr.affiliate(pe, member_role) assert pe.profiles.include?(pr) end -- libgit2 0.21.2