List units ordered, not using nested_set

This commit is contained in:
cryptogopher 2024-01-09 00:11:26 +01:00
parent 92132768fa
commit 4e91c89153
2 changed files with 9 additions and 6 deletions

View File

@ -15,10 +15,13 @@ class Unit < ApplicationRecord
order_column: :multiplier
scope :defaults, ->{ where(user: nil) }
after_save if: :base do |record|
record.move_to_ordered_child_of(record.base, :multiplier)
end
scope :ordered, ->{
parent_symbol = Arel::Nodes::NamedFunction.new(
'COALESCE',
[Arel::Table.new(:bases_units)[:symbol], Unit.arel_table[:symbol]]
)
left_outer_joins(:base).order(parent_symbol, :multiplier)
}
before_destroy do
# TODO: disallow destruction if any object depends on this unit

View File

@ -11,7 +11,7 @@ class User < ApplicationRecord
disabled: 0, # administratively disallowed to sign in
}, default: :active
has_many :units, -> { order :lft }, dependent: :destroy
has_many :units, -> { ordered }, dependent: :destroy
def at_least(status)
User.statuses[self.status] >= User.statuses[status]