brendon/positioning

before_update called even when no positioning columns are updated

creativetags opened this issue · 3 comments

I'm assuming this is not intended to happen but it appears that the before_update callback is issued even when the positioning column wouldn't be touched. It creates a lot of unnecessary calls to the db.

https://github.com/brendon/positioning/blob/ead38ac95cabca002a16b69a32afef3e9c36eb20/lib/positioning.rb#L53C1-L54C1

Hi @creativetags, the callback always runs because we need to determine within there if the position or scope (or both) have changed and then react. If a position is actually assigned from parameters (even if it's the same value) we force will_change because it could be that the scope has changed and we coincidentally want to position the item in the new scope at the same position it was in in the old scope:

send :"#{column}_will_change!"

Here's the code where we determine if we need to shuffle anything:

def update_position
clear_position if positioning_scope_changed? && !position_changed?
solidify_position
if positioning_scope_changed? || position_changed?
move_out_of_the_way
if positioning_scope_changed?
contract(positioning_scope_was, position_was..)
expand(positioning_scope, position..)
elsif position_was > position
expand(positioning_scope, position..position_was)
else
contract(positioning_scope, position_was..position)
end
end
end

Can you let me know a scenario where nothing changes (including you not just including the current position in your params) where any db calls are made?

Ok, I misunderstood. Thanks for explaining

That's all good. Glad it wasn't anything serious :)