forked from fixin.me/fixin.me
Add Units
This commit is contained in:
26
app/models/unit.rb
Normal file
26
app/models/unit.rb
Normal 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
|
||||
@@ -11,6 +11,8 @@ class User < ApplicationRecord
|
||||
disabled: 0, # administratively disallowed to sign in
|
||||
}, default: :active
|
||||
|
||||
has_many :units, -> { order :lft }, dependent: :destroy
|
||||
|
||||
def at_least(status)
|
||||
User.statuses[self.status] >= User.statuses[status]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user