forked from fixin.me/fixin.me
Find successive Unit according to sort order
This will allow to insert Units into table without refresh of all items/removal of forms
This commit is contained in:
parent
ba8ac5d2fa
commit
55b6ff3248
@ -75,11 +75,16 @@ class Unit < ApplicationRecord
|
||||
.from(units).group(:base_id, :symbol)
|
||||
}
|
||||
scope :ordered, ->{
|
||||
left_outer_joins(:base)
|
||||
.order(arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]),
|
||||
arel_table[:base_id].not_eq(nil), :multiplier, :symbol)
|
||||
left_outer_joins(:base).order(ordering)
|
||||
}
|
||||
|
||||
def self.ordering
|
||||
[arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]),
|
||||
arel_table[:base_id].not_eq(nil),
|
||||
:multiplier,
|
||||
:symbol]
|
||||
end
|
||||
|
||||
before_destroy do
|
||||
# TODO: disallow destruction if any object depends on this unit
|
||||
nil
|
||||
@ -104,4 +109,14 @@ class Unit < ApplicationRecord
|
||||
Unit.find_or_initialize_by(user: recipient, symbol: symbol)
|
||||
.update!(base: recipient_base, **params)
|
||||
end
|
||||
|
||||
def successive
|
||||
units = Unit.arel_table
|
||||
lead = Arel::Nodes::NamedFunction.new('LAG', [units[:id]])
|
||||
window = Arel::Nodes::Window.new.order(*Unit.ordering)
|
||||
lag_id = lead.over(window).as('lag_id')
|
||||
Unit.with(
|
||||
units: user.units.left_outer_joins(:base).select(units[Arel.star], lag_id)
|
||||
).where(units[:lag_id].eq(id)).first
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user