scinote-web/app/services/toolbars/repositories_service.rb

126 lines
3.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Toolbars
2023-05-24 17:48:32 +08:00
class RepositoriesService
attr_reader :current_user
include Canaid::Helpers::PermissionsHelper
include Rails.application.routes.url_helpers
def initialize(current_user, current_team, repository_ids: [])
@current_user = current_user
@current_team = current_team
@repositories = Repository.readable_by_user(current_user)
.where(id: repository_ids)
@repository = @repositories.first
2023-05-24 17:48:32 +08:00
@archived_state = @repositories.all.any?(&:archived?)
@single = @repositories.uniq.length == 1
2023-05-24 17:48:32 +08:00
end
def actions
return [] if @repositories.none?
if @archived_state
[export_action, restore_action, delete_action]
2023-05-24 17:48:32 +08:00
else
[rename_action, duplicate_action, export_action, archive_action, share_action]
2023-05-24 17:48:32 +08:00
end.compact
end
private
def rename_action
return unless @single && can_manage_repository?(@repository)
2023-05-24 17:48:32 +08:00
{
name: 'rename',
label: I18n.t('libraries.index.buttons.edit'),
button_id: 'renameRepoBtn',
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-edit',
2023-05-24 17:48:32 +08:00
path: team_repository_rename_modal_path(@current_team, repository_id: @repository),
type: 'remote-modal'
}
end
def duplicate_action
return unless @single && can_create_repositories?(@current_team)
2023-05-24 17:48:32 +08:00
{
name: 'duplicate',
label: I18n.t('libraries.index.buttons.duplicate'),
button_id: 'copyRepoBtn',
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-duplicate',
2023-05-24 17:48:32 +08:00
path: team_repository_copy_modal_path(@current_team, repository_id: @repository),
type: 'remote-modal'
}
end
def export_action
return unless @repositories.all? { |repository| can_read_repository?(repository) }
{
name: 'export',
label: I18n.t('libraries.index.buttons.export'),
button_id: 'exportRepoBtn',
icon: 'sn-icon sn-icon-export',
path: export_modal_team_repositories_path(@current_team, counter: @repositories.length),
type: 'remote-modal'
}
end
2023-05-24 17:48:32 +08:00
def archive_action
return unless @repositories.all? { |repository| can_archive_repository?(repository) }
{
name: 'archive',
label: I18n.t('libraries.index.buttons.archive'),
button_id: 'archiveRepoBtn',
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-archive',
2023-05-24 17:48:32 +08:00
path: archive_team_repositories_path(@current_team),
type: :request,
request_method: :post
}
end
def share_action
return unless @single && can_share_repository?(@repository)
2023-05-24 17:48:32 +08:00
{
name: 'share',
label: I18n.t('repositories.index.share_inventory'),
icon: 'sn-icon sn-icon-shared',
2023-05-24 17:48:32 +08:00
button_class: 'share-repository-button',
path: team_repository_share_modal_path(@current_team, repository_id: @repository),
type: 'remote-modal'
}
end
def restore_action
return unless @repositories.all? { |repository| can_archive_repository?(repository) }
{
name: 'restore',
label: I18n.t('libraries.index.buttons.restore'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-restore',
2023-05-24 17:48:32 +08:00
button_id: 'restoreRepoBtn',
path: restore_team_repositories_path(@current_team),
type: :request,
request_method: :post
}
end
def delete_action
return unless @single && can_delete_repository?(@repository)
2023-05-24 17:48:32 +08:00
{
name: 'delete',
label: I18n.t('libraries.index.buttons.delete'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-delete',
2023-05-24 17:48:32 +08:00
button_id: 'deleteRepoBtn',
path: team_repository_destroy_modal_path(@current_team, repository_id: @repository),
type: 'remote-modal'
}
end
2023-05-24 17:48:32 +08:00
end
end