refactor finder

This commit is contained in:
James Lopez 2018-06-20 10:27:40 +02:00
parent 825c68e2ed
commit 4e9b094b29
No known key found for this signature in database
GPG Key ID: 756BF8E9D7C0CF39
1 changed files with 13 additions and 9 deletions

View File

@ -2,18 +2,22 @@ module Gitlab
module ImportExport
class GroupProjectFinder
def self.find_or_new(*args)
new(*args).find_or_new
Project.transaction do
new(*args).find_or_new
end
end
def self.find_or_create(*args)
new(*args).find_or_create
Project.transaction do
new(*args).find_or_create
end
end
def initialize(klass, attributes)
@klass = klass
@attributes = attributes
@group_id = @attributes['group_id']
@project_id = @attributes['project_id']
@group = @attributes[:group]
@project = @attributes[:project]
end
def find_or_new
@ -27,11 +31,11 @@ module Gitlab
private
def where_clause
@attributes.except('group_id', 'project_id').map do |key, value|
project_clause = table[key].eq(value).and(table[:project_id].eq(@project_id))
@attributes.except(:group, :project).map do |key, value|
project_clause = table[key].eq(value).and(table[:project_id].eq(@project.id))
if @group_id
project_clause.or(table[key].eq(value).and(table[:group_id].eq(@group_id)))
if @group
project_clause.or(table[key].eq(value).and(table[:group_id].eq(@group.id)))
else
project_clause
end
@ -43,7 +47,7 @@ module Gitlab
end
def project_attributes
@attributes.except('group_id').tap do |atts|
@attributes.except(:group).tap do |atts|
atts['type'] = 'ProjectLabel' if label?
end
end