Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grape::DSL::Helpers::ClassMethods#helpers modifies helper module #1335

Open
sevaorlov opened this issue Mar 22, 2016 · 3 comments · May be fixed by #1338
Open

Grape::DSL::Helpers::ClassMethods#helpers modifies helper module #1335

sevaorlov opened this issue Mar 22, 2016 · 3 comments · May be fixed by #1338

Comments

@sevaorlov
Copy link

Hi, Grape::DSL::Helpers::ClassMethods#helpers evaluates helper methods in context of a given helper module, that leads to problems if that helper module is used anywhere else.

Example

helpers MyHelperModule do
  def resource
    ...
  end
end

now if I include MyHelperModule anywhere else, I'll have resource method, that I don't want to include

Update:
https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/helpers.rb#L31

@dblock
Copy link
Member

dblock commented Mar 22, 2016

But that's the whole point of helpers, no? What do you suggest?

@sevaorlov
Copy link
Author

@dblock, you see, we want to add helper module to several endpoints and also we want to add unique resource method to each of these endpoints

it is possible with

helpers do
  include MyHelperModule

  def resource
    ...
  end
end

but grape allows us to do it with

helpers MyHelperModule do
  def resource
    ...
  end
end

and with that code resource method is added to MyHelperModule twice and the last added resource method is used in both endpoints

I suggest to always create anonymous module and include module passed in new_mod in that anonymous module. https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/helpers.rb#L31

I can create a request if you prefer.

@dblock
Copy link
Member

dblock commented Mar 25, 2016

Ok that makes total sense @vsorlov. I wonder whether this is a feature through, in Ruby if you write module MyHelperModule you'd expect it to reopen a previous one. On the other hand, we could consider modules namespaces, and maybe what you suggest has less of a surprise. I think you should make a pull request either way.

@sevaorlov sevaorlov linked a pull request Mar 25, 2016 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants