-
Notifications
You must be signed in to change notification settings - Fork 127
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
Sorting in admin sortable index miscalculates indices when using acts_as_list plus ancestry #72
Comments
Hmm.. I've yet to use ActsAsList. Do you have a sample app to illustrate? I do know that the indexes are only calculated from zero at the top most node then it iterates from top to bottom incrementing the indexes to be saved to the DB. Are you managing the list in two separate locations with only portions of the overall tree (a subtree)? |
I have a sample app, but unfortunately it's private. I could give you read-only access if it would help. I'm not using a subtree. You're right that, by default, acts_as_list calculates indices from the top of the list. But you can give it a scope - meaning another field within the same model - and then it will calculate an index for each unique scope value. When using with the ancestry gem, the scope is the ancestry field, which is ancestry's field for storing the ancestors of a given node as a colon-delimited list. Here's the model code:
My examples above demonstrate expected and actual position columns when using acts_as_list methods to resort vs. using active_admin-sortable_tree. That's because active_admin-sortable_tree assumes there will be a single index sequence for position, so it doesn't really work with this combination. I had to fork active_admin-sortable_tree and completely rewrite the internals of the sort method to work correctly for this use case. My hacky code isn't suitable for a PR - I pretty much destroyed active_admin-sortable_tree's ability to work in any other use case - but I could see a couple of ways of extending the gem to keep it agnostic but also support this use case:
If either of those appeals, I'd be happy to submit a PR. Either way, the documentation for active_admin-sortable_tree specifically mentions both acts_as_list and ancestry, so the documentation could probably be updated to call out incompatibility with this specific combination. |
By the way, here's my hacky code:
And here's the #resort method I call above, using the raw
|
Great detail, thank you. I'll read up more on ActsAsList to be more informed before I can decide on the best action to take. |
I'm using Acts as List + Ancestry to create a sortable tree for my model, and I'm using this gem to generate a drag-and-drop sortable index.
Relevant code here:
Based on that combination, I expect that when I create a bunch of records, indices will be calculated starting at 0 in each subnode of my tree structure. So if I start with this:
I can then call this:
And end up with this:
The acts_as_list methods are correctly executing the move commands within the context of the scope, AND the positions within each level of the tree are calculated correctly, starting at 0 within each ancestry depth.
However, when I revert to the original ordering and perform the same operations within my admin panel, I end up with this:
As you can see, active_admin-sortable_tree is generating positions without regard to scope. This hoses the position sequence of my entire model because when I use acts_as_list methods directly in the future, it assumes the indices will be relative to scope: :ancestry.
Am I doing something wrong in configuring my admin panel? Or is this a bug? Please advise.
The text was updated successfully, but these errors were encountered: