forked from fixin.me/fixin.me
Simplify object and association checks
* check for <object> instead of <object>.nil? * check for <association>_id? instead of <association>.nil? (avoids record loading)
This commit is contained in:
parent
c908063212
commit
7962cdf169
@ -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]),
|
||||
|
@ -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}
|
||||
|
@ -1,5 +1,5 @@
|
||||
<%= tag.tr do %>
|
||||
<td class="<%= class_names({subunit: unit.base, grayed: unit.default?}) %>">
|
||||
<td class="<%= class_names({subunit: unit.base_id?, grayed: unit.default?}) %>">
|
||||
<%= unit %>
|
||||
</td>
|
||||
|
||||
|
@ -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) %>
|
||||
|
@ -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 %>
|
||||
|
@ -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 %>
|
||||
|
@ -2,16 +2,14 @@
|
||||
<%- tag.tr id: row, class: "form", onkeydown: "processKey(event)",
|
||||
data: {link: link, form: form_tag, hidden_row: hidden_row} do %>
|
||||
|
||||
<td class="<%= class_names({subunit: @unit.base}) %>">
|
||||
<td class="<%= class_names({subunit: @unit.base_id?}) %>">
|
||||
<%= form.text_field :symbol, required: true, autofocus: true, size: 12 %>
|
||||
</td>
|
||||
<td>
|
||||
<%= form.text_area :description, cols: 30, rows: 1, escape: false %>
|
||||
</td>
|
||||
<td class="number">
|
||||
<% 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? %>
|
||||
</td>
|
||||
|
||||
<td class="actions">
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
<% if current_user.at_least(:active) %>
|
||||
<td class="actions">
|
||||
<% 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 %>
|
||||
|
@ -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) %>
|
||||
|
@ -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? %>
|
||||
|
@ -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 %>
|
||||
|
@ -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) %>
|
||||
|
@ -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 %>
|
||||
|
Loading…
x
Reference in New Issue
Block a user