heartcombo/has_scope

adding named parameters / keyword arguments

nruth opened this issue · 3 comments

nruth commented

Given a model scope like this

scope :for_course, lambda { |course_id:| where(course_id: course_id) }

and a controller like this

has_scope :for_course, using: :course_id, type: :hash

I get this error

wrong number of arguments (1 for 0)

I think it's because the model scope gets called like for_course(bla) instead of for_course(course_id: bla).

Am I doing this wrong, or is it something that isn't supported yet?
What might be a good api for adding it? I guess another has_scope option in the controller is needed?

nruth commented

Just realised block scopes support this already. It feels like a bit of a work-around given this is a first-class language feature now, but it does work:

has_scope :for_course do |controller, scope, value|
  scope.for_course(course_id: value)
end

Because of this I think two possibles PRs would make sense:

  • A: not changing the code, but mentioning this in the readme;
  • B: adding keyword arguments to the api somehow, with the aim to make the above a 1-liner without adding a lot of lib complexity

Thoughts?

A: not changing the code, but mentioning this in the readme;

I'm 👍 for this - @nruth do you want to write a Pull Request for this?

nruth commented

Yes, made it, closing this.