Commit d3bae4732e29d4e29257349674a6c30887afe6f3

Authored by Joenio Costa
Committed by Antonio Terceiro
1 parent e6c801d6

ActionItem1021: "Page not exists" when exit from community on another environment

* move leave logic to profile_controller
 * use lightbox to leave confirmation, like join
 * store location to remember to where redirect
app/controllers/my_profile/memberships_controller.rb
@@ -6,19 +6,6 @@ class MembershipsController < MyProfileController @@ -6,19 +6,6 @@ class MembershipsController < MyProfileController
6 @memberships = profile.memberships 6 @memberships = profile.memberships
7 end 7 end
8 8
9 - def leave  
10 - @to_leave = Profile.find(params[:id])  
11 - @wizard = params[:wizard]  
12 - if request.post? && params[:confirmation]  
13 - @to_leave.remove_member(profile)  
14 - if @wizard  
15 - redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true  
16 - else  
17 - redirect_to :action => 'index'  
18 - end  
19 - end  
20 - end  
21 -  
22 def new_community 9 def new_community
23 @community = Community.new(params[:community]) 10 @community = Community.new(params[:community])
24 @wizard = params[:wizard].blank? ? false : params[:wizard] 11 @wizard = params[:wizard].blank? ? false : params[:wizard]
app/controllers/public/profile_controller.rb
@@ -2,7 +2,7 @@ class ProfileController < PublicController @@ -2,7 +2,7 @@ class ProfileController < PublicController
2 2
3 needs_profile 3 needs_profile
4 before_filter :check_access_to_profile 4 before_filter :check_access_to_profile
5 - before_filter :login_required, :only => [:join, :refuse_join] 5 + before_filter :login_required, :only => [:join, :refuse_join, :leave]
6 6
7 helper TagsHelper 7 helper TagsHelper
8 8
@@ -55,9 +55,27 @@ class ProfileController < PublicController @@ -55,9 +55,27 @@ class ProfileController < PublicController
55 if @wizard 55 if @wizard
56 redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true 56 redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
57 else 57 else
58 - redirect_to profile.url 58 + redirect_back_or_default profile.url
59 end 59 end
60 else 60 else
  61 + store_location(request.referer)
  62 + if request.xhr?
  63 + render :layout => false
  64 + end
  65 + end
  66 + end
  67 +
  68 + def leave
  69 + @wizard = params[:wizard]
  70 + if request.post? && params[:confirmation]
  71 + profile.remove_member(current_user.person)
  72 + if @wizard
  73 + redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
  74 + else
  75 + redirect_back_or_default profile.url
  76 + end
  77 + else
  78 + store_location(request.referer)
61 if request.xhr? 79 if request.xhr?
62 render :layout => false 80 render :layout => false
63 end 81 end
app/models/profile.rb
@@ -350,6 +350,14 @@ class Profile < ActiveRecord::Base @@ -350,6 +350,14 @@ class Profile < ActiveRecord::Base
350 { :profile => identifier, :controller => 'profile_editor', :action => 'index' } 350 { :profile => identifier, :controller => 'profile_editor', :action => 'index' }
351 end 351 end
352 352
  353 + def leave_url
  354 + { :profile => identifier, :controller => 'profile', :action => 'leave' }
  355 + end
  356 +
  357 + def join_url
  358 + { :profile => identifier, :controller => 'profile', :action => 'join' }
  359 + end
  360 +
