Commit b7a9e41b authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Handle perfomance issue with team import. Model specs

parent fd0aca12
...@@ -22,19 +22,32 @@ class UsersProject < ActiveRecord::Base ...@@ -22,19 +22,32 @@ class UsersProject < ActiveRecord::Base
class << self class << self
def import_team(source_project, target_project) def import_team(source_project, target_project)
UsersProject.transaction do UsersProject.without_repository_callback do
team = source_project.users_projects.all UsersProject.transaction do
team = source_project.users_projects.all
team.each do |tm|
# Skip if user already present in team team.each do |tm|
next if target_project.users.include?(tm.user) # Skip if user already present in team
next if target_project.users.include?(tm.user)
new_tm = tm.dup
new_tm.id = nil new_tm = tm.dup
new_tm.project_id = target_project.id new_tm.id = nil
new_tm.save new_tm.project_id = target_project.id
new_tm.save
end
end end
end end
target_project.update_repository
true
rescue
false
end
def without_repository_callback
UsersProject.skip_callback(:destroy, :after, :update_repository)
yield
UsersProject.set_callback(:destroy, :after, :update_repository)
end end
def bulk_delete(project, user_ids) def bulk_delete(project, user_ids)
......
...@@ -35,4 +35,37 @@ describe UsersProject do ...@@ -35,4 +35,37 @@ describe UsersProject do
it { should respond_to(:user_name) } it { should respond_to(:user_name) }
it { should respond_to(:user_email) } it { should respond_to(:user_email) }
end end
describe :import_team do
before do
@abilities = Six.new
@abilities << Ability
@project_1 = create :project
@project_2 = create :project
@user_1 = create :user
@user_2 = create :user
@project_1.add_access @user_1, :write
@project_2.add_access @user_2, :read
@status = UsersProject.import_team(@project_1, @project_2)
end
it { @status.should be_true }
describe 'project 2 should get user 1 as developer. user_2 should not be changed' do
it { @project_2.users.should include(@user_1) }
it { @project_2.users.should include(@user_2) }
it { @abilities.allowed?(@user_1, :write_project, @project_2).should be_true }
it { @abilities.allowed?(@user_2, :read_project, @project_2).should be_true }
end
describe 'project 1 should not be changed' do
it { @project_1.users.should include(@user_1) }
it { @project_1.users.should_not include(@user_2) }
end
end
end end
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