2023-05-04 17:29:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Toolbars
|
|
|
|
class MyModulesService
|
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
def initialize(current_user, my_module_ids: [])
|
|
|
|
@current_user = current_user
|
|
|
|
@my_modules = MyModule.joins(experiment: :project)
|
|
|
|
.where(projects: { team_id: current_user.current_team_id })
|
|
|
|
.readable_by_user(current_user)
|
|
|
|
.where(id: my_module_ids)
|
|
|
|
|
|
|
|
@single = @my_modules.length == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions
|
|
|
|
return [] if @my_modules.none?
|
|
|
|
|
|
|
|
[
|
|
|
|
restore_action,
|
|
|
|
edit_action,
|
|
|
|
access_action,
|
|
|
|
move_action,
|
|
|
|
duplicate_action,
|
|
|
|
archive_action
|
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def restore_action
|
|
|
|
return unless @my_modules.all? { |my_module| can_restore_my_module?(my_module) }
|
|
|
|
|
|
|
|
experiment = @my_modules.first.experiment
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'restore',
|
|
|
|
label: I18n.t('experiments.table.toolbar.restore'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-restore',
|
2024-08-01 21:44:47 +08:00
|
|
|
button_id: 'restoreTask',
|
2023-05-04 17:29:19 +08:00
|
|
|
path: restore_my_modules_experiment_path(experiment),
|
2023-12-12 19:17:38 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
my_module = @my_modules.first
|
|
|
|
|
|
|
|
return unless can_manage_my_module?(my_module)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'edit',
|
|
|
|
label: I18n.t('experiments.table.toolbar.edit'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-edit',
|
2023-12-12 19:17:38 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
my_module = @my_modules.first
|
|
|
|
|
|
|
|
return unless can_read_my_module?(my_module)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'access',
|
|
|
|
label: I18n.t('experiments.table.my_module_actions.access'),
|
2023-06-26 16:29:56 +08:00
|
|
|
icon: 'sn-icon sn-icon-project-member-access',
|
2024-08-01 21:44:47 +08:00
|
|
|
path: my_module_path(my_module, format: :json),
|
|
|
|
button_id: 'openAccessModal',
|
2023-12-12 19:17:38 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_action
|
2023-12-29 16:53:46 +08:00
|
|
|
return unless @my_modules.all? { |my_module| can_move_my_module?(my_module) }
|
2023-05-04 17:29:19 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
name: 'move',
|
|
|
|
label: I18n.t('experiments.table.toolbar.move'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-move',
|
2024-08-01 21:44:47 +08:00
|
|
|
button_id: 'moveTask',
|
2024-05-21 19:46:17 +08:00
|
|
|
type: :emit,
|
|
|
|
path: move_modules_experiment_path(@my_modules.first.experiment, my_module_ids: @my_modules.pluck(:id))
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate_action
|
|
|
|
experiment = @my_modules.first.experiment
|
|
|
|
|
|
|
|
return unless @my_modules.all?(&:active?)
|
|
|
|
return unless can_manage_experiment?(experiment)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'duplicate',
|
|
|
|
label: I18n.t('experiments.table.toolbar.duplicate'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-duplicate',
|
2023-05-04 17:29:19 +08:00
|
|
|
path: batch_clone_my_modules_experiment_path(experiment),
|
2023-12-12 19:17:38 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive_action
|
|
|
|
return unless @my_modules.all? { |my_module| can_archive_my_module?(my_module) }
|
|
|
|
|
|
|
|
experiment = @my_modules.first.experiment
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'archive',
|
|
|
|
label: I18n.t('experiments.table.toolbar.archive'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-archive',
|
2023-05-04 17:29:19 +08:00
|
|
|
path: archive_my_modules_experiment_path(experiment),
|
2023-12-12 19:17:38 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|