diff --git a/app/controllers/default/units_controller.rb b/app/controllers/default/units_controller.rb index e6dd2ed..6ca2733 100644 --- a/app/controllers/default/units_controller.rb +++ b/app/controllers/default/units_controller.rb @@ -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 diff --git a/app/models/unit.rb b/app/models/unit.rb index fbb2c8e..027c492 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -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 diff --git a/app/views/default/units/_unit.html.erb b/app/views/default/units/_unit.html.erb index 13c2d9e..83e605b 100644 --- a/app/views/default/units/_unit.html.erb +++ b/app/views/default/units/_unit.html.erb @@ -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?) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index ff3c4fd..fe127bb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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