Project

General

Profile

Patch #43640 » 0002-Add-or-remove-users-from-group-using-the-context-men.patch

Marius BĂLTEANU, 2026-04-26 17:40

View differences:

app/controllers/context_menus_controller.rb
134 134
    if @users.size == 1
135 135
      @user = @users.first
136 136
    end
137

  
138
    @groups = Group.givable.sorted.to_a
139
    @common_group_ids = @users.map(&:group_ids).reduce(:&)
140
    @back = back_url
141

  
137 142
    render layout: false
138 143
  end
139 144
end
app/controllers/groups_controller.rb
117 117
    @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
118 118
    @group.users << @users
119 119
    respond_to do |format|
120
      format.html {redirect_to edit_group_path(@group, :tab => 'users')}
120
      format.html {redirect_back_or_default edit_group_path(@group, :tab => 'users')}
121 121
      format.js
122 122
      format.api do
123 123
        if @users.any?
......
133 133
    @users = User.where(:id => (params[:user_id] || params[:user_ids])).to_a
134 134
    @group.users.delete(@users) if request.delete?
135 135
    respond_to do |format|
136
      format.html {redirect_to edit_group_path(@group, :tab => 'users')}
136
      format.html {redirect_back_or_default edit_group_path(@group, :tab => 'users')}
137 137
      format.js
138 138
      format.api {render_api_ok}
139 139
    end
app/views/context_menus/users.html.erb
36 36
                            method: :delete, class: 'icon icon-del' %>
37 37
    </li>
38 38
  <% end %>
39

  
40
  <% if @groups.present? %>
41
    <li class="folder">
42
      <a href="#" class="submenu"><%= l(:button_add_to_group) %></a>
43
      <span class="icon-only"><%= sprite_icon('angle-right', rtl: true) %></span>
44
      <ul>
45
        <% @groups.each do |group| %>
46
          <li><%= context_menu_link group.name, group_users_path(group, :user_ids => @users.map(&:id), :back_url => @back), :method => :post %></li>
47
        <% end %>
48
      </ul>
49
    </li>
50
    <% if @common_group_ids.present? %>
51
      <li class="folder">
52
        <a href="#" class="submenu"><%= l(:button_remove_from_group) %></a>
53
        <span class="icon-only"><%= sprite_icon('angle-right', rtl: true) %></span>
54
        <ul>
55
          <% @groups.each do |group| %>
56
            <% if @common_group_ids.include?(group.id) %>
57
              <li><%= context_menu_link group.name, group_users_path(group, :user_ids => @users.map(&:id), :back_url => @back), :method => :delete %></li>
58
            <% end %>
59
          <% end %>
60
        </ul>
61
      </li>
62
    <% end %>
63
  <% end %>
39 64
</ul>
config/locales/en.yml
1205 1205
  button_expand_all: Expand all
1206 1206
  button_delete: Delete
1207 1207
  button_remove: Remove
1208
  button_remove_from_group: Remove from group
1208 1209
  button_create: Create
1209 1210
  button_create_and_continue: Create and add another
1210 1211
  button_test: Test
1211 1212
  button_edit: Edit
1212 1213
  button_edit_associated_wikipage: "Edit associated Wiki page: %{page_title}"
1213 1214
  button_add: Add
1215
  button_add_to_group: Add to group
1214 1216
  button_change: Change
1215 1217
  button_apply: Apply
1216 1218
  button_clear: Clear
test/functional/context_menus_controller_test.rb
522 522
      assert_select 'a.icon-del', :count => 0
523 523
    end
524 524
  end
525

  
526
  def test_users_context_menu
527
    @request.session[:user_id] = 1 # admin
528
    get :users, :params => {:ids => [8]}
529
    assert_response :success
530

  
531
    assert_select 'li.folder' do
532
      assert_select 'a', :text => 'Add to group'
533
      assert_select 'ul' do
534
        assert_select 'a', :text => 'A Team'
535
      end
536
    end
537
    # User 8 is in Group 10
538
    assert_select 'li.folder' do
539
      assert_select 'a', :text => 'Remove from group'
540
      assert_select 'a', :text => 'A Team'
541
    end
542
  end
543

  
544
  def test_users_context_menu_bulk
545
    @request.session[:user_id] = 1 # admin
546
    # Add user 2 to group 10 (user 8 is already there)
547
    Group.find(10).users << User.find(2)
548

  
549
    get :users, :params => {:ids => [2, 8]}
550
    assert_response :success
551

  
552
    assert_select 'li.folder' do
553
      assert_select 'a', :text => 'Add to group'
554
      assert_select 'ul' do
555
        assert_select 'a', :text => 'A Team'
556
        assert_select 'a', :text => 'B Team'
557
      end
558
    end
559
    # Both users are in Group 10
560
    assert_select 'li.folder' do
561
      assert_select 'a', :text => 'Remove from group'
562
      assert_select 'a', :text => 'A Team'
563
    end
564
  end
525 565
end
test/functional/groups_controller_test.rb
314 314
    assert_response :success
315 315
    assert_include 'John Smith', response.body
316 316
  end
317

  
318
  def test_add_users_from_context_menu_should_redirect_to_back_url
319
    assert_difference 'Group.find(10).users.count', 1 do
320
      post(
321
        :add_users,
322
        :params => {
323
          :id => 10,
324
          :user_ids => ['2'],
325
          :back_url => '/users'
326
        }
327
      )
328
      assert_redirected_to '/users'
329
    end
330
  end
317 331
end
(12-12/13)