353 def public_profile_url 361 def public_profile_url
354 generate_url(:profile => identifier, :controller => 'profile', :action => 'index') 362 generate_url(:profile => identifier, :controller => 'profile', :action => 'index')
355 end 363 end
app/views/account/_profile_details.rhtml
@@ -7,11 +7,11 @@ @@ -7,11 +7,11 @@
7 <%= _('Members: %s') % @profile.members.size.to_s %> <br/> 7 <%= _('Members: %s') % @profile.members.size.to_s %> <br/>
8 <%= _('Created at: %s') % show_date(@profile.created_at) %> <br/> 8 <%= _('Created at: %s') % show_date(@profile.created_at) %> <br/>
9 9
10 -<% form_tag({ :profile => @profile.identifier, :controller => 'profile', :action => 'join'}) do %> 10 +<% form_tag(@profile.join_url) do %>
11 <%= hidden_field_tag(:confirmation, 1) %> 11 <%= hidden_field_tag(:confirmation, 1) %>
12 <%= hidden_field_tag(:wizard, true) %> 12 <%= hidden_field_tag(:wizard, true) %>
13 <% if @profile.members.include?(user) %> 13 <% if @profile.members.include?(user) %>
14 - <%= link_to( _('Leave'), { :profile => user.identifier, :controller => 'memberships', :action => 'leave', :id => @profile.id, :confirmation => 1, :wizard => true }, :method => 'post') %> 14 + <%= link_to( _('Leave'), @profile.leave_url.merge(:confirmation => 1, :wizard => true }, :method => 'post') %>
15 <% else %> 15 <% else %>
16 <%= submit_button(:ok, _("Join now")) %> 16 <%= submit_button(:ok, _("Join now")) %>
17 <% end %> 17 <% end %>
app/views/blocks/profile_info_actions/community.rhtml
@@ -3,11 +3,11 @@ @@ -3,11 +3,11 @@
3 3
4 <% if profile.members.include?(user) %> 4 <% if profile.members.include?(user) %>
5 <li> 5 <li>
6 - <%= link_to content_tag('span', _('Leave')), { :profile => user.identifier, :controller => 'memberships', :action => 'leave', :id => profile.id }, :class => 'button with-text icon-delete', :title => _('Leave this community') %> 6 + <%= lightbox_link_to content_tag('span', _('Leave')), profile.leave_url, :class => 'button with-text icon-delete', :title => _('Leave this community') %>
7 </li> 7 </li>
8 <% else %> 8 <% else %>
9 <li> 9 <li>
10 - <%= lightbox_link_to content_tag('span', _('Join')), { :profile => profile.identifier, :controller => 'profile', :action => 'join' }, :class => 'button with-text icon-add', :title => _('Join this community') %> 10 + <%= lightbox_link_to content_tag('span', _('Join')), profile.join_url, :class => 'button with-text icon-add', :title => _('Join this community') %>
11 </li> 11 </li>
12 <% end %> 12 <% end %>
13 13
app/views/memberships/index.rhtml
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 <%= _('Members: %s') % membership.members.size.to_s %> <br/> 21 <%= _('Members: %s') % membership.members.size.to_s %> <br/>
22 <%= _('Created at: %s') % show_date(membership.created_at) unless membership.enterprise? %> <br/> 22 <%= _('Created at: %s') % show_date(membership.created_at) unless membership.enterprise? %> <br/>
23 <%= [ link_to(_('Manage'), membership.admin_url), 23 <%= [ link_to(_('Manage'), membership.admin_url),
24 - link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership }), 24 + lightbox_link_to(_('Leave'), membership.leave_url),
25 (membership.community? && user.has_permission?(:destroy_profile, membership) ? link_to(_('Remove'), { :action => 'destroy_community', :id => membership }) : nil) 25 (membership.community? && user.has_permission?(:destroy_profile, membership) ? link_to(_('Remove'), { :action => 'destroy_community', :id => membership }) : nil)
26 ].compact.join(', ') 26 ].compact.join(', ')
27 %> 27 %>
app/views/memberships/leave.rhtml
@@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
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 %>  
app/views/profile/leave.rhtml 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +<h1><%= _('Leaving %s') % profile.name %></h1>
  2 +
  3 +<p>
  4 +<%= _('Are you sure you want to leave %s?') % profile.name %>
  5 +</p>
  6 +
  7 +<% form_tag do %>
  8 + <%= hidden_field_tag(:confirmation, 1) %>
  9 + <%= submit_button(:ok, _("Yes, I want to leave.") % profile.name) %>
  10 + <% if logged_in? && request.xhr? %>
  11 + <%= lightbox_close_button(_("No, I don't want")) %>
  12 + <% else %>
  13 + <%= button(:cancel, _("No, I don't want."), profile.url) %>
  14 + <% end %>
  15 +<% end %>
