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

View File

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

View File

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

View File

@@ -13,6 +13,7 @@ class Quantity < ApplicationRecord
errors.add(:parent, :self_reference) if id == parent_id errors.add(:parent, :self_reference) if id == parent_id
end end
validate if: ->{ parent.present? }, on: :update do validate if: ->{ parent.present? }, on: :update do
# TODO: should not report error for (parent == self)
errors.add(:parent, :descendant_reference) if ancestor_of?(parent) errors.add(:parent, :descendant_reference) if ancestor_of?(parent)
end end
validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]}, validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]},

View File

@@ -1,4 +1,4 @@
<%= turbo_stream.close_form dom_id(@unit.base || Unit, :new) %> <%= turbo_stream.close_form dom_target(:new, @unit.base || :unit) %>
<%= turbo_stream.remove :no_items %> <%= turbo_stream.remove :no_items %>
<%= turbo_stream.replace @unit.base if @unit.base_id? %> <%= turbo_stream.replace @unit.base if @unit.base_id? %>
<%= @before ? turbo_stream.before(@before, @unit) : turbo_stream.append(:units, @unit) %> <%= @before ? turbo_stream.before(@before, @unit) : turbo_stream.append(:units, @unit) %>

View File

@@ -1,7 +1,7 @@
<% ids = {row: dom_id(@unit, :edit), <% ids = {row: dom_target(:edit, @unit),
hidden_row: dom_id(@unit), hidden_row: dom_target(@unit),
link: nil, link: nil,
form_tag: dom_id(@unit, :edit, :form)} %> form_tag: dom_target(:edit, @unit, :form)} %>
<%= turbo_stream.append :unit_form do %> <%= turbo_stream.append :unit_form do %>
<%- tabular_form_with model: @unit, html: {id: ids[:form_tag]} do %> <%- tabular_form_with model: @unit, html: {id: ids[:form_tag]} do %>

View File

@@ -1,8 +1,8 @@
<% dom_obj = @unit.base || @unit %> <% target_id = dom_target(:new, @unit.base || :unit) %>
<% ids = {row: dom_id(dom_obj, :new), <% ids = {row: target_id,
hidden_row: nil, hidden_row: nil,
link: dom_id(dom_obj, :new, :link), link: dom_target(target_id, :link),
form_tag: dom_id(dom_obj, :new, :form)} %> form_tag: dom_target(target_id, :form)} %>
<%= turbo_stream.disable ids[:link] -%> <%= turbo_stream.disable ids[:link] -%>

View File

@@ -1,3 +1,3 @@
<%= turbo_stream.close_form dom_id(@unit, :edit) %> <%= turbo_stream.close_form dom_target(:edit, @unit) %>
<%= turbo_stream.replace @unit.base if @unit.base_id? %> <%= turbo_stream.replace @unit.base if @unit.base_id? %>
<%= turbo_stream.replace @unit %> <%= turbo_stream.replace @unit %>

View File

@@ -1,4 +1,5 @@
module CoreExt::ActionView::RecordIdentifierWithSuffix module CoreExt::ActionView::RecordIdentifierWithSuffix
# TODO: replace dom_id with dom_target, then remove this override
def dom_id(object, prefix = nil, suffix = nil) def dom_id(object, prefix = nil, suffix = nil)
if suffix if suffix
"#{super(object, prefix)}#{::ActionView::RecordIdentifier::JOIN}#{suffix}" "#{super(object, prefix)}#{::ActionView::RecordIdentifier::JOIN}#{suffix}"