Commit f86661c9ecd8b52d233a2436cfedbedc0057f59c
1 parent
cd4a78e9
Exists in
master
and in
29 other branches
ActionItem384: add link Leave to enterprise/community
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1780 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
9 changed files
with
77 additions
and
2 deletions
Show diff stats
app/controllers/my_profile/memberships_controller.rb
... | ... | @@ -14,6 +14,14 @@ class MembershipsController < MyProfileController |
14 | 14 | end |
15 | 15 | end |
16 | 16 | |
17 | + def leave | |
18 | + @to_leave = Profile.find(params[:id]) | |
19 | + if request.post? && params[:confirmation] | |
20 | + @to_leave.remove_member(profile) | |
21 | + redirect_to :action => 'index' | |
22 | + end | |
23 | + end | |
24 | + | |
17 | 25 | def new_community |
18 | 26 | @community = Community.new(params[:community]) |
19 | 27 | if request.post? | ... | ... |
app/models/profile.rb
... | ... | @@ -281,6 +281,10 @@ class Profile < ActiveRecord::Base |
281 | 281 | def add_member(person) |
282 | 282 | self.affiliate(person, Profile::Roles.member) |
283 | 283 | end |
284 | + | |
285 | + def remove_member(person) | |
286 | + self.disaffiliate(person, Profile::Roles.member) | |
287 | + end | |
284 | 288 | |
285 | 289 | # adds a person as administrator os this profile |
286 | 290 | def add_admin(person) | ... | ... |
app/views/memberships/index.rhtml
... | ... | @@ -14,7 +14,8 @@ |
14 | 14 | <%= _('Description: %s') % membership.description + '<br/>' if membership.kind_of?(Community) %> |
15 | 15 | <%= _('Members: %s') % membership.members.size.to_s %> <br/> |
16 | 16 | <%= _('Created at: %s') % show_date(membership.created_at) %> <br/> |
17 | - <%= _('Actions: %s') % link_to(_('Manage'), membership.admin_url) %> <br/> | |
17 | + <%= _('Actions: %s') % link_to(_('Manage'), membership.admin_url) + ', ' + link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership.id }) %> <br/> | |
18 | + | |
18 | 19 | </td> |
19 | 20 | </tr> |
20 | 21 | <% end %> | ... | ... |
... | ... | @@ -0,0 +1,11 @@ |
1 | +<h1><%= _('Leaving %s') % @to_leave.name %></h1> | |
2 | + | |
3 | +<p> | |
4 | +<%= _('Are you sure you want to leave %s?') % @to_leave.name %> | |
5 | +</p> | |
6 | + | |
7 | +<% form_tag do %> | |
8 | + <%= hidden_field_tag(:confirmation, 1) %> | |
9 | + <%= submit_button(:ok, _("Yes, I want to leave.") % @to_leave.name) %> | |
10 | + <%= button(:cancel, _("No, I don't want."), :action => 'index') %> | |
11 | +<% end %> | ... | ... |
test/functional/memberships_controller_test.rb
... | ... | @@ -113,4 +113,30 @@ class MembershipsControllerTest < Test::Unit::TestCase |
113 | 113 | assert_no_tag :tag => 'td', :content => /Description:/ |
114 | 114 | end |
115 | 115 | |
116 | + should 'show link to leave from community' do | |
117 | + community = Community.create!(:name => 'my test community', :description => 'description test') | |
118 | + community.add_member(profile) | |
119 | + get :index, :profile => profile.identifier | |
120 | + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/leave/#{community.id}" }, :content => 'Leave' | |
121 | + end | |
122 | + | |
123 | + should 'present confirmation before leaving a profile' do | |
124 | + community = Community.create!(:name => 'my test community') | |
125 | + get :leave, :profile => profile.identifier, :id => community.id | |
126 | + | |
127 | + assert_response :success | |
128 | + assert_template 'leave' | |
129 | + end | |
130 | + | |
131 | + should 'actually leave profile' do | |
132 | + community = Community.create!(:name => 'my test community') | |
133 | + post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1' | |
134 | + | |
135 | + assert_response :redirect | |
136 | + assert_redirected_to :action => 'index' | |
137 | + | |
138 | + profile.reload | |
139 | + assert !profile.memberships.include?(community) | |
140 | + end | |
141 | + | |
116 | 142 | end | ... | ... |
test/unit/profile_test.rb
... | ... | @@ -456,6 +456,15 @@ class ProfileTest < Test::Unit::TestCase |
456 | 456 | assert_includes Enterprise.find(:all, :within => 2, :origin => [45, 45]), e |
457 | 457 | end |
458 | 458 | |
459 | + should 'allow to remove members' do | |
460 | + c = Profile.create!(:name => 'my other test profile', :identifier => 'myothertestprofile') | |
461 | + p = create_user('myothertestuser').person | |
462 | + | |
463 | + c.add_member(p) | |
464 | + c.remove_member(p) | |
465 | + assert !c.members.include?(p) | |
466 | + end | |
467 | + | |
459 | 468 | private |
460 | 469 | |
461 | 470 | def assert_invalid_identifier(id) | ... | ... |
vendor/plugins/access_control/lib/acts_as_accessible.rb
... | ... | @@ -19,6 +19,11 @@ class ActiveRecord::Base |
19 | 19 | roles.map {|role| accessor.add_role(role, self)}.any? |
20 | 20 | end |
21 | 21 | |
22 | + def disaffiliate(accessor, roles) | |
23 | + roles = [roles] unless roles.kind_of?(Array) | |
24 | + roles.map {|role| accessor.remove_role(role, self)}.any? | |
25 | + end | |
26 | + | |
22 | 27 | def members |
23 | 28 | role_assignments.map(&:accessor).uniq |
24 | 29 | end | ... | ... |
vendor/plugins/access_control/lib/acts_as_accessor.rb
vendor/plugins/access_control/test/acts_as_accessible_test.rb
... | ... | @@ -2,6 +2,7 @@ require 'test/unit' |
2 | 2 | require File.dirname(__FILE__) + '/test_helper' |
3 | 3 | |
4 | 4 | class AccessControlTest < Test::Unit::TestCase |
5 | + | |
5 | 6 | def test_can_have_role_in_respect_to_an_resource |
6 | 7 | r = AccessControlTestResource.create(:name => 'bla') |
7 | 8 | a = AccessControlTestAccessor.create(:name => 'ze') |
... | ... | @@ -9,4 +10,14 @@ class AccessControlTest < Test::Unit::TestCase |
9 | 10 | r.affiliate(a, member_role) |
10 | 11 | assert a.has_permission?('bli', r) |
11 | 12 | end |
13 | + | |
14 | + def test_cant_have_role_in_respect_to_an_resource | |
15 | + r = AccessControlTestResource.create(:name => 'bla') | |
16 | + a = AccessControlTestAccessor.create(:name => 'ze') | |
17 | + member_role = Role.create(:name => 'member', :permissions => ['bli']) | |
18 | + r.affiliate(a, member_role) | |
19 | + r.disaffiliate(a, member_role) | |
20 | + assert !a.has_permission?('bli', r) | |
21 | + end | |
22 | + | |
12 | 23 | end | ... | ... |