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

@ -14,11 +14,14 @@ class Unit < ApplicationRecord
acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy, acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy,
order_column: :multiplier order_column: :multiplier
scope :defaults, -> { where(user: nil) } scope :defaults, ->{ where(user: nil) }
scope :ordered, ->{
after_save if: :base do |record| parent_symbol = Arel::Nodes::NamedFunction.new(
record.move_to_ordered_child_of(record.base, :multiplier) 'COALESCE',
end [Arel::Table.new(:bases_units)[:symbol], Unit.arel_table[:symbol]]
)
left_outer_joins(:base).order(parent_symbol, :multiplier)
}
before_destroy do before_destroy do
# TODO: disallow destruction if any object depends on this unit # 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 disabled: 0, # administratively disallowed to sign in
}, default: :active }, default: :active
has_many :units, -> { order :lft }, dependent: :destroy has_many :units, -> { ordered }, dependent: :destroy
def at_least(status) def at_least(status)
User.statuses[self.status] >= User.statuses[status] User.statuses[self.status] >= User.statuses[status]