-
Notifications
You must be signed in to change notification settings - Fork 140
Check arity for scopes #19
Comments
I agree. I'd be happy to accept a patch with tests for this. |
And I would be happy to write said patch & tests. I'm reading through the source now, I'll add a pull request when I've got it sussed. Thanks! |
So the problem that I'm running into with this is that scopes are wrapped up in lambdas. So calling arity on a scope always returns -1. I'm not really sure how to get at the inner lambda to check. Any ideas? Or would it be better/easier to pass some config in the search_methods method? |
The other option that just dawned on me is to define the scope using Proc to skirt the argument checking. But I feel dirty suggesting it. |
Yeah, that actually occurred to me when you mentioned it -- seems sloppy but would work just fine. |
@dogenpunk any progress on a pull request? |
Not much actually. I think this is actually an issue with the implementation of scope in Rails, but I haven't been able to figure out how that would have to change in order to expose the arity. And after some thought, the idea of passing in a param to define the number of required parameters in the search_methods method seems like a bad idea. I'll open an issue with the Rails team and see if I can get some feedback. |
OK. I'll close this one for now, but happy to reopen if things get sorted on the core side. |
mmm, so this is the root of my problem (#86 (comment)) Ok, the workaround i found is not satisfactory, but works. It doesn't help scopes created by gems: scope :with_qty, lambda { |*p| where(:id.lt => 100) }
search_methods :with_qty, :validator => Proc.new {|cb| cb == "1"} I tried to remove the parameter completely with |
It is possible to patch named scopes in Rails 3, so they would return a correct arity, here is an example: https://gist.github.com/4659655. The patch works for named scopes with arity 0 and 1, tough it is refactorable to be more general. |
Hello! I had the same expect for fiterrific. I've made a fork. As arity for scope and enums doesn't work as expected I use the ArgumentError exception.
|
When using a checkbox on a form to include a named scope in a search, the value of the form element is automatically passed to the scope regardless of whether or not the scope requires a parameter. I think that the arity should be checked before passing arguments.
The text was updated successfully, but these errors were encountered: