Implement Units default destroy

This commit is contained in:
cryptogopher 2024-11-30 16:11:31 +01:00
parent 1fedd70fe5
commit 2cbae12fa2
4 changed files with 32 additions and 9 deletions

View File

@ -1,7 +1,8 @@
class Default::UnitsController < ApplicationController
navigation_tab :units
before_action :find_unit, only: [:import, :export, :destroy]
before_action :find_unit, only: :export
before_action :find_unit_default, only: [:import, :destroy]
before_action only: :import do
raise AccessForbidden unless current_user.at_least(:active)
@ -15,7 +16,9 @@ class Default::UnitsController < ApplicationController
end
def import
raise ParameterInvalid unless @unit.default? && @unit.port(current_user)
@unit.port!(current_user)
flash.now[:notice] = t('.success', unit: @unit)
ensure
run_and_render :index
end
@ -26,16 +29,26 @@ class Default::UnitsController < ApplicationController
#end
def export
raise ParameterInvalid unless !@unit.default? && @unit.port(nil)
@unit.port!(nil)
flash.now[:notice] = t('.success', unit: @unit)
ensure
run_and_render :index
end
def destroy
@unit.destroy!
flash.now[:notice] = t('.success', unit: @unit)
ensure
run_and_render :index
end
private
def find_unit
@unit = Unit.find_by!(id: params[:id], user: [current_user, nil])
@unit = Unit.find_by!(id: params[:id], user: current_user)
end
def find_unit_default
@unit = Unit.find_by!(id: params[:id], user: nil)
end
end

View File

@ -97,11 +97,11 @@ class Unit < ApplicationRecord
user_id.nil?
end
def port(recipient)
recipient_base = base && Unit.find_by(symbol: base.symbol, user: recipient)
return nil if recipient_base.nil? != base.nil?
# Should only by invoked on Units returned from #defaults_diff which are #portable
def port!(recipient)
recipient_base = base && Unit.find_by!(symbol: base.symbol, user: recipient)
params = slice(ATTRIBUTES - [:symbol, :base_id])
Unit.find_or_initialize_by(user: recipient, symbol: symbol)
.update(base: recipient_base, **params)
.update!(base: recipient_base, **params)
end
end

View File

@ -11,7 +11,8 @@
<% end %>
<% if current_user.at_least(:admin) %>
<% if unit.default? %>
<%= image_button_to t('.delete'), 'delete-outline', unit_path(unit), method: :delete %>
<%= image_button_to t('.delete'), 'delete-outline', default_unit_path(unit),
method: :delete %>
<% else %>
<%= image_button_to t('.export'), 'upload-outline', export_default_unit_path(unit),
disabled_attributes(!unit.portable?) %>

View File

@ -33,6 +33,9 @@ en:
forbidden: >
You have not been granted access to this action (403 Forbidden).
This should not happen, please notify site administrator.
not_found: >
The record that you requested operation on does not exist (404).
This should not happen, please notify site administrator.
unprocessable_entity: >
The request is semantically incorrect and was rejected (422 Unprocessable Entity).
This should not happen, please notify site administrator.
@ -77,6 +80,12 @@ en:
actions: Actions on defaults
import_all: Import all
back: Back to units
import:
success: Imported unit "%{unit}"
export:
success: Exported unit "%{unit}"
destroy:
success: Deleted unit "%{unit}"
users:
index:
disguise: View as