From 782256f6557315d499a16183e05303da095ce342 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 18 Jul 2024 11:46:30 +0200 Subject: [PATCH] Add toolbar for storage locations [SCI-10861] --- .../storage_locations_controller.rb | 6 +- app/permissions/team.rb | 4 + .../toolbars/storage_locations_service.rb | 86 +++++++++++++++++++ config/locales/en.yml | 5 ++ config/routes.rb | 4 + 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 app/services/toolbars/storage_locations_service.rb diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index a6af1aecc..9d3521058 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -52,7 +52,11 @@ class StorageLocationsController < ApplicationController def actions_toolbar render json: { - actions: [] # TODO: Add actions + actions: + Toolbars::StorageLocationsService.new( + current_user, + storage_location_ids: JSON.parse(params[:items]).map { |i| i['id'] } + ).actions } end diff --git a/app/permissions/team.rb b/app/permissions/team.rb index 7b69fe2e7..827fa6e4c 100644 --- a/app/permissions/team.rb +++ b/app/permissions/team.rb @@ -47,6 +47,10 @@ Canaid::Permissions.register_for(Team) do true # TODO: Add permission check end + can :manage_storage_locations do |user, team| + true # TODO: Add permission check + end + can :create_reports do |user, team| team.permission_granted?(user, TeamPermissions::REPORTS_CREATE) end diff --git a/app/services/toolbars/storage_locations_service.rb b/app/services/toolbars/storage_locations_service.rb new file mode 100644 index 000000000..fe33eeeff --- /dev/null +++ b/app/services/toolbars/storage_locations_service.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +module Toolbars + class StorageLocationsService + attr_reader :current_user + + include Canaid::Helpers::PermissionsHelper + include Rails.application.routes.url_helpers + + def initialize(current_user, storage_location_ids: []) + @current_user = current_user + @storage_locations = StorageLocation.where(id: storage_location_ids) + + @single = @storage_locations.length == 1 + end + + def actions + return [] if @storage_locations.none? + + [ + edit_action, + move_action, + duplicate_action, + delete_action + ].compact + end + + private + + def edit_action + return unless @single + + return unless can_manage_storage_locations?(current_user.current_team) + + { + name: 'edit', + label: I18n.t('storage_locations.index.toolbar.edit'), + icon: 'sn-icon sn-icon-edit', + path: storage_location_path(@storage_locations.first), + type: :emit + } + end + + def move_action + return unless @single + + return unless can_manage_storage_locations?(current_user.current_team) + + { + name: 'set_as_default', + label: I18n.t("storage_locations.index.toolbar.move"), + icon: 'sn-icon sn-icon-move', + path: move_storage_location_path(@storage_locations.first), + type: :emit + } + end + + def duplicate_action + return unless @single + + return unless can_manage_storage_locations?(current_user.current_team) + + { + name: 'duplicate', + label: I18n.t('storage_locations.index.toolbar.duplicate'), + icon: 'sn-icon sn-icon-duplicate', + path: copy_storage_location_path(@storage_locations.first), + type: :emit + } + end + + def delete_action + return unless @single + + return unless can_manage_storage_locations?(current_user.current_team) + + { + name: 'delete', + label: I18n.t('storage_locations.index.toolbar.delete'), + icon: 'sn-icon sn-icon-delete', + path: storage_location_path(@storage_locations.first), + type: :emit + } + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5409ac5ff..d32c0f24e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2670,6 +2670,11 @@ en: head_title: "Locations" new_location: "New location" new_box: "New box" + toolbar: + edit: 'Edit' + move: 'Move' + duplicate: 'Duplicate' + delete: 'Delete' table: name: "Location name" id: "ID" diff --git a/config/routes.rb b/config/routes.rb index ad233e0a6..41a447ed5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -811,6 +811,10 @@ Rails.application.routes.draw do collection do get :actions_toolbar end + member do + post :move + post :copy + end resources :storage_location_repository_rows, only: %i(index create destroy update) end