app/views/shared/join_community_popup.rhtml
@@ -3,9 +3,9 @@ @@ -3,9 +3,9 @@
3 3
4 <% button_bar do %> 4 <% button_bar do %>
5 <% if logged_in? %> 5 <% if logged_in? %>
6 - <%= button(:ok, _('Yes'), { :controller => 'profile', :action => 'join', :profile => profile.identifier, :confirmation => '1'}, :method => :post) %> 6 + <%= button(:ok, _('Yes'), profile.join_url.merge(:confirmation => '1'), :method => :post) %>
7 <% else %> 7 <% else %>
8 - <%= button(:ok, _('Yes'), :controller => 'profile', :action => 'join', :profile => profile.identifier) %> 8 + <%= button(:ok, _('Yes'), profile.join_url) %>
9 <% end %> 9 <% end %>
10 <%= button_to_remote(:cancel, _('Not now'), :url => profile.url.merge({:controller => 'profile', :action => 'refuse_for_now', :profile => profile.identifier}), :loaded => '$("join-community-popup").hide()') %> 10 <%= button_to_remote(:cancel, _('Not now'), :url => profile.url.merge({:controller => 'profile', :action => 'refuse_for_now', :profile => profile.identifier}), :loaded => '$("join-community-popup").hide()') %>
11 <%= button(:cancel, _('No and don\'t ask again'), :controller => 'profile', :action => 'refuse_join', :profile => profile.identifier) if logged_in? %> 11 <%= button(:cancel, _('No and don\'t ask again'), :controller => 'profile', :action => 'refuse_join', :profile => profile.identifier) if logged_in? %>
lib/authenticated_system.rb
@@ -79,8 +79,8 @@ module AuthenticatedSystem @@ -79,8 +79,8 @@ module AuthenticatedSystem
79 # Store the URI of the current request in the session. 79 # Store the URI of the current request in the session.
80 # 80 #
81 # We can return to this location by calling #redirect_back_or_default. 81 # We can return to this location by calling #redirect_back_or_default.
82 - def store_location  
83 - session[:return_to] = request.request_uri 82 + def store_location(location = request.request_uri)
  83 + session[:return_to] = location
84 end 84 end
85 85
86 # Redirect to the URI stored by the most recent store_location call or 86 # Redirect to the URI stored by the most recent store_location call or
test/functional/cms_controller_test.rb
@@ -1121,4 +1121,5 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -1121,4 +1121,5 @@ class CmsControllerTest &lt; Test::Unit::TestCase
1121 get :view, :profile => profile.identifier, :id => profile.blog.id 1121 get :view, :profile => profile.identifier, :id => profile.blog.id
1122 assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/upload_files?parent_id=#{profile.blog.id}"} 1122 assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/upload_files?parent_id=#{profile.blog.id}"}
1123 end 1123 end
  1124 +
