Change unit base with drag and drop

This commit is contained in:
2024-03-24 17:38:56 +01:00
parent 7387e7b1c1
commit 05b0c66216
6 changed files with 56 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ class Unit < ApplicationRecord
belongs_to :user, optional: true
# TODO: validate base.user == user
belongs_to :base, optional: true, class_name: "Unit"
has_many :subunits, class_name: "Unit", dependent: :restrict_with_error, inverse_of: :base
validates :symbol, presence: true, uniqueness: {scope: :user_id},
length: {maximum: columns_hash['symbol'].limit}
@@ -18,11 +19,16 @@ class Unit < ApplicationRecord
'COALESCE',
[Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]]
)
left_outer_joins(:base).order(parent_symbol, arel_table[:base_id].asc.nulls_first, :multiplier)
left_outer_joins(:base)
.order(parent_symbol, arel_table[:base_id].asc.nulls_first, :multiplier, :symbol)
}
before_destroy do
# TODO: disallow destruction if any object depends on this unit
nil
end
def movable?
subunits.empty?
end
end