2023-05-17 17:27:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryMismatchError < StandardError; end
|
|
|
|
|
|
|
|
module Toolbars
|
|
|
|
class RepositoryRowsService
|
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
def initialize(current_user, repository_row_ids: [])
|
|
|
|
@current_user = current_user
|
|
|
|
@repository_rows = RepositoryRow.where(id: repository_row_ids)
|
|
|
|
|
|
|
|
return if @repository_rows.none?
|
|
|
|
|
|
|
|
if @repository_rows.pluck(:repository_id).uniq.size != 1
|
|
|
|
raise RepositoryMismatchError, 'Items are not from the same repository!'
|
|
|
|
end
|
|
|
|
|
|
|
|
@repository = @repository_rows.first.repository
|
|
|
|
|
|
|
|
@single = @repository_rows.length == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions
|
|
|
|
return [] if @repository_rows.none?
|
|
|
|
|
|
|
|
[
|
|
|
|
restore_action,
|
|
|
|
edit_action,
|
2023-05-17 22:11:27 +08:00
|
|
|
assign_action,
|
2023-05-17 17:27:55 +08:00
|
|
|
duplicate_action,
|
|
|
|
export_action,
|
|
|
|
print_label_action,
|
|
|
|
archive_action,
|
|
|
|
delete_action
|
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def restore_action
|
|
|
|
return unless can_manage_repository_rows?(@repository)
|
|
|
|
|
|
|
|
return unless @repository_rows.all?(&:archived?)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'restore',
|
|
|
|
label: I18n.t('repositories.restore_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-restore',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'resotre-repository-row-btn',
|
|
|
|
button_id: 'restoreRepositoryRecords',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_action
|
|
|
|
return unless can_manage_repository_rows?(@repository)
|
|
|
|
|
2023-05-18 17:46:44 +08:00
|
|
|
return unless @repository_rows.all?(&:active?)
|
|
|
|
|
2023-05-17 17:27:55 +08:00
|
|
|
{
|
|
|
|
name: 'edit',
|
|
|
|
label: I18n.t('repositories.edit_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-edit',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'edit-repository-row-btn',
|
|
|
|
button_id: 'editRepositoryRecord',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-17 22:11:27 +08:00
|
|
|
def assign_action
|
2023-05-25 21:46:58 +08:00
|
|
|
return unless can_read_repository?(@repository)
|
2023-05-17 22:11:27 +08:00
|
|
|
|
2023-05-18 17:46:44 +08:00
|
|
|
return unless @repository_rows.all?(&:active?)
|
|
|
|
|
2023-05-17 22:11:27 +08:00
|
|
|
{
|
|
|
|
name: 'assign',
|
|
|
|
label: I18n.t('repositories.assign_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-files',
|
2023-05-17 22:11:27 +08:00
|
|
|
button_class: 'assign-repository-rows-btn',
|
|
|
|
button_id: 'assignRepositoryRecords',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-17 17:27:55 +08:00
|
|
|
def duplicate_action
|
|
|
|
return unless can_create_repository_rows?(@repository)
|
|
|
|
|
2023-05-18 17:46:44 +08:00
|
|
|
return unless @repository_rows.all?(&:active?)
|
|
|
|
|
2023-05-17 17:27:55 +08:00
|
|
|
{
|
|
|
|
name: 'duplicate',
|
|
|
|
label: I18n.t('repositories.copy_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-duplicate',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'copy-repository-row-btn',
|
|
|
|
button_id: 'copyRepositoryRecords',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_action
|
|
|
|
return unless can_read_repository?(@repository)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'export',
|
|
|
|
label: I18n.t('repositories.export_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-export',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'export-repository-row-btn',
|
|
|
|
button_id: 'exportRepositoriesButton',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_label_action
|
|
|
|
return unless can_read_repository?(@repository)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'print_label',
|
|
|
|
label: I18n.t('repositories.print_label'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-printer',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'print-label-button',
|
|
|
|
button_id: 'toolbarPrintLabel',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive_action
|
|
|
|
return unless can_manage_repository_rows?(@repository)
|
|
|
|
|
|
|
|
return unless @repository_rows.all?(&:active?)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'archive',
|
|
|
|
label: I18n.t('repositories.archive_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-archive',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'resotre-repository-row-btn',
|
|
|
|
button_id: 'archiveRepositoryRecordsButton',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_action
|
|
|
|
return unless can_delete_repository_rows?(@repository)
|
|
|
|
|
|
|
|
return unless @repository_rows.all?(&:archived?)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'delete',
|
|
|
|
label: I18n.t('repositories.delete_record'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-delete',
|
2023-05-17 17:27:55 +08:00
|
|
|
button_class: 'resotre-repository-row-btn',
|
|
|
|
button_id: 'deleteRepositoryRecords',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|