Commit e713df48762190e0402655f43ddd9b2e94e497e1
1 parent
cc56cb65
Exists in
master
and in
28 other branches
ActionItem5: changed the way of getting the memberships of a user
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@558 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
20 additions
and
24 deletions
Show diff stats
app/controllers/profile_admin/membership_editor_controller.rb
| 1 | class MembershipEditorController < ProfileAdminController | 1 | class MembershipEditorController < ProfileAdminController |
| 2 | 2 | ||
| 3 | def index | 3 | def index |
| 4 | - @memberships = Profile.find(:all, :include => 'role_assignments', :conditions => ['role_assignments.person_id = ?', current_user.person.id]) | 4 | + @memberships = current_user.person.memberships |
| 5 | end | 5 | end |
| 6 | end | 6 | end |
app/models/person.rb
| @@ -10,7 +10,6 @@ class Person < Profile | @@ -10,7 +10,6 @@ class Person < Profile | ||
| 10 | has_one :person_info | 10 | has_one :person_info |
| 11 | 11 | ||
| 12 | has_many :role_assignments | 12 | has_many :role_assignments |
| 13 | - has_many :memberships, :through => :role_assignments, :source => 'resource', :class_name => 'Enterprise' | ||
| 14 | 13 | ||
| 15 | def has_permission?(perm, res=nil) | 14 | def has_permission?(perm, res=nil) |
| 16 | role_assignments.any? {|ra| ra.has_permission?(perm, res)} | 15 | role_assignments.any? {|ra| ra.has_permission?(perm, res)} |
| @@ -30,27 +29,14 @@ class Person < Profile | @@ -30,27 +29,14 @@ class Person < Profile | ||
| 30 | new_conditions | 29 | new_conditions |
| 31 | end | 30 | end |
| 32 | 31 | ||
| 33 | - def profiles(conditions = {}) | 32 | + def memberships(conditions = {}) |
| 34 | Profile.find( | 33 | Profile.find( |
| 35 | :all, | 34 | :all, |
| 36 | :conditions => self.class.conditions_for_profiles(conditions, self), | 35 | :conditions => self.class.conditions_for_profiles(conditions, self), |
| 37 | :joins => "LEFT JOIN role_assignments ON profiles.id = role_assignments.resource_id AND role_assignments.resource_type = \"#{Profile.base_class.name}\"", | 36 | :joins => "LEFT JOIN role_assignments ON profiles.id = role_assignments.resource_id AND role_assignments.resource_type = \"#{Profile.base_class.name}\"", |
| 38 | :select => 'profiles.*') | 37 | :select => 'profiles.*') |
| 39 | end | 38 | end |
| 40 | - | ||
| 41 | 39 | ||
| 42 | - def enterprises(conditions = {}) | ||
| 43 | - profiles( ({:type => 'Enterprise'}).merge(conditions)) | ||
| 44 | - end | ||
| 45 | - | ||
| 46 | - def pending_enterprises | ||
| 47 | - enterprises :active => false | ||
| 48 | - end | ||
| 49 | - | ||
| 50 | - def active_enterprises | ||
| 51 | - enterprises :active => true | ||
| 52 | - end | ||
| 53 | - | ||
| 54 | def info | 40 | def info |
| 55 | person_info | 41 | person_info |
| 56 | end | 42 | end |
test/unit/person_test.rb
| @@ -19,7 +19,7 @@ class PersonTest < Test::Unit::TestCase | @@ -19,7 +19,7 @@ class PersonTest < Test::Unit::TestCase | ||
| 19 | assert pe.save | 19 | assert pe.save |
| 20 | member_role = Role.create(:name => 'member') | 20 | member_role = Role.create(:name => 'member') |
| 21 | pr.affiliate(pe, member_role) | 21 | pr.affiliate(pe, member_role) |
| 22 | - assert pe.profiles.include?(pr) | 22 | + assert pe.memberships.include?(pr) |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | def test_can_belongs_to_an_enterprise | 25 | def test_can_belongs_to_an_enterprise |
| @@ -29,7 +29,7 @@ class PersonTest < Test::Unit::TestCase | @@ -29,7 +29,7 @@ class PersonTest < Test::Unit::TestCase | ||
| 29 | assert p.save | 29 | assert p.save |
| 30 | member_role = Role.create(:name => 'member') | 30 | member_role = Role.create(:name => 'member') |
| 31 | e.affiliate(p, member_role) | 31 | e.affiliate(p, member_role) |
| 32 | - assert p.enterprises.include?(e) | 32 | + assert p.memberships.include?(e) |
| 33 | end | 33 | end |
| 34 | 34 | ||
| 35 | def test_can_have_user | 35 | def test_can_have_user |
| @@ -65,15 +65,25 @@ class PersonTest < Test::Unit::TestCase | @@ -65,15 +65,25 @@ class PersonTest < Test::Unit::TestCase | ||
| 65 | end | 65 | end |
| 66 | 66 | ||
| 67 | should 'change the roles of the user' do | 67 | should 'change the roles of the user' do |
| 68 | - assert p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person | ||
| 69 | - assert e = Enterprise.create(:identifier => 'enter') | ||
| 70 | - assert r1 = Role.create(:name => 'associate') | 68 | + p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person |
| 69 | + e = Enterprise.create(:identifier => 'enter', :name => 'Enter') | ||
| 70 | + r1 = Role.create(:name => 'associate') | ||
| 71 | assert e.affiliate(p, r1) | 71 | assert e.affiliate(p, r1) |
| 72 | - assert r2 = Role.create(:name => 'partner') | 72 | + r2 = Role.create(:name => 'partner') |
| 73 | assert p.define_roles([r2], e) | 73 | assert p.define_roles([r2], e) |
| 74 | - p = Person.find(p.id) | 74 | + p.reload |
| 75 | assert p.role_assignments.any? {|ra| ra.role == r2} | 75 | assert p.role_assignments.any? {|ra| ra.role == r2} |
| 76 | assert !p.role_assignments.any? {|ra| ra.role == r1} | 76 | assert !p.role_assignments.any? {|ra| ra.role == r1} |
| 77 | end | 77 | end |
| 78 | 78 | ||
| 79 | + should 'report that the user has the permission' do | ||
| 80 | + p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person | ||
| 81 | + r = Role.create(:name => 'associate', :permissions => ['edit_profile']) | ||
| 82 | + e = Enterprise.create(:identifier => 'enterpri', :name => 'Enterpri') | ||
| 83 | + assert e.affiliate(p, r) | ||
| 84 | + assert p.reload | ||
| 85 | + assert e.reload | ||
| 86 | + assert p.has_permission?('edit_profile', e) | ||
| 87 | + assert !p.has_permission?('destroy_profile', e) | ||
| 88 | + end | ||
| 79 | end | 89 | end |
test/unit/profile_test.rb
| @@ -84,7 +84,7 @@ class ProfileTest < Test::Unit::TestCase | @@ -84,7 +84,7 @@ class ProfileTest < Test::Unit::TestCase | ||
| 84 | assert member_role.save | 84 | assert member_role.save |
| 85 | assert pr.affiliate(pe, member_role) | 85 | assert pr.affiliate(pe, member_role) |
| 86 | 86 | ||
| 87 | - assert pe.profiles.include?(pr) | 87 | + assert pe.memberships.include?(pr) |
| 88 | end | 88 | end |
| 89 | 89 | ||
| 90 | def test_search | 90 | def test_search |