diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4f112fd8c..a56371c5f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -183,21 +183,8 @@ class UsersController < ApplicationController @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) @membership.save respond_to do |format| - if @membership.valid? - format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } - format.js { - render(:update) {|page| - page.replace_html "tab-content-memberships", :partial => 'users/memberships' - page.visual_effect(:highlight, "member-#{@membership.id}") - } - } - else - format.js { - render(:update) {|page| - page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) - } - } - end + format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } + format.js end end @@ -208,7 +195,7 @@ class UsersController < ApplicationController end respond_to do |format| format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } - format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } + format.js end end diff --git a/app/views/users/destroy_membership.js.erb b/app/views/users/destroy_membership.js.erb new file mode 100644 index 000000000..af06d5bed --- /dev/null +++ b/app/views/users/destroy_membership.js.erb @@ -0,0 +1 @@ +Element.update('tab-content-memberships', '<%= escape_javascript(render :partial => 'users/memberships') %>'); diff --git a/app/views/users/edit_membership.js.erb b/app/views/users/edit_membership.js.erb new file mode 100644 index 000000000..ee33799e5 --- /dev/null +++ b/app/views/users/edit_membership.js.erb @@ -0,0 +1,6 @@ +<% if @membership.valid? %> + Element.update("tab-content-memberships", '<%= escape_javascript(render :partial => 'users/memberships') %>'); + new Effect.Highlight("member-<%= @membership.id %>") +<% else %> + alert('<%= escape_javascript l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')) %>'); +<% end %> diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 27f725dc2..3d4f08487 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -363,38 +363,45 @@ class UsersControllerTest < ActionController::TestCase def test_create_membership_js_format assert_difference 'Member.count' do post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js' + assert_response :success + assert_template 'edit_membership' + assert_equal 'text/javascript', response.content_type end - assert_response :success - assert_select_rjs :replace_html, 'tab-content-memberships' member = Member.first(:order => 'id DESC') assert_equal User.find(7), member.principal assert_equal [2], member.role_ids assert_equal 3, member.project_id + assert_include 'tab-content-memberships', response.body end def test_create_membership_js_format_with_failure assert_no_difference 'Member.count' do post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js' + assert_response :success + assert_template 'edit_membership' + assert_equal 'text/javascript', response.content_type end - assert_response :success - assert @response.body.match(/alert/i), "Alert message not sent" - assert @response.body.match(/role can't be empty/i), "Error message not sent" + assert_include 'alert', response.body, "Alert message not sent" + assert_include 'Role can\\\'t be empty', response.body, "Error message not sent" end def test_update_membership assert_no_difference 'Member.count' do put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]} + assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' end - assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' assert_equal [2], Member.find(1).role_ids end def test_update_membership_js_format assert_no_difference 'Member.count' do put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js' + assert_response :success + assert_template 'edit_membership' + assert_equal 'text/javascript', response.content_type end - assert_response :success - assert_select_rjs :replace_html, 'tab-content-memberships' + assert_equal [2], Member.find(1).role_ids + assert_include 'tab-content-memberships', response.body end def test_destroy_membership @@ -408,8 +415,11 @@ class UsersControllerTest < ActionController::TestCase def test_destroy_membership_js_format assert_difference 'Member.count', -1 do delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js' + assert_response :success + assert_template 'destroy_membership' + assert_equal 'text/javascript', response.content_type end - assert_response :success - assert_select_rjs :replace_html, 'tab-content-memberships' + assert_nil Member.find_by_id(1) + assert_include 'tab-content-memberships', response.body end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c4c956953..fb0ba77ad 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -160,8 +160,8 @@ class ActiveSupport::TestCase assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options)) end - def assert_include(expected, s) - assert s.include?(expected), "\"#{expected}\" not found in \"#{s}\"" + def assert_include(expected, s, message=nil) + assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") end def assert_not_include(expected, s)