From 87dd3f215f8c087d85cde7ac880c5480fd501303 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 10 Apr 2015 15:25:32 +0200 Subject: [PATCH] Let members be invited from Add members pane. --- app/assets/javascripts/users_select.js.coffee | 38 ++++++++++++------- .../projects/project_members_controller.rb | 3 +- app/helpers/selects_helper.rb | 2 + app/models/group.rb | 20 ++++++++-- app/models/members/project_member.rb | 22 +++++++++-- app/views/admin/groups/show.html.haml | 2 +- .../group_members/_new_group_member.html.haml | 5 ++- .../_new_project_member.html.haml | 5 ++- 8 files changed, 72 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/users_select.js.coffee b/app/assets/javascripts/users_select.js.coffee index ccd85f2455d..aeeed9ca3cc 100644 --- a/app/assets/javascripts/users_select.js.coffee +++ b/app/assets/javascripts/users_select.js.coffee @@ -8,6 +8,7 @@ class @UsersSelect @groupId = $(select).data('group-id') showNullUser = $(select).data('null-user') showAnyUser = $(select).data('any-user') + showEmailUser = $(select).data('email-user') firstUser = $(select).data('first-user') $(select).select2 @@ -19,20 +20,6 @@ class @UsersSelect data = { results: users } if query.term.length == 0 - anyUser = { - name: 'Any', - avatar: null, - username: 'none', - id: null - } - - nullUser = { - name: 'Unassigned', - avatar: null, - username: 'none', - id: 0 - } - if firstUser # Move current user to the front of the list for obj, index in data.results @@ -40,11 +27,34 @@ class @UsersSelect data.results.splice(index, 1) data.results.unshift(obj) break + if showNullUser + nullUser = { + name: 'Unassigned', + avatar: null, + username: 'none', + id: 0 + } data.results.unshift(nullUser) + if showAnyUser + anyUser = { + name: 'Any', + avatar: null, + username: 'none', + id: null + } data.results.unshift(anyUser) + if showEmailUser && data.results.length == 0 && query.term.match(/^[^@]+@[^@]+$/) + emailUser = { + name: "Invite \"#{query.term}\"", + avatar: null, + username: query.term, + id: query.term + } + data.results.unshift(emailUser) + query.callback(data) initSelection: (element, callback) => diff --git a/app/controllers/projects/project_members_controller.rb b/app/controllers/projects/project_members_controller.rb index 148768e8fa1..91aba9a7068 100644 --- a/app/controllers/projects/project_members_controller.rb +++ b/app/controllers/projects/project_members_controller.rb @@ -36,8 +36,7 @@ class Projects::ProjectMembersController < Projects::ApplicationController end def create - users = User.where(id: params[:user_ids].split(',')) - @project.team << [users, params[:access_level]] + @project.team.add_users(params[:user_ids].split(','), params[:access_level], current_user) redirect_to namespace_project_project_members_path(@project.namespace, @project) end diff --git a/app/helpers/selects_helper.rb b/app/helpers/selects_helper.rb index 54e0f4f9b3e..bec8f2f1aa7 100644 --- a/app/helpers/selects_helper.rb +++ b/app/helpers/selects_helper.rb @@ -8,6 +8,7 @@ module SelectsHelper null_user = opts[:null_user] || false any_user = opts[:any_user] || false + email_user = opts[:email_user] || false first_user = opts[:first_user] && current_user ? current_user.username : false html = { @@ -15,6 +16,7 @@ module SelectsHelper 'data-placeholder' => placeholder, 'data-null-user' => null_user, 'data-any-user' => any_user, + 'data-email-user' => email_user, 'data-first-user' => first_user } diff --git a/app/models/group.rb b/app/models/group.rb index eec961f6012..2de397f90f7 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -47,10 +47,24 @@ class Group < Namespace end def add_users(user_ids, access_level, current_user = nil) - user_ids.compact.each do |user_id| - member = self.group_members.find_or_initialize_by(user_id: user_id) - member.access_level = access_level + users = user_ids.map do |user_id| + (user_id if user_id.is_a?(User)) || + User.find_by(id: user_id) || + User.find_by(email: user_id) || + user_id + end + + users.compact.each do |user| + if user.is_a?(User) + member = self.group_members.find_or_initialize_by(user_id: user.id) + else + member = self.group_members.build + member.invite_email = user + end + member.created_by ||= current_user + member.access_level = access_level + member.save end end diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index a63d5af1219..3b3b1bd4680 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -60,12 +60,28 @@ class ProjectMember < Member raise "Non valid access" end + users = user_ids.map do |user_id| + (user_id if user_id.is_a?(User)) || + User.find_by(id: user_id) || + User.find_by(email: user_id) || + user_id + end + ProjectMember.transaction do project_ids.each do |project_id| - user_ids.each do |user_id| - member = ProjectMember.new(access_level: access_level, user_id: user_id) - member.source_id = project_id + project = Project.find(project_id) + + users.each do |user| + if user.is_a?(User) + member = project.project_members.find_or_initialize_by(user_id: user.id) + else + member = project.project_members.build + member.invite_email = user + end + member.created_by ||= current_user + member.access_level = access_level + member.save end end diff --git a/app/views/admin/groups/show.html.haml b/app/views/admin/groups/show.html.haml index 404b918c245..14996dcd6a2 100644 --- a/app/views/admin/groups/show.html.haml +++ b/app/views/admin/groups/show.html.haml @@ -60,7 +60,7 @@ = form_tag members_update_admin_group_path(@group), id: "new_project_member", class: "bulk_import", method: :put do %div - = users_select_tag(:user_ids, multiple: true) + = users_select_tag(:user_ids, multiple: true, email_user: true) %div.prepend-top-10 = select_tag :access_level, options_for_select(GroupMember.access_level_roles), class: "project-access-select select2" %hr diff --git a/app/views/groups/group_members/_new_group_member.html.haml b/app/views/groups/group_members/_new_group_member.html.haml index a52b8197384..3361d7e2a8d 100644 --- a/app/views/groups/group_members/_new_group_member.html.haml +++ b/app/views/groups/group_members/_new_group_member.html.haml @@ -1,7 +1,10 @@ = form_for @group_member, url: group_group_members_path(@group), html: { class: 'form-horizontal users-group-form' } do |f| .form-group = f.label :user_ids, "People", class: 'control-label' - .col-sm-10= users_select_tag(:user_ids, multiple: true, class: 'input-large', scope: :all) + .col-sm-10 + = users_select_tag(:user_ids, multiple: true, class: 'input-large', scope: :all, email_user: true) + .help-block + Search for existing users or invite new ones using their email address. .form-group = f.label :access_level, "Group Access", class: 'control-label' diff --git a/app/views/projects/project_members/_new_project_member.html.haml b/app/views/projects/project_members/_new_project_member.html.haml index 5daae2708e6..d708b01a114 100644 --- a/app/views/projects/project_members/_new_project_member.html.haml +++ b/app/views/projects/project_members/_new_project_member.html.haml @@ -1,7 +1,10 @@ = form_for @project_member, as: :project_member, url: namespace_project_project_members_path(@project.namespace, @project), html: { class: 'form-horizontal users-project-form' } do |f| .form-group = f.label :user_ids, "People", class: 'control-label' - .col-sm-10= users_select_tag(:user_ids, multiple: true, class: 'input-large', scope: :all) + .col-sm-10 + = users_select_tag(:user_ids, multiple: true, class: 'input-large', scope: :all, email_user: true) + .help-block + Search for existing users or invite new ones using their email address. .form-group = f.label :access_level, "Project Access", class: 'control-label'