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

118 lines
3.2 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)
.distinct
@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
{
2024-01-06 01:58:12 +08:00
name: :update,
2023-05-24 17:48:32 +08:00
label: I18n.t('libraries.index.buttons.edit'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-edit',
2024-01-06 01:58:12 +08:00
type: :emit
2023-05-24 17:48:32 +08:00
}
end
def duplicate_action
return unless @single && can_create_repositories?(@current_team)
2023-05-24 17:48:32 +08:00
{
2024-01-06 01:58:12 +08:00
name: :duplicate,
2023-05-24 17:48:32 +08:00
label: I18n.t('libraries.index.buttons.duplicate'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-duplicate',
2024-01-06 01:58:12 +08:00
type: :emit
2023-05-24 17:48:32 +08:00
}
end
def export_action
return unless @repositories.all? { |repository| can_read_repository?(repository) }
{
2024-01-06 01:58:12 +08:00
name: :export,
label: I18n.t('libraries.index.buttons.export'),
icon: 'sn-icon sn-icon-export',
2024-01-06 01:58:12 +08:00
path: export_repositories_team_path(@current_team),
export_limit: TeamZipExport.exports_limit,
num_of_requests_left: @current_user.exports_left - 1,
export_file_type: current_user.settings[:repository_export_file_type] || 'xlsx',
2024-01-06 01:58:12 +08:00
type: :emit
}
end
2023-05-24 17:48:32 +08:00
def archive_action
return unless @repositories.all? { |repository| can_archive_repository?(repository) }
{
2024-01-06 01:58:12 +08:00
name: :archive,
2023-05-24 17:48:32 +08:00
label: I18n.t('libraries.index.buttons.archive'),
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),
2024-01-06 01:58:12 +08:00
type: :emit
2023-05-24 17:48:32 +08:00
}
end
def share_action
return unless @single && can_share_repository?(@repository)
2023-05-24 17:48:32 +08:00
{
2024-01-06 01:58:12 +08:00
name: :share,
2023-05-24 17:48:32 +08:00
label: I18n.t('repositories.index.share_inventory'),
icon: 'sn-icon sn-icon-shared',
2024-01-06 01:58:12 +08:00
type: :emit
2023-05-24 17:48:32 +08:00
}
end
def restore_action
return unless @repositories.all? { |repository| can_archive_repository?(repository) }
{
2024-01-06 01:58:12 +08:00
name: :restore,
2023-05-24 17:48:32 +08:00
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
path: restore_team_repositories_path(@current_team),
2024-01-06 01:58:12 +08:00
type: :emit
2023-05-24 17:48:32 +08:00
}
end
def delete_action
return unless @single && can_delete_repository?(@repository)
2023-05-24 17:48:32 +08:00
{
2024-01-06 01:58:12 +08:00
name: :delete,
2023-05-24 17:48:32 +08:00
label: I18n.t('libraries.index.buttons.delete'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-delete',
2024-01-06 01:58:12 +08:00
path: team_repository_path(@current_team, @repository),
type: :emit
2023-05-24 17:48:32 +08:00
}
end
2023-05-24 17:48:32 +08:00
end
end