Merge assign and unassign service

This commit is contained in:
aignatov-bio 2020-05-07 13:51:07 +02:00
parent 912b01d34b
commit 29903f570d
4 changed files with 59 additions and 114 deletions

View file

@ -33,22 +33,14 @@ class MyModuleRepositoriesController < ApplicationController
end
def update
if params[:rows_to_assign]
assign_service = RepositoryRows::MyModuleAssigningService.call(my_module: @my_module,
repository: @repository,
user: current_user,
params: params)
end
if params[:rows_to_unassign]
unassign_service = RepositoryRows::MyModuleUnassigningService.call(my_module: @my_module,
repository: @repository,
user: current_user,
params: params)
end
if (params[:rows_to_assign].nil? || assign_service.succeed?) &&
(params[:rows_to_unassign].nil? || unassign_service.succeed?)
flash = update_flash_message
update_row_service = RepositoryRows::MyModuleUpdateRowsService.call(my_module: @my_module,
repository: @repository,
user: current_user,
params: params)
if update_row_service.succeed? &&
(update_row_service.assigned_rows_count.positive? ||
update_row_service.unassigned_rows_count.positive?)
flash = update_flash_message(update_row_service)
status = :ok
else
flash = t('my_modules.repository.flash.update_error')
@ -129,9 +121,9 @@ class MyModuleRepositoriesController < ApplicationController
render_403 unless can_assign_repository_rows_to_module?(@my_module)
end
def update_flash_message
assigned_count = params[:rows_to_assign]&.count
unassigned_count = params[:rows_to_unassign]&.count
def update_flash_message(update_row_service)
assigned_count = update_row_service.assigned_rows_count
unassigned_count = update_row_service.unassigned_rows_count
if params[:downstream] == 'true'
if assigned_count && unassigned_count

View file

@ -1,86 +0,0 @@
# frozen_string_literal: true
module RepositoryRows
class MyModuleUnassigningService
extend Service
attr_reader :repository, :my_module, :user, :params, :unassigned_rows_names, :errors
def initialize(my_module:, repository:, user:, params:)
@my_module = my_module
@repository = repository
@user = user
@params = params
@unassigned_rows_names = Set[]
@errors = {}
end
def call
return self unless valid?
ActiveRecord::Base.transaction do
if params[:downstream] == 'true'
@my_module.downstream_modules.each do |downstream_module|
unassign_repository_rows_from_my_module(downstream_module)
end
else
unassign_repository_rows_from_my_module(@my_module)
end
rescue StandardError => e
@errors[e.record.class.name.underscore] = e.record.errors.full_messages
raise ActiveRecord::Rollback
end
self
end
def succeed?
@errors.none?
end
private
# Returns array of unassigned repository rows
def unassign_repository_rows_from_my_module(my_module)
unassigned_names = my_module.my_module_repository_rows
.joins(:repository_row)
.where(repository_rows: { repository: @repository, id: params[:rows_to_unassign] })
.select('my_module_repository_rows.*, repository_rows.name AS name')
.destroy_all
.pluck(:name)
return [] if unassigned_names.blank?
# update row last_modified_by
my_module.repository_rows
.where(repository: @repository, id: params[:rows_to_unassign])
.update_all(last_modified_by_id: @user.id)
Activities::CreateActivityService.call(activity_type: :unassign_repository_record,
owner: @user,
team: my_module.experiment.project.team,
project: my_module.experiment.project,
subject: my_module,
message_items: { my_module: my_module.id,
repository: @repository.id,
record_names: unassigned_names.join(', ') })
@unassigned_rows_names.merge(unassigned_names)
end
def valid?
unless @my_module && @repository && @user && @params
@errors[:invalid_arguments] =
{ 'my_module': @my_module,
'repository': @repository,
'params': @params,
'user': @user }
.map do |key, value|
I18n.t('repositories.my_module_unassigning_service.invalid_arguments', key: key.capitalize) if value.nil?
end.compact
return false
end
true
end
end
end

View file

@ -1,17 +1,24 @@
# frozen_string_literal: true
module RepositoryRows
class MyModuleAssigningService
class MyModuleUpdateRowsService
extend Service
attr_reader :repository, :my_module, :user, :params, :assigned_rows_names, :errors
attr_reader :repository,
:my_module,
:user,
:params,
:assigned_rows_count,
:unassigned_rows_count,
:errors
def initialize(my_module:, repository:, user:, params:)
@my_module = my_module
@repository = repository
@user = user
@params = params
@assigned_rows_names = Set[]
@assigned_rows_count = 0
@unassigned_rows_count = 0
@errors = {}
end
@ -21,12 +28,14 @@ module RepositoryRows
ActiveRecord::Base.transaction do
if params[:downstream] == 'true'
@my_module.downstream_modules.each do |downstream_module|
unassign_repository_rows_from_my_module(downstream_module)
assign_repository_rows_to_my_module(downstream_module)
end
else
unassign_repository_rows_from_my_module(@my_module)
assign_repository_rows_to_my_module(@my_module)
end
rescue ActiveRecord::RecordInvalid => e
rescue StandardError => e
@errors[e.record.class.name.underscore] = e.record.errors.full_messages
raise ActiveRecord::Rollback
end
@ -40,8 +49,40 @@ module RepositoryRows
private
def unassign_repository_rows_from_my_module(my_module)
return [] unless params[:rows_to_unassign]
unassigned_names = my_module.my_module_repository_rows
.joins(:repository_row)
.where(repository_rows: { repository: @repository, id: params[:rows_to_unassign] })
.select('my_module_repository_rows.*, repository_rows.name AS name')
.destroy_all
.pluck(:name)
return [] if unassigned_names.blank?
# update row last_modified_by
my_module.repository_rows
.where(repository: @repository, id: params[:rows_to_unassign])
.update_all(last_modified_by_id: @user.id)
Activities::CreateActivityService.call(activity_type: :unassign_repository_record,
owner: @user,
team: my_module.experiment.project.team,
project: my_module.experiment.project,
subject: my_module,
message_items: { my_module: my_module.id,
repository: @repository.id,
record_names: unassigned_names.join(', ') })
@unassigned_rows_count += unassigned_names.count
end
def assign_repository_rows_to_my_module(my_module)
assigned_names = []
return [] unless params[:rows_to_assign]
unassigned_rows = @repository.repository_rows
.joins("LEFT OUTER JOIN my_module_repository_rows "\
"ON repository_rows.id = my_module_repository_rows.repository_row_id "\
@ -68,7 +109,7 @@ module RepositoryRows
message_items: { my_module: my_module.id,
repository: @repository.id,
record_names: assigned_names.join(', ') })
@assigned_rows_names.merge(assigned_names)
@assigned_rows_count += assigned_names.count
end
def valid?
@ -79,7 +120,7 @@ module RepositoryRows
'params': @params,
'user': @user }
.map do |key, value|
I18n.t('repositories.my_module_assigning_service.invalid_arguments', key: key.capitalize) if value.nil?
I18n.t('repositories.my_module_update_row.invalid_arguments', key: key.capitalize) if value.nil?
end.compact
return false
end

View file

@ -1206,9 +1206,7 @@ en:
not_allowed: "You are not allowed to share this repository!"
invalid_arguments: "Can't find %{key}"
nothing_to_delete: "Can't find sharing relation for destroy"
my_module_assigning_service:
invalid_arguments: "Can't find %{key}"
my_module_unassigning_service:
my_module_update_row_service:
invalid_arguments: "Can't find %{key}"
my_module_assigned_snapshot_service:
invalid_arguments: "Can't find %{key}"