From 7962cdf169592a42cfafe84518973795ab08509c Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Thu, 16 Jan 2025 20:42:18 +0100 Subject: [PATCH] Simplify object and association checks * check for instead of .nil? * check for _id? instead of .nil? (avoids record loading) --- app/models/quantity.rb | 6 +++--- app/models/unit.rb | 6 +++--- app/views/default/units/_unit.html.erb | 2 +- app/views/quantities/create.turbo_stream.erb | 4 ++-- app/views/quantities/new.turbo_stream.erb | 4 ++-- app/views/quantities/reparent.turbo_stream.erb | 2 +- app/views/units/_form.html.erb | 6 ++---- app/views/units/_unit.html.erb | 2 +- app/views/units/create.turbo_stream.erb | 4 ++-- app/views/units/destroy.turbo_stream.erb | 2 +- app/views/units/new.turbo_stream.erb | 4 ++-- app/views/units/rebase.turbo_stream.erb | 6 +++--- app/views/units/update.turbo_stream.erb | 2 +- 13 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/models/quantity.rb b/app/models/quantity.rb index b733004..3d6b52a 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -7,8 +7,8 @@ class Quantity < ApplicationRecord dependent: :restrict_with_error validate if: ->{ parent.present? } do - errors.add(:parent, :user_mismatch) unless user == parent.user - errors.add(:parent, :self_reference) if self == parent + errors.add(:parent, :user_mismatch) unless user_id == parent.user_id + errors.add(:parent, :self_reference) if id == parent_id end validate if: ->{ parent.present? }, on: :update do errors.add(:parent, :descendant_reference) if ancestor_of?(parent) @@ -68,7 +68,7 @@ class Quantity < ApplicationRecord numbered.project( numbered[Arel.star], numbered.cast(numbered[:child_number], 'BINARY').as('path'), - ).where(root.nil? ? numbered[:parent_id].eq(nil) : numbered[:id].eq(root.id)), + ).where(root ? numbered[:id].eq(root.id) : numbered[:parent_id].eq(nil)), numbered.project( numbered[Arel.star], arel_table[:path].concat(numbered[:child_number]), diff --git a/app/models/unit.rb b/app/models/unit.rb index 7b84871..5fad8e7 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -6,9 +6,9 @@ class Unit < ApplicationRecord has_many :subunits, class_name: "Unit", inverse_of: :base, dependent: :restrict_with_error validate if: ->{ base.present? } do - errors.add(:base, :user_mismatch) unless user == base.user - errors.add(:base, :self_reference) if self == base - errors.add(:base, :multilevel_nesting) if base.base.present? + errors.add(:base, :user_mismatch) unless user_id == base.user_id + errors.add(:base, :self_reference) if id == base_id + errors.add(:base, :multilevel_nesting) if base.base_id? end validates :symbol, presence: true, uniqueness: {scope: :user_id}, length: {maximum: type_for_attribute(:symbol).limit} diff --git a/app/views/default/units/_unit.html.erb b/app/views/default/units/_unit.html.erb index 810e803..bce8a73 100644 --- a/app/views/default/units/_unit.html.erb +++ b/app/views/default/units/_unit.html.erb @@ -1,5 +1,5 @@ <%= tag.tr do %> - + <%= unit %> diff --git a/app/views/quantities/create.turbo_stream.erb b/app/views/quantities/create.turbo_stream.erb index b73eba5..7a5c339 100644 --- a/app/views/quantities/create.turbo_stream.erb +++ b/app/views/quantities/create.turbo_stream.erb @@ -3,5 +3,5 @@ <% @ancestors.map do |ancestor| %> <%= turbo_stream.replace ancestor %> <% end %> -<%= @before.nil? ? turbo_stream.append(:quantities, @quantity) : - turbo_stream.before(@before, @quantity) %> +<%= @before ? turbo_stream.before(@before, @quantity) : + turbo_stream.append(:quantities, @quantity) %> diff --git a/app/views/quantities/new.turbo_stream.erb b/app/views/quantities/new.turbo_stream.erb index 9ebb8e2..5f3ebfd 100644 --- a/app/views/quantities/new.turbo_stream.erb +++ b/app/views/quantities/new.turbo_stream.erb @@ -8,11 +8,11 @@ <%= turbo_stream.append :quantity_form do %> <%- tabular_form_with model: @quantity, html: {id: ids[:form_tag]} do |form| %> - <%= form.hidden_field :parent_id unless @quantity.parent.nil? %> + <%= form.hidden_field :parent_id if @quantity.parent_id? %> <% end %> <% end %> -<% if @quantity.parent %> +<% if @quantity.parent_id? %> <%= turbo_stream.remove ids[:row] %> <%= turbo_stream.after @quantity.parent, partial: 'form', locals: ids %> <% else %> diff --git a/app/views/quantities/reparent.turbo_stream.erb b/app/views/quantities/reparent.turbo_stream.erb index 3023389..0533837 100644 --- a/app/views/quantities/reparent.turbo_stream.erb +++ b/app/views/quantities/reparent.turbo_stream.erb @@ -5,5 +5,5 @@ <%= turbo_stream.replace ancestor %> <% end %> <% @self_and_progenies.each do |q| %> - <%= @before.nil? ? turbo_stream.append(:quantities, q) : turbo_stream.before(@before, q) %> + <%= @before ? turbo_stream.before(@before, q) : turbo_stream.append(:quantities, q) %> <% end %> diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb index 474b070..82c08cb 100644 --- a/app/views/units/_form.html.erb +++ b/app/views/units/_form.html.erb @@ -2,16 +2,14 @@ <%- tag.tr id: row, class: "form", onkeydown: "processKey(event)", data: {link: link, form: form_tag, hidden_row: hidden_row} do %> - + <%= form.text_field :symbol, required: true, autofocus: true, size: 12 %> <%= form.text_area :description, cols: 30, rows: 1, escape: false %> - <% unless @unit.base.nil? %> - <%= form.number_field :multiplier, required: true, size: 10, min: :step %> - <% end %> + <%= form.number_field :multiplier, required: true, size: 10, min: :step if @unit.base_id? %> diff --git a/app/views/units/_unit.html.erb b/app/views/units/_unit.html.erb index dad60d4..f3bc986 100644 --- a/app/views/units/_unit.html.erb +++ b/app/views/units/_unit.html.erb @@ -14,7 +14,7 @@ <% if current_user.at_least(:active) %> - <% if unit.base.nil? %> + <% unless unit.base_id? %> <%= image_link_to t('.new_subunit'), 'plus-outline', new_unit_path(unit), id: dom_id(unit, :new, :link), onclick: 'this.blur();', data: {turbo_stream: true} %> <% end %> diff --git a/app/views/units/create.turbo_stream.erb b/app/views/units/create.turbo_stream.erb index 98beca6..75fb4f5 100644 --- a/app/views/units/create.turbo_stream.erb +++ b/app/views/units/create.turbo_stream.erb @@ -1,4 +1,4 @@ <%= turbo_stream.close_form dom_id(@unit.base || Unit, :new) %> <%= turbo_stream.remove :no_items %> -<%= turbo_stream.replace @unit.base unless @unit.base.nil? %> -<%= @before.nil? ? turbo_stream.append(:units, @unit) : turbo_stream.before(@before, @unit) %> +<%= turbo_stream.replace @unit.base if @unit.base_id? %> +<%= @before ? turbo_stream.before(@before, @unit) : turbo_stream.append(:units, @unit) %> diff --git a/app/views/units/destroy.turbo_stream.erb b/app/views/units/destroy.turbo_stream.erb index 2923de8..e556834 100644 --- a/app/views/units/destroy.turbo_stream.erb +++ b/app/views/units/destroy.turbo_stream.erb @@ -1,3 +1,3 @@ -<%= turbo_stream.replace @unit.base unless @unit.base.nil? %> +<%= turbo_stream.replace @unit.base if @unit.base_id? %> <%= turbo_stream.remove @unit %> <%= turbo_stream.append(:units, render_no_items) if current_user.units.empty? %> diff --git a/app/views/units/new.turbo_stream.erb b/app/views/units/new.turbo_stream.erb index 697a65f..8540537 100644 --- a/app/views/units/new.turbo_stream.erb +++ b/app/views/units/new.turbo_stream.erb @@ -8,11 +8,11 @@ <%= turbo_stream.append :unit_form do %> <%- tabular_form_with model: @unit, html: {id: ids[:form_tag]} do |form| %> - <%= form.hidden_field :base_id unless @unit.base.nil? %> + <%= form.hidden_field :base_id if @unit.base_id? %> <% end %> <% end %> -<% if @unit.base %> +<% if @unit.base_id? %> <%= turbo_stream.remove ids[:row] %> <%= turbo_stream.after @unit.base, partial: 'form', locals: ids %> <% else %> diff --git a/app/views/units/rebase.turbo_stream.erb b/app/views/units/rebase.turbo_stream.erb index bec908e..36c369d 100644 --- a/app/views/units/rebase.turbo_stream.erb +++ b/app/views/units/rebase.turbo_stream.erb @@ -1,4 +1,4 @@ <%= turbo_stream.remove @unit %> -<%= turbo_stream.replace @previous_base unless @previous_base.nil? %> -<%= turbo_stream.replace @unit.base unless @unit.base.nil? || @previous_base == @unit.base %> -<%= @before.nil? ? turbo_stream.append(:units, @unit) : turbo_stream.before(@before, @unit) %> +<%= turbo_stream.replace @previous_base if @previous_base %> +<%= turbo_stream.replace @unit.base if @unit.base_id? && (@previous_base.id != @unit.base_id) %> +<%= @before ? turbo_stream.before(@before, @unit) : turbo_stream.append(:units, @unit) %> diff --git a/app/views/units/update.turbo_stream.erb b/app/views/units/update.turbo_stream.erb index 3dd4b73..99d7adc 100644 --- a/app/views/units/update.turbo_stream.erb +++ b/app/views/units/update.turbo_stream.erb @@ -1,3 +1,3 @@ <%= turbo_stream.close_form dom_id(@unit, :edit) %> -<%= turbo_stream.replace @unit.base unless @unit.base.nil? %> +<%= turbo_stream.replace @unit.base if @unit.base_id? %> <%= turbo_stream.replace @unit %>