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 class Default::UnitsController < ApplicationController
navigation_tab :units 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 before_action only: :import do
raise AccessForbidden unless current_user.at_least(:active) raise AccessForbidden unless current_user.at_least(:active)
@ -15,7 +16,9 @@ class Default::UnitsController < ApplicationController
end end
def import 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 run_and_render :index
end end
@ -26,16 +29,26 @@ class Default::UnitsController < ApplicationController
#end #end
def export 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 run_and_render :index
end end
def destroy def destroy
@unit.destroy!
flash.now[:notice] = t('.success', unit: @unit)
ensure
run_and_render :index
end end
private private
def find_unit 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
end end

View File

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

View File

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

View File

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