Commit f947ef5a84ff10cee85af89383928db59c7a427e

Authored by MoisesMachado
1 parent 44acf4c2

ActionItem5: functionality of affiliaton replaced by roles assignments


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@495 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/profile_admin/enterprise_controller.rb
... ... @@ -5,8 +5,8 @@ class EnterpriseController < ProfileAdminController
5 5  
6 6 # Redirects to show if there is only one action and to list otherwise
7 7 def index
8   - if @my_enterprises.size == 1
9   - redirect_to :action => 'show', :id => @my_enterprises[0]
  8 + if @person.enterprises.size == 1
  9 + redirect_to :action => 'show', :id => @person.enterprises[0]
10 10 else
11 11 redirect_to :action => 'list'
12 12 end
... ... @@ -14,12 +14,12 @@ class EnterpriseController < ProfileAdminController
14 14  
15 15 # Lists all enterprises
16 16 def list
17   - @enterprises = Enterprise.find(:all) - @my_enterprises
  17 + @enterprises = Enterprise.find(:all) - @person.enterprises
18 18 end
19 19  
20 20 # Show details about an enterprise
21 21 def show
22   - @enterprise = @my_enterprises.find(params[:id])
  22 + @enterprise = Enterprise.find(params[:id])
23 23 end
24 24  
25 25 # Make a form to the creation of an eterprise
... ... @@ -47,13 +47,13 @@ class EnterpriseController < ProfileAdminController
47 47  
48 48 # Provides an interface to editing the enterprise details
49 49 def edit
50   - @enterprise = @my_enterprises.find(params[:id])
  50 + @enterprise = @person.enterprises(:id => params[:id])[0]
51 51 @validation_entities = Organization.find(:all) - [@enterprise]
52 52 end
53 53  
54 54 # Saves the changes made in an enterprise
55 55 def update
56   - @enterprise = @my_enterprises.find(params[:id])
  56 + @enterprise = @person.enterprises(:id => params[:id])[0]
57 57 if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info])
58 58 redirect_to :action => 'index'
59 59 else
... ... @@ -66,13 +66,14 @@ class EnterpriseController < ProfileAdminController
66 66 # Make the current user a new member of the enterprise
67 67 def affiliate
68 68 @enterprise = Enterprise.find(params[:id])
69   - @enterprise.people << @person
  69 + member_role = Role.find_by_name('member') || Role.create(:name => 'member')
  70 + @enterprise.affiliate(@person,member_role)
70 71 redirect_to :action => 'index'
71 72 end
72 73  
73 74 # Elimitates the enterprise of the system
74 75 def destroy
75   - @enterprise = @my_enterprises.find(params[:id])
  76 + @enterprise = @person.enterprises(:id => params[:id])[0]
76 77 if @enterprise
77 78 @enterprise.destroy
78 79 else
... ...
app/models/affiliation.rb
... ... @@ -1,5 +0,0 @@
1   -# Affiliation it is a join model to hold affiliations of people to others profiles
2   -class Affiliation < ActiveRecord::Base
3   - belongs_to :person
4   - belongs_to :profile
5   -end
app/models/person.rb
1 1 # A person is the profile of an user holding all relationships with the rest of the system
2 2 class Person < Profile
3   - ENTERPRISE = {:class_name => 'Enterprise', :through => :affiliations, :foreign_key => 'person_id', :source => 'profile'}
4   -
5 3 belongs_to :user
6   - has_many :affiliations, :dependent => :destroy
7   - has_many :profiles, :through => :affiliations
8   - has_many :enterprises, ENTERPRISE
9   - has_many :pending_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', false])
10   - has_many :active_enterprises, ENTERPRISE.merge(:conditions => ['active = ?', true])
11   - has_many :friendships
12   - has_many :friends, :class_name => 'Person', :through => :friendships
13   - has_many :person_friendships
14   - has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
  4 +
  5 +# has_many :friendships
  6 +# has_many :friends, :class_name => 'Person', :through => :friendships
  7 +# has_many :person_friendships
  8 +# has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
  9 +
15 10 has_one :person_info
16 11  
17 12 has_many :role_assignments
  13 + has_many :memberships, :through => :role_assignments, :source => 'resource', :class_name => 'Enterprise'
18 14  
19 15 def has_permission?(perm, res=nil)
20 16 role_assignments.any? {|ra| ra.has_permission?(perm, res)}
21 17 end
22 18  
  19 + def self.conditions_for_profiles(conditions, person)
  20 + new_conditions = sanitize_sql(['role_assignments.person_id = ?', person])
  21 + new_conditions << ' AND ' + sanitize_sql(conditions) unless conditions.blank?
  22 + new_conditions
  23 + end
  24 +
  25 + def profiles(conditions = {})
  26 + Profile.find(
  27 + :all,
  28 + :conditions => self.class.conditions_for_profiles(conditions, self),
  29 + :joins => "LEFT JOIN role_assignments ON profiles.id = role_assignments.resource_id AND role_assignments.resource_type = \"#{Profile.base_class.name}\"",
  30 + :select => 'profiles.*')
  31 + end
  32 +
  33 +
  34 + def enterprises(conditions = {})
  35 + profiles( ({:type => 'Enterprise'}).merge(conditions))
  36 + end
  37 +
  38 + def pending_enterprises
  39 + enterprises :active => false
  40 + end
  41 +
  42 + def active_enterprises
  43 + enterprises :active => true
  44 + end
  45 +
