brendon / ranked-model

An acts_as_sortable/acts_as_list replacement built for Rails 4+
https://github.com/mixonic/ranked-model
MIT License
1.09k stars 134 forks source link

How to detect that `rearrange_ranks` or `rebalance_ranks` has been executed? #190

Closed ofnhwx closed 1 year ago

ofnhwx commented 2 years ago

We are creating an application that can reorder items in real time using the values in the position column. The application uses the cached value from the position column to display the items in order. Therefore, if rearrange_rank or rebalance_rank is called and the value in the position column of another record is updated, the cached value must also be updated.

Is there a better way to do this?

brendon commented 2 years ago

Hi @ofnhwx, you shouldn't use the database column directly but you can sort by it. You can them move things about by specifying their new position. Not sure if that helps?

ofnhwx commented 2 years ago

Thanks, @brendon.

Reordering of items is implemented declaratively in the front side application using JavaScript, it is a bit difficult to deal with using only the positions of individual updated items.

I was thinking that if it is possible to know when a position has been rebalanced, then it could be handled by re-obtaining the positions for all records at that time.

There doesn't seem to be a way to detect that in the current implementation, but would it be possible to add that functionality if I created a PR?

brendon commented 2 years ago

Hi @ofnhwx, when someone reorders on the front end are you able to detect where that item ended up in the list (in terms of absolute position)? If so then you can just tell ranked model to move that item to that position and the rest will be taken care of.

If you're looking for more visibility of the actual position column then you might be better off with acts_as_list :)