From 92132768fa34246ac466546919c5ca412ce5badb Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 9 Jan 2024 00:08:53 +0100 Subject: [PATCH] Multiplier in nil for toplevel units --- app/models/unit.rb | 10 ++++------ app/views/units/_index.html.erb | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/models/unit.rb b/app/models/unit.rb index f466809..7a4877e 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -1,16 +1,14 @@ class Unit < ApplicationRecord - attribute :multiplier, default: 1 - belongs_to :user, optional: true belongs_to :base, optional: true, class_name: "Unit" validates :symbol, presence: true, uniqueness: {scope: :user_id}, length: {maximum: columns_hash['symbol'].limit} validates :name, length: {maximum: columns_hash['name'].limit} - validates :multiplier, numericality: {equal_to: 1}, unless: :base - validates :multiplier, numericality: {other_than: 1}, if: :base - validate if: -> { base.present? } do - errors.add(:base, :only_top_level_base_units) unless base.base.nil? + validates :multiplier, absence: true, unless: :base + validates :multiplier, presence: true, numericality: {other_than: 0}, if: :base + validate if: ->{ base&.base.present? } do + errors.add(:base, :multilevel_nesting) end acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy, diff --git a/app/views/units/_index.html.erb b/app/views/units/_index.html.erb index 51e1739..5cb6164 100644 --- a/app/views/units/_index.html.erb +++ b/app/views/units/_index.html.erb @@ -4,7 +4,7 @@ <%= link_to unit.symbol, edit_unit_path(unit), class: level > 0 ? 'subunit' : '' %> <%= unit.name %> - <%= scientifize(unit.multiplier) unless unit.multiplier == 1 %> + <%= scientifize(unit.multiplier) if unit.multiplier %> <% if current_user.at_least(:active) %> <%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),