Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Check arity for scopes #19

Closed
dogenpunk opened this issue Apr 19, 2011 · 11 comments
Closed

Check arity for scopes #19

dogenpunk opened this issue Apr 19, 2011 · 11 comments

Comments

@dogenpunk
Copy link

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.

@ernie
Copy link
Contributor

ernie commented Apr 19, 2011

I agree. I'd be happy to accept a patch with tests for this.

@dogenpunk
Copy link
Author

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!

@dogenpunk
Copy link
Author

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?

@dogenpunk
Copy link
Author

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.

@ernie
Copy link
Contributor

ernie commented May 6, 2011

Yeah, that actually occurred to me when you mentioned it -- seems sloppy but would work just fine.

@ernie
Copy link
Contributor

ernie commented Jun 22, 2011

@dogenpunk any progress on a pull request?

@dogenpunk
Copy link
Author

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.

@ernie
Copy link
Contributor

ernie commented Jun 24, 2011

OK. I'll close this one for now, but happy to reopen if things get sorted on the core side.

@ernie ernie closed this as completed Jun 24, 2011
@gamov
Copy link

gamov commented Dec 21, 2012

mmm, so this is the root of my problem (#86 (comment))
I've been battling with meta_search to add a checkbox to activate or not a scope with a lambda without success.
The only way I find is described in the linked comment.
I think it wouldn't be too bad to add a parameter to the search_methods to specify that the defined methods/scopes can be activated/deactivated by a checkbox...

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 :formatter => Proc.new {|param| nil} but it doesn't work.
Any help?

@afurmanov
Copy link

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.

@hachpai
Copy link

hachpai commented Jul 15, 2015

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.

filterrific_available_filters.each do |filter_name|
    filter_param = filterrific_param_set.send(filter_name)
      next if filter_param.blank? # skip blank filter_params
      begin
        ar_rel = ar_rel.send(filter_name, filter_param)
      rescue ArgumentError #if we have a scope with arity 0 or enum query, we can perform the request without the parameter
        ar_rel = ar_rel.send(filter_name) if (filter_param == 1)
      end
    end
    ar_rel
end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants