Require a scope to be present before re-ordering
tomchapin opened this issue · 3 comments
I'm running into an issue where acts_as_list is attempting to re-order every single item that isn't associated with the scope.
For example, we have a bunch of images that are associated with products, and acts_as_list is scoped to product_id, like so:
acts_as_list scope: :product, column: :order_index
The problem is that we also have numerous images in the system which are NOT associated with any product. This means that every time one of those disassociated images is touched, acts_as_list attempts to re-order hundreds of thousands of images in the system that have a null product id.
Is there a way to make act_as_list skip the re-ordering process if the required scope isn't actually defined?
We came up with this really hacky way of doing it, but I'm not happy with it. Surely there's a better way?
acts_as_list scope: 'images.product_id = #{(product_id.present? ? product_id : false)}', column: :order_index
Hi @tomchapin, thanks for reaching out :)
I think currently NULL
is considered just another value for the scope (in addition to integer id's).
I'd imagine if we want to make acts_as_list
support ignoring NULL
scope id's, it'd have to be written as a new feature.
I currently don't have time to do this myself, but if you feel comfortable creating a PR with a well tested feature addition then I'd be more than happy to review it and get it into the gem :)
I'm thinking that it'd be an option passed to acts_as_list
that is off by default so it doesn't interfere with existing implementations that might rely on NULL
being sortable too.
Hope that helps! :)
Closing due to lack of followup. @tomchapin, if you want to explore this further let me know. I'd say check out my new positioning gem: https://github.com/brendon/positioning but even in that gem we assume an item is always in the list. You can pretend it isn't by ignoring say a boolean scope when it's false
but we'll still maintain positions within that false
scope.