1124 end 1125 end
test/functional/memberships_controller_test.rb
@@ -109,41 +109,7 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase @@ -109,41 +109,7 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
109 community = Community.create!(:name => 'my test community', :description => 'description test') 109 community = Community.create!(:name => 'my test community', :description => 'description test')
110 community.add_member(profile) 110 community.add_member(profile)
111 get :index, :profile => profile.identifier 111 get :index, :profile => profile.identifier
112 - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/leave/#{community.id}" }, :content => 'Leave'  
113 - end  
114 -  
115 - should 'present confirmation before leaving a profile' do  
116 - community = Community.create!(:name => 'my test community')  
117 - community.add_member(profile)  
118 - get :leave, :profile => profile.identifier, :id => community.id  
119 -  
120 - assert_response :success  
121 - assert_template 'leave'  
122 - end  
123 -  
124 - should 'actually leave profile' do  
125 - community = Community.create!(:name => 'my test community')  
126 - community.add_member(profile)  
127 - assert_includes profile.memberships, community  
128 - post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1'  
129 -  
130 - assert_response :redirect  
131 - assert_redirected_to :action => 'index'  
132 -  
133 - profile = Profile.find(@profile.id)  
134 - assert_not_includes profile.memberships, community  
135 - end  
136 -  
137 - should 'leave profile when on wizard' do  
138 - community = Community.create!(:name => 'my test community')  
139 - community.add_member(profile)  
140 - post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1', :wizard => true  
141 -  
142 - assert_response :redirect  
143 - assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true  
144 -  
145 - profile = Profile.find(@profile.id)  
146 - assert_not_includes profile.memberships, community 112 + assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/leave" }, :content => 'Leave'
147 end 113 end
148 114
149 should 'current user is added as admin after create new community' do 115 should 'current user is added as admin after create new community' do
test/functional/profile_controller_test.rb
@@ -12,6 +12,7 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -12,6 +12,7 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
12 12
13 @profile = create_user('testuser').person 13 @profile = create_user('testuser').person
14 end 14 end
  15 + attr_reader :profile
15 16
16 def test_local_files_reference 17 def test_local_files_reference
17 assert_local_files_reference 18 assert_local_files_reference
@@ -224,7 +225,8 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -224,7 +225,8 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
224 community = Community.create!(:name => 'my test community') 225 community = Community.create!(:name => 'my test community')
225 community.add_member(@profile) 226 community.add_member(@profile)
226 get :index, :profile => community.identifier 227 get :index, :profile => community.identifier
227 - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/leave/#{community.id}" } 228 + assert_tag :tag => 'a',
  229 + :attributes => { :href => "/profile/#{community.identifier}/leave" }
