Add Units

This commit is contained in:
2023-07-06 18:34:16 +02:00
parent 6f415dfb62
commit a4745c9cb8
21 changed files with 285 additions and 15 deletions

26
app/models/unit.rb Normal file
View File

@@ -0,0 +1,26 @@
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}
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?
end
acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy, order_column: :multiplier
scope :defaults, -> { where(user: nil) }
after_save if: :base do |record|
record.move_to_ordered_child_of(record.base, :multiplier)
end
before_destroy do
# TODO: disallow destruction if any object depends on this unit
nil
end
end