23 46 def info
24 47 person_info
25 48 end
... ...
app/models/profile.rb
... ... @@ -33,10 +33,9 @@ class Profile &lt; ActiveRecord::Base
33 33  
34 34 has_many :domains, :as => :owner
35 35 belongs_to :virtual_community
36   - has_many :affiliations, :dependent => :destroy
37   - has_many :people, :through => :affiliations
38 36  
39   - has_many :role_assignment, :as => :resource
  37 + has_many :role_assignments, :as => :resource
  38 + has_many :people, :through => :role_assignments
40 39  
41 40  
42 41 # Sets the identifier for this profile. Raises an exception when called on a
... ... @@ -103,4 +102,7 @@ class Profile &lt; ActiveRecord::Base
103 102 homepage.children.find(:all, :limit => limit, :order => 'created_on desc')
104 103 end
105 104  
  105 + def affiliate(person, role)
  106 + RoleAssignment.new(:person => person, :role => role, :resource => self).save
  107 + end
106 108 end
... ...
db/migrate/007_create_affiliations.rb
... ... @@ -1,12 +0,0 @@
1   -class CreateAffiliations < ActiveRecord::Migration
2   - def self.up
3   - create_table :affiliations do |t|
4   - t.column :person_id, :integer
5   - t.column :profile_id, :integer
6   - end
7   - end
8   -
9   - def self.down
10   - drop_table :affiliations
11   - end
12   -end
test/fixtures/affiliations.yml
... ... @@ -1,13 +0,0 @@
1   -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2   -one:
3   - id: 1
4   - person_id: 4
5   - profile_id: 5
6   -two:
7   - id: 2
8   - person_id: 1
9   - profile_id: 5
10   -three:
11   - id: 3
12   - person_id: 1
13   - profile_id: 6
test/fixtures/role_assignments.yml
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 2 one:
3 3 id: 1
  4 + person_id: 4
  5 + role_id: 1
  6 + resource_id: 5
  7 + resource_type: 'Profile'
4 8 two:
5 9 id: 2
  10 + person_id: 1
  11 + role_id: 1
  12 + resource_id: 5
  13 + resource_type: 'Profile'
  14 +three:
  15 + id: 3
  16 + person_id: 1
  17 + role_id: 1
  18 + resource_id: 6
  19 + resource_type: 'Profile'
... ...
test/fixtures/roles.yml
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 2 one:
3 3 id: 1
  4 + name: 'member'
4 5 two:
5 6 id: 2
  7 + name: 'owner'
... ...
test/unit/affiliation_test.rb
... ... @@ -1,10 +0,0 @@
1   -require File.dirname(__FILE__) + '/../test_helper'
2   -
3   -class AffiliationTest < Test::Unit::TestCase
4   - fixtures :affiliations
5   -
6   - # Replace this with your real tests.
7   - def test_truth
8   - assert true
9   - end
10   -end
test/unit/person_test.rb
... ... @@ -17,7 +17,8 @@ class PersonTest &lt; Test::Unit::TestCase
17 17 assert pr.save
18 18 pe = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
19 19 assert pe.save
20   - pe.profiles << pr
  20 + member_role = Role.create(:name => 'member')
  21 + pr.affiliate(pe, member_role)
21 22 assert pe.profiles.include?(pr)
22 23 end
23 24  
... ... @@ -26,16 +27,8 @@ class PersonTest &lt; Test::Unit::TestCase
26 27 assert e.save
27 28 p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
28 29 assert p.save
29   - p.profiles << e
30   - assert p.enterprises.include?(e)
31   - end
32   -
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
  30 + member_role = Role.create(:name => 'member')
  31 + e.affiliate(p, member_role)
39 32 assert p.enterprises.include?(e)
40 33 end
41 34  
... ...
test/unit/profile_test.rb
... ... @@ -79,7 +79,10 @@ class ProfileTest &lt; Test::Unit::TestCase
79 79 def test_can_have_affiliated_people
80 80 pr = Profile.create(:name => 'composite_profile', :identifier => 'composite')
81 81 pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person
82   - pr.people << pe
  82 +
  83 + member_role = Role.new(:name => 'member')
  84 + assert member_role.save
  85 + assert pr.affiliate(pe, member_role)
83 86  
84 87 assert pe.profiles.include?(pr)
85 88 end
... ...