Patch #43640 » 0002-Add-or-remove-users-from-group-using-the-context-men.patch
| app/controllers/context_menus_controller.rb | ||
|---|---|---|
|
if @users.size == 1
|
||
|
@user = @users.first
|
||
|
end
|
||
|
@groups = Group.givable.sorted.to_a
|
||
|
@common_group_ids = Group.givable.joins(:groups_users).where(groups_users: { user_id: @users.map(&:id) }).distinct.pluck(:id).to_set
|
||
|
@back = back_url
|
||
|
render layout: false
|
||
|
end
|
||
|
end
|
||
| app/controllers/groups_controller.rb | ||
|---|---|---|
|
@users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
|
||
|
@group.users << @users
|
||
|
respond_to do |format|
|
||
|
format.html {redirect_to edit_group_path(@group, :tab => 'users')}
|
||
|
format.html {redirect_back_or_default edit_group_path(@group, :tab => 'users')}
|
||
|
format.js
|
||
|
format.api do
|
||
|
if @users.any?
|
||
| ... | ... | |
|
@users = User.where(:id => (params[:user_id] || params[:user_ids])).to_a
|
||
|
@group.users.delete(@users) if request.delete?
|
||
|
respond_to do |format|
|
||
|
format.html {redirect_to edit_group_path(@group, :tab => 'users')}
|
||
|
format.html {redirect_back_or_default edit_group_path(@group, :tab => 'users')}
|
||
|
format.js
|
||
|
format.api {render_api_ok}
|
||
|
end
|
||
| app/views/context_menus/users.html.erb | ||
|---|---|---|
|
method: :delete, class: 'icon icon-del' %>
|
||
|
</li>
|
||
|
<% end %>
|
||
|
<% if @groups.present? %>
|
||
|
<li class="folder">
|
||
|
<a href="#" class="submenu"><%= l(:button_add_to_group) %></a>
|
||
|
<span class="icon-only"><%= sprite_icon('angle-right', rtl: true) %></span>
|
||
|
<ul>
|
||
|
<% @groups.each do |group| %>
|
||
|
<li><%= context_menu_link group.name, group_users_path(group, :user_ids => @users.map(&:id), :back_url => @back), :method => :post %></li>
|
||
|
<% end %>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<% if @common_group_ids.present? %>
|
||
|
<li class="folder">
|
||
|
<a href="#" class="submenu"><%= l(:button_remove_from_group) %></a>
|
||
|
<span class="icon-only"><%= sprite_icon('angle-right', rtl: true) %></span>
|
||
|
<ul>
|
||
|
<% @groups.each do |group| %>
|
||
|
<% if @common_group_ids.include?(group.id) %>
|
||
|
<li><%= context_menu_link group.name, group_users_path(group, :user_ids => @users.map(&:id), :back_url => @back), :method => :delete %></li>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
</ul>
|
||
| config/locales/en.yml | ||
|---|---|---|
|
button_expand_all: Expand all
|
||
|
button_delete: Delete
|
||
|
button_remove: Remove
|
||
|
button_remove_from_group: Remove from group
|
||
|
button_create: Create
|
||
|
button_create_and_continue: Create and add another
|
||
|
button_test: Test
|
||
|
button_edit: Edit
|
||
|
button_edit_associated_wikipage: "Edit associated Wiki page: %{page_title}"
|
||
|
button_add: Add
|
||
|
button_add_to_group: Add to group
|
||
|
button_change: Change
|
||
|
button_apply: Apply
|
||
|
button_clear: Clear
|
||
| test/functional/context_menus_controller_test.rb | ||
|---|---|---|
|
assert_select 'a.icon-del', :count => 0
|
||
|
end
|
||
|
end
|
||
|
def test_users_context_menu
|
||
|
@request.session[:user_id] = 1 # admin
|
||
|
get :users, :params => {:ids => [8]}
|
||
|
assert_response :success
|
||
|
assert_select 'li.folder' do
|
||
|
assert_select 'a', :text => 'Add to group'
|
||
|
assert_select 'ul' do
|
||
|
assert_select 'a', :text => 'A Team'
|
||
|
end
|
||
|
end
|
||
|
# User 8 is in Group 10
|
||
|
assert_select 'li.folder' do
|
||
|
assert_select 'a', :text => 'Remove from group'
|
||
|
assert_select 'a', :text => 'A Team'
|
||
|
end
|
||
|
end
|
||
|
def test_users_context_menu_bulk
|
||
|
@request.session[:user_id] = 1 # admin
|
||
|
# Add user 2 to group 10 (user 8 is already there)
|
||
|
Group.find(10).users << User.find(2)
|
||
|
get :users, :params => {:ids => [2, 8]}
|
||
|
assert_response :success
|
||
|
assert_select 'li.folder' do
|
||
|
assert_select 'a', :text => 'Add to group'
|
||
|
assert_select 'ul' do
|
||
|
assert_select 'a', :text => 'A Team'
|
||
|
assert_select 'a', :text => 'B Team'
|
||
|
end
|
||
|
end
|
||
|
# Both users are in Group 10
|
||
|
assert_select 'li.folder' do
|
||
|
assert_select 'a', :text => 'Remove from group'
|
||
|
assert_select 'a', :text => 'A Team'
|
||
|
end
|
||
|
end
|
||
|
def test_users_context_menu_bulk_with_different_groups
|
||
|
@request.session[:user_id] = 1 # admin
|
||
|
# User 8 is in Group 10
|
||
|
# Add User 2 to Group 11
|
||
|
Group.find(11).users << User.find(2)
|
||
|
get :users, :params => {:ids => [2, 8]}
|
||
|
assert_response :success
|
||
|
# Both Group 10 and Group 11 should be in the Remove submenu
|
||
|
assert_select 'li.folder' do
|
||
|
assert_select 'a', :text => 'Remove from group'
|
||
|
assert_select 'ul' do
|
||
|
assert_select 'a', :text => 'A Team'
|
||
|
assert_select 'a', :text => 'B Team'
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| test/functional/groups_controller_test.rb | ||
|---|---|---|
|
assert_response :success
|
||
|
assert_include 'John Smith', response.body
|
||
|
end
|
||
|
def test_add_users_from_context_menu_should_redirect_to_back_url
|
||
|
assert_difference 'Group.find(10).users.count', 1 do
|
||
|
post(
|
||
|
:add_users,
|
||
|
:params => {
|
||
|
:id => 10,
|
||
|
:user_ids => ['2'],
|
||
|
:back_url => '/users'
|
||
|
}
|
||
|
)
|
||
|
assert_redirected_to '/users'
|
||
|
end
|
||
|
end
|
||
|
end
|
||