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)
|
.from(units).group(:base_id, :symbol)
|
||||||
}
|
}
|
||||||
scope :ordered, ->{
|
scope :ordered, ->{
|
||||||
left_outer_joins(:base)
|
left_outer_joins(:base).order(ordering)
|
||||||
.order(arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]),
|
|
||||||
arel_table[:base_id].not_eq(nil), :multiplier, :symbol)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
before_destroy do
|
||||||
# TODO: disallow destruction if any object depends on this unit
|
# TODO: disallow destruction if any object depends on this unit
|
||||||
nil
|
nil
|
||||||
@ -104,4 +109,14 @@ class Unit < ApplicationRecord
|
|||||||
Unit.find_or_initialize_by(user: recipient, symbol: symbol)
|
Unit.find_or_initialize_by(user: recipient, symbol: symbol)
|
||||||
.update!(base: recipient_base, **params)
|
.update!(base: recipient_base, **params)
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user