Merge pull request #4839 from techAPJ/bulk-add-group

FEATURE: provide more details when performing a bulk add to group
This commit is contained in:
Arpit Jalan 2017-04-27 02:39:57 +05:30 committed by GitHub
commit 9b0ecdaf7e
7 changed files with 38 additions and 5 deletions

View File

@ -0,0 +1,4 @@
export default Ember.Controller.extend({
adminGroupsBulk: Ember.inject.controller(),
bulkAddResponse: Ember.computed.alias('adminGroupsBulk.bulkAddResponse')
});

View File

@ -6,6 +6,7 @@ export default Ember.Controller.extend({
users: null,
groupId: null,
saving: false,
bulkAddResponse: null,
@computed('saving', 'users', 'groupId')
buttonDisabled(saving, users, groupId) {
@ -24,7 +25,8 @@ export default Ember.Controller.extend({
ajax('/admin/groups/bulk', {
data: { users, group_id: this.get('groupId') },
method: 'PUT'
}).then(() => {
}).then(result => {
this.set('bulkAddResponse', result);
this.transitionToRoute('adminGroups.bulkComplete');
}).catch(popupAjaxError).finally(() => {
this.set('saving', false);

View File

@ -1 +1,11 @@
<p>{{i18n "admin.groups.bulk_complete"}}</p>
{{#if bulkAddResponse}}
<p>{{{bulkAddResponse.message}}}</p>
{{#if bulkAddResponse.users_not_added}}
<p>{{i18n "admin.groups.bulk_complete_users_not_added"}}</p>
{{#each bulkAddResponse.users_not_added as |user|}}
{{user}}<br/>
{{/each}}
{{/if}}
{{else}}
<p>{{i18n "admin.groups.bulk_complete"}}</p>
{{/if}}

View File

@ -24,13 +24,22 @@ class Admin::GroupsController < Admin::AdminController
def bulk_perform
group = Group.find(params[:group_id].to_i)
users_added = 0
if group.present?
users = (params[:users] || []).map {|u| u.downcase}
user_ids = User.where("username_lower in (:users) OR email IN (:users)", users: users).pluck(:id)
group.bulk_add(user_ids) if user_ids.present?
valid_emails = valid_usernames = {}
valid_users = User.where("username_lower IN (:users) OR email IN (:users)", users: users).pluck(:id, :username_lower, :email)
valid_users.each do |vu|
valid_emails[vu[1]] = valid_usernames[vu[2]] = vu[0]
vu.slice!(1..2)
end
invalid_users = users.reject! { |u| valid_emails[u] || valid_usernames[u] }
valid_users.flatten!
group.bulk_add(valid_users) if valid_users.present?
users_added = valid_users.count
end
render json: success_json
render json: { success: true, message: I18n.t('groups.success.bulk_add', users_added: users_added), users_not_added: invalid_users }
end
def create

View File

@ -2601,6 +2601,7 @@ en:
add_members: "Add members"
custom: "Custom"
bulk_complete: "The users have been added to the group."
bulk_complete_users_not_added: "These users were not added:"
bulk: "Bulk Add to Group"
bulk_paste: "Paste a list of usernames or emails, one per line:"
bulk_select: "(select a group)"

View File

@ -255,6 +255,8 @@ en:
delete_reason: "Deleted via post moderation queue"
groups:
success:
bulk_add: "%{users_added} users have been added to the group."
errors:
can_not_modify_automatic: "You cannot modify an automatic group"
member_already_exist: "'%{username}' is already a member of this group."

View File

@ -69,6 +69,11 @@ describe Admin::GroupsController do
expect(user2.primary_group).to eq(group)
expect(user2.title).to eq("WAT")
expect(user2.trust_level).to eq(4)
# verify JSON response
json = ::JSON.parse(response.body)
expect(json['message']).to eq("2 users have been added to the group.")
expect(json['users_not_added'][0]).to eq("doesnt_exist")
end
end