-
-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reorder positions #233
Comments
Hi @MarcReniu, there isn't currently a method to clean up the list orderings. We're always keen to improve the library. If you have any ideas (and PR's) that can help, we'll do our best to see them merged. One feature I'd like is an auto-repair process that fixes up duplicate position values in a list. |
I finally made a You could use it as an starting point, or throw it to the garbage, as you wish. module Ordenable
extend ActiveSupport::Concern
def valid_ordering? relation
records = self.send(relation)
records_class = records.class_name.constantize
top_of_list_config = records_class.acts_as_list_top
position_column = records_class.quoted_position_column.delete("`")
current_order = records.collect(&:"#{position_column}")
correct_order = (top_of_list_config...(records.size + top_of_list_config)).to_a
return current_order == correct_order
end
def correct_ordering relation
return if self.valid_ordering?(relation)
records = self.send(relation)
records_class = records.class_name.constantize
top_of_list_config = records_class.acts_as_list_top
position_column = records_class.quoted_position_column.delete("`")
new_positions = (top_of_list_config...(records.size + top_of_list_config)).to_a
records.each_with_index do |record, index|
record.update_column(position_column, new_positions[index])
end
end
end |
Thanks @MarcReniu, the important part is this:
which is what a lot of people do when processing a reorder request from the browser anyway (many drag and drop plugins send through just an array of We recently merged a PR that dealt with items having the same position integer. The fact that you have a gap between the I recently had to refactor some code where I had the id of the item being moved, and an array of ids that included the id of the item being moved:
Sorry, that's a bit rambling but it might give you some ideas :) I'll close this for now but do get in touch if you have any more issues :) |
Imagine you have the next elements with missing higher positions:
If I want to move position of ElementA to top of list, I can use
move_to_top
method, but in case I execute nextmove_higher
over ElementB, it becomes position 4, and ElementA, position 5.The final goal is to reorder all elements, considering
top_of_list
config. So, there is some method to perform it, or I have to do it manually?The text was updated successfully, but these errors were encountered: