Improve controllers and views

This commit is contained in:
2026-07-28 00:46:33 +02:00
parent eb2797bd90
commit eae0ee9d20
9 changed files with 25 additions and 28 deletions

View File

@@ -17,7 +17,7 @@ class Default::UnitsController < ApplicationController
def import
@unit.port!(current_user)
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
ensure
run_and_render :index
end
@@ -30,14 +30,14 @@ class Default::UnitsController < ApplicationController
def export
@unit.port!(nil)
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
ensure
run_and_render :index
end
def destroy
@unit.destroy!
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
ensure
run_and_render :index
end

View File

@@ -21,7 +21,7 @@ class QuantitiesController < ApplicationController
if @quantity.save
@before = @quantity.successive
@ancestors = @quantity.ancestors
flash.now[:notice] = t('.success', quantity: @quantity)
flash.now.notice = t('.success', quantity: @quantity)
else
render :new
end
@@ -33,7 +33,7 @@ class QuantitiesController < ApplicationController
def update
if @quantity.update(quantity_params.except(:parent_id))
@ancestors = @quantity.ancestors
flash.now[:notice] = t('.success', quantity: @quantity)
flash.now.notice = t('.success', quantity: @quantity)
else
render :edit
end
@@ -57,7 +57,7 @@ class QuantitiesController < ApplicationController
def destroy
@quantity.destroy!
@ancestors = @quantity.ancestors
flash.now[:notice] = t('.success', quantity: @quantity)
flash.now.notice = t('.success', quantity: @quantity)
end
private

View File

@@ -1,10 +1,9 @@
class UnitsController < ApplicationController
before_action only: :new do
find_unit if params[:id].present?
end
before_action :find_unit, only: [:edit, :update, :rebase, :destroy]
before_action ->{ @unit = current_user.units.find_by!(id: params[:id]) },
only: [:new, :edit, :update, :rebase, :destroy],
unless: ->{ action_name == "new" && params[:id].nil? }
before_action except: :index do
before_action except: [:index] do
raise AccessForbidden unless current_user.at_least(:active)
end
@@ -20,7 +19,7 @@ class UnitsController < ApplicationController
@unit = current_user.units.new(params.expect(Unit::ATTRIBUTES))
if @unit.save
@before = @unit.successive
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
else
render_errors @unit
end
@@ -31,7 +30,7 @@ class UnitsController < ApplicationController
def update
if @unit.update(params.except(:base_id).expect(Unit::ATTRIBUTES))
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
else
render_errors @unit
end
@@ -48,18 +47,14 @@ class UnitsController < ApplicationController
@before = @unit.successive
if @unit.multiplier_previously_changed?
flash.now[:notice] = t(".multiplier_reset", unit: @unit)
flash.now.notice = t(".multiplier_reset", unit: @unit)
end
end
def destroy
@unit.destroy!
flash.now[:notice] = t('.success', unit: @unit)
flash.now.notice = t('.success', unit: @unit)
end
private
def find_unit
@unit = current_user.units.find_by!(id: params[:id])
end
end