Same scope for different actions
youroff opened this issue · 3 comments
I have different actions rendering the collection in a different ways, and sort
scope with the default
option setting column name and the direction. However this column does not exist in the table rendered by one of three actions. Initially I was going to do something like this:
has_scope :sort, default: "duration.desc", except: :photo
has_scope :sort, default: "photo_request.asc", only: :photo
It didn't work, as has_scope call completely overwrites scope definition. I thought there was a way to change the name of the outer scope (key in params) while saving name called on model class, something like reversed as
option. But there wasn't.
Right now I'm going to declare an alias for sort scope on model and use different key. Or the same key with except/only. Any better way to solve it?
That's because we need to keep track of the scopes configuration, and that's keyed by the scope name you use, so I don't think there's a better way around this right now. Thanks.
I may be wrong, but I don't see any calls to scopes_configuration
by key (except the moment when it's filled up), so it may be an array, which will work fine, cause scopes are applied sequentially anyway. In this case except
and only
will work for my initial attempt.
So I could try to propose some solution as a pull-request if you accept that...
It's what we use to call the model scopes when you call apply_scopes
. There might be other ways of implementing that, though, I'm not entirely sure it needs to be a hash, but it's what has been working so far. If you want to go ahead and try to propose something else, as long as it is mostly backwards compat (everything should work, except of course scopes_configuration wouldn't be a hash anymore), we'd be glad to review and discuss. Thanks!