From d3bae4732e29d4e29257349674a6c30887afe6f3 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Mon, 20 Jul 2009 17:33:55 -0300 Subject: [PATCH] ActionItem1021: "Page not exists" when exit from community on another environment --- app/controllers/my_profile/memberships_controller.rb | 13 ------------- app/controllers/public/profile_controller.rb | 22 ++++++++++++++++++++-- app/models/profile.rb | 8 ++++++++ app/views/account/_profile_details.rhtml | 4 ++-- app/views/blocks/profile_info_actions/community.rhtml | 4 ++-- app/views/memberships/index.rhtml | 2 +- app/views/memberships/leave.rhtml | 11 ----------- app/views/profile/leave.rhtml | 15 +++++++++++++++ app/views/shared/join_community_popup.rhtml | 4 ++-- lib/authenticated_system.rb | 4 ++-- test/functional/cms_controller_test.rb | 1 + test/functional/memberships_controller_test.rb | 36 +----------------------------------- test/functional/profile_controller_test.rb | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- test/unit/profile_test.rb | 10 ++++++++++ 14 files changed, 189 insertions(+), 71 deletions(-) delete mode 100644 app/views/memberships/leave.rhtml create mode 100644 app/views/profile/leave.rhtml diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index b8a1334..c3d34a9 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -6,19 +6,6 @@ class MembershipsController < MyProfileController @memberships = profile.memberships end - def leave - @to_leave = Profile.find(params[:id]) - @wizard = params[:wizard] - if request.post? && params[:confirmation] - @to_leave.remove_member(profile) - if @wizard - redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true - else - redirect_to :action => 'index' - end - end - end - def new_community @community = Community.new(params[:community]) @wizard = params[:wizard].blank? ? false : params[:wizard] diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 7c23c83..ef80e92 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -2,7 +2,7 @@ class ProfileController < PublicController needs_profile before_filter :check_access_to_profile - before_filter :login_required, :only => [:join, :refuse_join] + before_filter :login_required, :only => [:join, :refuse_join, :leave] helper TagsHelper @@ -55,9 +55,27 @@ class ProfileController < PublicController if @wizard redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true else - redirect_to profile.url + redirect_back_or_default profile.url end else + store_location(request.referer) + if request.xhr? + render :layout => false + end + end + end + + def leave + @wizard = params[:wizard] + if request.post? && params[:confirmation] + profile.remove_member(current_user.person) + if @wizard + redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true + else + redirect_back_or_default profile.url + end + else + store_location(request.referer) if request.xhr? render :layout => false end diff --git a/app/models/profile.rb b/app/models/profile.rb index 5ed0956..cb9a51c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -350,6 +350,14 @@ class Profile < ActiveRecord::Base { :profile => identifier, :controller => 'profile_editor', :action => 'index' } end + def leave_url + { :profile => identifier, :controller => 'profile', :action => 'leave' } + end + + def join_url + { :profile => identifier, :controller => 'profile', :action => 'join' } + end + def public_profile_url generate_url(:profile => identifier, :controller => 'profile', :action => 'index') end diff --git a/app/views/account/_profile_details.rhtml b/app/views/account/_profile_details.rhtml index 95a0a0d..43ce7e4 100644 --- a/app/views/account/_profile_details.rhtml +++ b/app/views/account/_profile_details.rhtml @@ -7,11 +7,11 @@ <%= _('Members: %s') % @profile.members.size.to_s %>
<%= _('Created at: %s') % show_date(@profile.created_at) %>
-<% form_tag({ :profile => @profile.identifier, :controller => 'profile', :action => 'join'}) do %> +<% form_tag(@profile.join_url) do %> <%= hidden_field_tag(:confirmation, 1) %> <%= hidden_field_tag(:wizard, true) %> <% if @profile.members.include?(user) %> - <%= link_to( _('Leave'), { :profile => user.identifier, :controller => 'memberships', :action => 'leave', :id => @profile.id, :confirmation => 1, :wizard => true }, :method => 'post') %> + <%= link_to( _('Leave'), @profile.leave_url.merge(:confirmation => 1, :wizard => true }, :method => 'post') %> <% else %> <%= submit_button(:ok, _("Join now")) %> <% end %> diff --git a/app/views/blocks/profile_info_actions/community.rhtml b/app/views/blocks/profile_info_actions/community.rhtml index 3a2e8e5..6f1951a 100644 --- a/app/views/blocks/profile_info_actions/community.rhtml +++ b/app/views/blocks/profile_info_actions/community.rhtml @@ -3,11 +3,11 @@ <% if profile.members.include?(user) %>
  • - <%= 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') %> + <%= lightbox_link_to content_tag('span', _('Leave')), profile.leave_url, :class => 'button with-text icon-delete', :title => _('Leave this community') %>
  • <% else %>
  • - <%= lightbox_link_to content_tag('span', _('Join')), { :profile => profile.identifier, :controller => 'profile', :action => 'join' }, :class => 'button with-text icon-add', :title => _('Join this community') %> + <%= lightbox_link_to content_tag('span', _('Join')), profile.join_url, :class => 'button with-text icon-add', :title => _('Join this community') %>
  • <% end %> diff --git a/app/views/memberships/index.rhtml b/app/views/memberships/index.rhtml index 3fba821..c94c1e9 100644 --- a/app/views/memberships/index.rhtml +++ b/app/views/memberships/index.rhtml @@ -21,7 +21,7 @@ <%= _('Members: %s') % membership.members.size.to_s %>
    <%= _('Created at: %s') % show_date(membership.created_at) unless membership.enterprise? %>
    <%= [ link_to(_('Manage'), membership.admin_url), - link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership }), + lightbox_link_to(_('Leave'), membership.leave_url), (membership.community? && user.has_permission?(:destroy_profile, membership) ? link_to(_('Remove'), { :action => 'destroy_community', :id => membership }) : nil) ].compact.join(', ') %> diff --git a/app/views/memberships/leave.rhtml b/app/views/memberships/leave.rhtml deleted file mode 100644 index 2bff7d8..0000000 --- a/app/views/memberships/leave.rhtml +++ /dev/null @@ -1,11 +0,0 @@ -

    <%= _('Leaving %s') % @to_leave.name %>

    - -

    -<%= _('Are you sure you want to leave %s?') % @to_leave.name %> -

    - -<% form_tag do %> - <%= hidden_field_tag(:confirmation, 1) %> - <%= submit_button(:ok, _("Yes, I want to leave.") % @to_leave.name) %> - <%= button(:cancel, _("No, I don't want."), :action => 'index') %> -<% end %> diff --git a/app/views/profile/leave.rhtml b/app/views/profile/leave.rhtml new file mode 100644 index 0000000..bd3937c --- /dev/null +++ b/app/views/profile/leave.rhtml @@ -0,0 +1,15 @@ +

    <%= _('Leaving %s') % profile.name %>

    + +

    +<%= _('Are you sure you want to leave %s?') % profile.name %> +

    + +<% form_tag do %> + <%= hidden_field_tag(:confirmation, 1) %> + <%= submit_button(:ok, _("Yes, I want to leave.") % profile.name) %> + <% if logged_in? && request.xhr? %> + <%= lightbox_close_button(_("No, I don't want")) %> + <% else %> + <%= button(:cancel, _("No, I don't want."), profile.url) %> + <% end %> +<% end %> diff --git a/app/views/shared/join_community_popup.rhtml b/app/views/shared/join_community_popup.rhtml index 56540d8..0e131c2 100644 --- a/app/views/shared/join_community_popup.rhtml +++ b/app/views/shared/join_community_popup.rhtml @@ -3,9 +3,9 @@ <% button_bar do %> <% if logged_in? %> - <%= button(:ok, _('Yes'), { :controller => 'profile', :action => 'join', :profile => profile.identifier, :confirmation => '1'}, :method => :post) %> + <%= button(:ok, _('Yes'), profile.join_url.merge(:confirmation => '1'), :method => :post) %> <% else %> - <%= button(:ok, _('Yes'), :controller => 'profile', :action => 'join', :profile => profile.identifier) %> + <%= button(:ok, _('Yes'), profile.join_url) %> <% end %> <%= button_to_remote(:cancel, _('Not now'), :url => profile.url.merge({:controller => 'profile', :action => 'refuse_for_now', :profile => profile.identifier}), :loaded => '$("join-community-popup").hide()') %> <%= button(:cancel, _('No and don\'t ask again'), :controller => 'profile', :action => 'refuse_join', :profile => profile.identifier) if logged_in? %> diff --git a/lib/authenticated_system.rb b/lib/authenticated_system.rb index 7e73acb..70ded91 100644 --- a/lib/authenticated_system.rb +++ b/lib/authenticated_system.rb @@ -79,8 +79,8 @@ module AuthenticatedSystem # Store the URI of the current request in the session. # # We can return to this location by calling #redirect_back_or_default. - def store_location - session[:return_to] = request.request_uri + def store_location(location = request.request_uri) + session[:return_to] = location end # Redirect to the URI stored by the most recent store_location call or diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index be9ebb6..ffe1256 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1121,4 +1121,5 @@ class CmsControllerTest < Test::Unit::TestCase get :view, :profile => profile.identifier, :id => profile.blog.id assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/upload_files?parent_id=#{profile.blog.id}"} end + end diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 84d78a2..0556331 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -109,41 +109,7 @@ class MembershipsControllerTest < Test::Unit::TestCase community = Community.create!(:name => 'my test community', :description => 'description test') community.add_member(profile) get :index, :profile => profile.identifier - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/leave/#{community.id}" }, :content => 'Leave' - end - - should 'present confirmation before leaving a profile' do - community = Community.create!(:name => 'my test community') - community.add_member(profile) - get :leave, :profile => profile.identifier, :id => community.id - - assert_response :success - assert_template 'leave' - end - - should 'actually leave profile' do - community = Community.create!(:name => 'my test community') - community.add_member(profile) - assert_includes profile.memberships, community - post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1' - - assert_response :redirect - assert_redirected_to :action => 'index' - - profile = Profile.find(@profile.id) - assert_not_includes profile.memberships, community - end - - should 'leave profile when on wizard' do - community = Community.create!(:name => 'my test community') - community.add_member(profile) - post :leave, :profile => profile.identifier, :id => community.id, :confirmation => '1', :wizard => true - - assert_response :redirect - assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true - - profile = Profile.find(@profile.id) - assert_not_includes profile.memberships, community + assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/leave" }, :content => 'Leave' end should 'current user is added as admin after create new community' do diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index a6aa7b2..b593905 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -12,6 +12,7 @@ class ProfileControllerTest < Test::Unit::TestCase @profile = create_user('testuser').person end + attr_reader :profile def test_local_files_reference assert_local_files_reference @@ -224,7 +225,8 @@ class ProfileControllerTest < Test::Unit::TestCase community = Community.create!(:name => 'my test community') community.add_member(@profile) get :index, :profile => community.identifier - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/leave/#{community.id}" } + assert_tag :tag => 'a', + :attributes => { :href => "/profile/#{community.identifier}/leave" } end should 'not show Leave This Community button for non-registered users' do @@ -510,4 +512,126 @@ class ProfileControllerTest < Test::Unit::TestCase assert_equal ((2..10).to_a + [community.id]), @request.session[:no_asking] end + should 'present confirmation before leaving a profile' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + login_as(profile.identifier) + get :leave, :profile => community.identifier + + assert_template 'leave' + assert_tag :tag => 'input', :attributes => {:value => 'Yes, I want to leave.', :type => 'submit'} + end + + should 'actually leave profile' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + assert_includes profile.memberships, community + + login_as(profile.identifier) + post :leave, :profile => community.identifier, :confirmation => '1' + + profile = Profile.find(@profile.id) + assert_not_includes profile.memberships, community + end + + should 'leave profile when on wizard' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + login_as(profile.identifier) + post :leave, :profile => community.identifier, :confirmation => '1', :wizard => true + + assert_response :redirect + assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true + + profile = Profile.find(@profile.id) + assert_not_includes profile.memberships, community + end + + should "offer button to close 'leave community' lightbox" do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + login_as(profile.identifier) + get :index, :profile => community.identifier + + assert_tag :tag => 'a', :content => 'Leave', :attributes => { :href => "/profile/#{community.identifier}/leave", :class => /^lbOn/ } + end + + should 'offer button to cancel leaving community' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + login_as(profile.identifier) + get :leave, :profile => community.identifier + + assert_tag :tag => 'a', :content => "No, I don't want." + end + + should 'render without layout when use lightbox to leave community' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + @request.stubs(:xhr?).returns(true) + login_as(profile.identifier) + get :leave, :profile => community.identifier + + assert_no_tag :tag => 'body' # e.g. no layout + end + + should 'require login to leave community' do + community = Community.create!(:name => 'my test community') + get :leave, :profile => community.identifier + + assert_redirected_to :controller => 'account', :action => 'login' + end + + should 'redirect to stored location after leave community' do + community = Community.create!(:name => 'my test community') + community.add_member(profile) + + @request.session[:return_to] = "/profile/#{community.identifier}/to_go" + login_as(profile.identifier) + + post :leave, :profile => community.identifier, :confirmation => '1' + + assert_redirected_to "/profile/#{community.identifier}/to_go" + end + + should 'store referer location when request leave via get' do + community = Community.create!(:name => 'my test community') + login_as(profile.identifier) + + assert_nil @request.session[:return_to] + @request.expects(:referer).returns("/profile/redirect_to") + + get :leave, :profile => community.identifier + + assert_equal '/profile/redirect_to', @request.session[:return_to] + end + + should 'store referer location when request join via get' do + community = Community.create!(:name => 'my test community') + login_as(profile.identifier) + + assert_nil @request.session[:return_to] + @request.expects(:referer).returns("/profile/redirect_to") + + get :join, :profile => community.identifier + + assert_equal '/profile/redirect_to', @request.session[:return_to] + end + + should 'redirect to stored location after join community' do + community = Community.create!(:name => 'my test community') + + @request.session[:return_to] = "/profile/#{community.identifier}/to_go" + login_as(profile.identifier) + + post :join, :profile => community.identifier, :confirmation => '1' + + assert_redirected_to "/profile/#{community.identifier}/to_go" + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index d6799d1..a0e1775 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1370,6 +1370,16 @@ class ProfileTest < Test::Unit::TestCase assert_equal "header customized", person.custom_header end + should 'provide URL to leave' do + profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'leave'}, profile.leave_url) + end + + should 'provide URL to join' do + profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'join'}, profile.join_url) + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2