Commit f688afe0c4746433a8d54ff84587ae99a5aa7b3b

Authored by JoenioCosta
1 parent 90f24f9a

ActionItem568: owner can destroy community

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2295 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/memberships_controller.rb
... ... @@ -33,4 +33,14 @@ class MembershipsController < MyProfileController
33 33 end
34 34 end
35 35  
  36 + def destroy_community
  37 + @community = Community.find(params[:id])
  38 + if request.post?
  39 + if @community.destroy
  40 + flash[:notice] = _('%s was destroyed!') % @community.display_name
  41 + redirect_to :action => 'index'
  42 + end
  43 + end
  44 + end
  45 +
36 46 end
... ...
app/views/memberships/destroy_community.rhtml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +<h1><%= _('Destroy %s') % @community.display_name %></h1>
  2 +
  3 +<p><strong><%= _('Are you sure you want to destroy %s?') % @community.display_name %></strong></p>
  4 +
  5 +<% form_tag do %>
  6 + <%= hidden_field_tag(:confirmation, 1) %>
  7 + <%= submit_button(:ok, _("Yes, I want to destroy.") % @community.display_name) %>
  8 + <%= button(:cancel, _("No, I don't want."), :action => 'index') %>
  9 +<% end %>
... ...
app/views/memberships/index.rhtml
... ... @@ -12,13 +12,13 @@
12 12 <strong><%= membership.display_name %></strong><br/>
13 13 <%= _('Role: %s') % rolename_for(profile, membership) %> <br/>
14 14 <%= _('Type: %s') % _(membership.class.name) %> <br/>
15   - <%= _('Description: %s') % membership.description + '<br/>' if membership.kind_of?(Community) %>
  15 + <%= _('Description: %s') % membership.description + '<br/>' if membership.community? %>
16 16 <%= _('Members: %s') % membership.members.size.to_s %> <br/>
17 17 <%= _('Created at: %s') % show_date(membership.created_at) %> <br/>
18 18 <%= [ link_to(_('Manage'), membership.admin_url),
19   - link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership.id }),
20   - link_to(_('Destroy'), { :profile => profile.identifier, :controller => 'memberships', :action => 'destroy', :id => membership.id })
21   - ].join(', ')
  19 + link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership }),
  20 + (membership.community? && user.has_permission?(:destroy_profile, membership) ? link_to(_('Destroy'), { :action => 'destroy_community', :id => membership }) : nil)
  21 + ].compact.join(', ')
22 22 %>
23 23 </span>
24 24 </li>
... ...
test/fixtures/roles.yml
... ... @@ -36,6 +36,7 @@ profile_admin:
36 36 permissions:
37 37 - edit_profile_design
38 38 - moderate_comments
  39 + - destroy_profile
39 40 profile_member:
40 41 id: 6
41 42 key: 'profile_member'
... ...
test/functional/memberships_controller_test.rb
... ... @@ -171,4 +171,48 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
171 171 assert_tag :tag => 'a', :content => 'Register a new Enterprise'
172 172 end
173 173  
  174 + should 'render destroy_community template' do
  175 + community = Community.create!(:name => 'A community to destroy')
  176 + get :destroy_community, :profile => 'testuser', :id => community.id
  177 + assert_template 'destroy_community'
  178 + end
  179 +
  180 + should 'display destroy link only to communities' do
  181 + community = Community.create!(:name => 'A community to destroy')
  182 + enterprise = Enterprise.create!(:name => 'A enterprise test', :identifier => 'enterprise-test')
  183 +
  184 + person = Person['testuser']
  185 + community.add_admin(person)
  186 + enterprise.add_admin(person)
  187 +
  188 + get :index, :profile => 'testuser'
  189 +
  190 + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" }
  191 + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{enterprise.id}" }
  192 + end
  193 +
  194 + should 'be able to destroy communities' do
  195 + community = Community.create!(:name => 'A community to destroy')
  196 +
  197 + person = Person['testuser']
  198 + community.add_admin(person)
  199 +
  200 + assert_difference Community, :count, -1 do
  201 + post :destroy_community, :profile => 'testuser', :id => community.id
  202 + end
  203 + end
  204 +
  205 + should 'not display destroy link to normal members' do
  206 + community = Community.create!(:name => 'A community to destroy')
  207 +
  208 + person = Person['testuser']
  209 + community.add_member(person)
  210 +
  211 + login_as('testuser')
  212 + get :index, :profile => 'testuser'
  213 +
  214 + assert_template 'index'
  215 + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" }
  216 + end
  217 +
174 218 end
... ...
test/unit/community_test.rb
... ... @@ -64,4 +64,23 @@ class CommunityTest &lt; Test::Unit::TestCase
64 64 assert_not_includes c.members, p
65 65 end
66 66  
  67 + should 'clear relationships after destroy' do
  68 + c = Community.create!(:name => 'my test profile', :identifier => 'mytestprofile')
  69 + member = create_user('memberuser').person
  70 + admin = create_user('adminuser').person
  71 + moderator = create_user('moderatoruser').person
  72 +
  73 + c.add_member(member)
  74 + c.add_admin(admin)
  75 + c.add_moderator(moderator)
  76 +
  77 + relationships = c.role_assignments
  78 + assert_not_nil relationships
  79 +
  80 + c.destroy
  81 + relationships.each do |i|
  82 + assert !RoleAssignment.exists?(i.id)
  83 + end
  84 + end
  85 +
67 86 end
... ...