228 end 230 end
229 231
230 should 'not show Leave This Community button for non-registered users' do 232 should 'not show Leave This Community button for non-registered users' do
@@ -510,4 +512,126 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -510,4 +512,126 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
510 assert_equal ((2..10).to_a + [community.id]), @request.session[:no_asking] 512 assert_equal ((2..10).to_a + [community.id]), @request.session[:no_asking]
511 end 513 end
512 514
  515 + should 'present confirmation before leaving a profile' do
  516 + community = Community.create!(:name => 'my test community')
  517 + community.add_member(profile)
  518 +
  519 + login_as(profile.identifier)
  520 + get :leave, :profile => community.identifier
  521 +
  522 + assert_template 'leave'
  523 + assert_tag :tag => 'input', :attributes => {:value => 'Yes, I want to leave.', :type => 'submit'}
  524 + end
  525 +
  526 + should 'actually leave profile' do
  527 + community = Community.create!(:name => 'my test community')
  528 + community.add_member(profile)
  529 + assert_includes profile.memberships, community
  530 +
  531 + login_as(profile.identifier)
  532 + post :leave, :profile => community.identifier, :confirmation => '1'
  533 +
  534 + profile = Profile.find(@profile.id)
  535 + assert_not_includes profile.memberships, community
  536 + end
  537 +
  538 + should 'leave profile when on wizard' do
  539 + community = Community.create!(:name => 'my test community')
  540 + community.add_member(profile)
  541 +
  542 + login_as(profile.identifier)
  543 + post :leave, :profile => community.identifier, :confirmation => '1', :wizard => true
  544 +
  545 + assert_response :redirect
  546 + assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
  547 +
  548 + profile = Profile.find(@profile.id)
  549 + assert_not_includes profile.memberships, community
  550 + end
  551 +
  552 + should "offer button to close 'leave community' lightbox" do
  553 + community = Community.create!(:name => 'my test community')
  554 + community.add_member(profile)
  555 +
  556 + login_as(profile.identifier)
  557 + get :index, :profile => community.identifier
  558 +
  559 + assert_tag :tag => 'a', :content => 'Leave', :attributes => { :href => "/profile/#{community.identifier}/leave", :class => /^lbOn/ }
  560 + end
  561 +
  562 + should 'offer button to cancel leaving community' do
  563 + community = Community.create!(:name => 'my test community')
  564 + community.add_member(profile)
  565 +
  566 + login_as(profile.identifier)
  567 + get :leave, :profile => community.identifier
  568 +
  569 + assert_tag :tag => 'a', :content => "No, I don't want."
  570 + end
  571 +
  572 + should 'render without layout when use lightbox to leave community' do
  573 + community = Community.create!(:name => 'my test community')
  574 + community.add_member(profile)
  575 +
  576 + @request.stubs(:xhr?).returns(true)
  577 + login_as(profile.identifier)
  578 + get :leave, :profile => community.identifier
  579 +
  580 + assert_no_tag :tag => 'body' # e.g. no layout
  581 + end
  582 +
  583 + should 'require login to leave community' do
  584 + community = Community.create!(:name => 'my test community')
  585 + get :leave, :profile => community.identifier
  586 +
  587 + assert_redirected_to :controller => 'account', :action => 'login'
  588 + end
  589 +
  590 + should 'redirect to stored location after leave community' do
  591 + community = Community.create!(:name => 'my test community')
  592 + community.add_member(profile)
  593 +
  594 + @request.session[:return_to] = "/profile/#{community.identifier}/to_go"
  595 + login_as(profile.identifier)
  596 +
  597 + post :leave, :profile => community.identifier, :confirmation => '1'
  598 +
  599 + assert_redirected_to "/profile/#{community.identifier}/to_go"
  600 + end
  601 +
  602 + should 'store referer location when request leave via get' do
  603 + community = Community.create!(:name => 'my test community')
  604 + login_as(profile.identifier)
  605 +
  606 + assert_nil @request.session[:return_to]
  607 + @request.expects(:referer).returns("/profile/redirect_to")
  608 +
  609 + get :leave, :profile => community.identifier
  610 +
  611 + assert_equal '/profile/redirect_to', @request.session[:return_to]
  612 + end
  613 +
  614 + should 'store referer location when request join via get' do
  615 + community = Community.create!(:name => 'my test community')
  616 + login_as(profile.identifier)
  617 +
  618 + assert_nil @request.session[:return_to]
  619 + @request.expects(:referer).returns("/profile/redirect_to")
  620 +
  621 + get :join, :profile => community.identifier
  622 +
  623 + assert_equal '/profile/redirect_to', @request.session[:return_to]
  624 + end
  625 +
  626 + should 'redirect to stored location after join community' do
  627 + community = Community.create!(:name => 'my test community')
  628 +
  629 + @request.session[:return_to] = "/profile/#{community.identifier}/to_go"
  630 + login_as(profile.identifier)
  631 +
  632 + post :join, :profile => community.identifier, :confirmation => '1'
  633 +
  634 + assert_redirected_to "/profile/#{community.identifier}/to_go"
  635 + end
  636 +
513 end 637 end
test/unit/profile_test.rb
@@ -1370,6 +1370,16 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -1370,6 +1370,16 @@ class ProfileTest &lt; Test::Unit::TestCase
1370 assert_equal "header customized", person.custom_header 1370 assert_equal "header customized", person.custom_header
1371 end 1371 end
1372 1372
  1373 + should 'provide URL to leave' do
  1374 + profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id)
  1375 + assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'leave'}, profile.leave_url)
  1376 + end
  1377 +
  1378 + should 'provide URL to join' do
  1379 + profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id)
  1380 + assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'join'}, profile.join_url)
  1381 + end
  1382 +
1373 private 1383 private
1374 1384
1375 def assert_invalid_identifier(id) 1385 def assert_invalid_identifier(id)