Commit 0013ea51 authored by Douwe Maan's avatar Douwe Maan

Remove duplication between Group and ProjectMember.

parent a8b462bb
...@@ -52,8 +52,7 @@ class Projects::ProjectMembersController < Projects::ApplicationController ...@@ -52,8 +52,7 @@ class Projects::ProjectMembersController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.html do format.html do
redirect_to namespace_project_project_members_path(@project.namespace, redirect_to namespace_project_project_members_path(@project.namespace, @project)
@project)
end end
format.js { render nothing: true } format.js { render nothing: true }
end end
......
...@@ -47,25 +47,8 @@ class Group < Namespace ...@@ -47,25 +47,8 @@ class Group < Namespace
end end
def add_users(user_ids, access_level, current_user = nil) def add_users(user_ids, access_level, current_user = nil)
users = user_ids.map do |user_id| user_ids.each do |user_id|
(user_id if user_id.is_a?(User)) || Member.add_user(self.group_members, user_id, access_level, current_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
end end
......
...@@ -52,9 +52,39 @@ class Member < ActiveRecord::Base ...@@ -52,9 +52,39 @@ class Member < ActiveRecord::Base
delegate :name, :username, :email, to: :user, prefix: true delegate :name, :username, :email, to: :user, prefix: true
def self.find_by_invite_token(invite_token) class << self
invite_token = Devise.token_generator.digest(self, :invite_token, invite_token) def find_by_invite_token(invite_token)
find_by(invite_token: invite_token) invite_token = Devise.token_generator.digest(self, :invite_token, invite_token)
find_by(invite_token: invite_token)
end
# This method is used to find users that have been entered into the "Add members" field.
# These can be the User objects directly, their IDs, their emails, or new emails to be invited.
def user_for_id(user_id)
return user_id if user_id.is_a?(User)
user = User.find_by(id: user_id)
user ||= User.find_by(email: user_id)
user ||= user_id
user
end
def add_user(members, user_id, access_level, current_user = nil)
user = user_for_id(user_id)
# `user` can be either a User object or an email to be invited
if user.is_a?(User)
member = members.find_or_initialize_by(user_id: user.id)
else
member = members.build
member.invite_email = user
end
member.created_by ||= current_user
member.access_level = access_level
member.save
end
end end
def invite? def invite?
......
...@@ -60,29 +60,14 @@ class ProjectMember < Member ...@@ -60,29 +60,14 @@ class ProjectMember < Member
raise "Non valid access" raise "Non valid access"
end end
users = user_ids.map do |user_id| users = user_ids.map { |user_id| Member.user_for_id(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 ProjectMember.transaction do
project_ids.each do |project_id| project_ids.each do |project_id|
project = Project.find(project_id) project = Project.find(project_id)
users.each do |user| users.each do |user|
if user.is_a?(User) Member.add_user(project.project_members, user, access_level, current_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
end end
end end
......
...@@ -58,6 +58,43 @@ describe Member do ...@@ -58,6 +58,43 @@ describe Member do
it { is_expected.to respond_to(:user_email) } it { is_expected.to respond_to(:user_email) }
end end
describe ".add_user" do
let!(:user) { create(:user) }
let(:project) { create(:project) }
context "when called with a user id" do
it "adds the user as a member" do
Member.add_user(project.project_members, user.id, ProjectMember::MASTER)
expect(project.users).to include(user)
end
end
context "when called with a user object" do
it "adds the user as a member" do
Member.add_user(project.project_members, user, ProjectMember::MASTER)
expect(project.users).to include(user)
end
end
context "when called with a known user email" do
it "adds the user as a member" do
Member.add_user(project.project_members, user.email, ProjectMember::MASTER)
expect(project.users).to include(user)
end
end
context "when called with an unknown user email" do
it "adds a member invite" do
Member.add_user(project.project_members, "user@example.com", ProjectMember::MASTER)
expect(project.project_members.invite.pluck(:invite_email)).to include("user@example.com")
end
end
end
describe "#accept_invite!" do describe "#accept_invite!" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) } let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
...@@ -89,7 +126,6 @@ describe Member do ...@@ -89,7 +126,6 @@ describe Member do
end end
describe "#decline_invite!" do describe "#decline_invite!" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) } let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
it "destroys the member" do it "destroys the member" do
...@@ -106,7 +142,6 @@ describe Member do ...@@ -106,7 +142,6 @@ describe Member do
end end
describe "#generate_invite_token" do describe "#generate_invite_token" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) } let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
it "sets the invite token" do it "sets the invite token" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment