-
Notifications
You must be signed in to change notification settings - Fork 336
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
prepare_locals: Use Object#send instead of calling directly. #136
base: 1-4-stable
Are you sure you want to change the base?
Conversation
Object#respond_to? will return true for protected methods. However calling a protected method directly results in an exception. Using Object#send allows a protected method (like current_user) to be called.
Do you need this fix for your case? I don't remember anyone complaining about this, is this an issue for you? |
Yep. We define #current_user as a protected method because it's not an action (which would be public), and it's shared amongst different controller classes (so it's not private). |
Perhaps you could use this to address the problem: I don't think anyone makes #current_user protected/private, does it work as On 16 February 2014 20:24, Alex Zepeda [email protected] wrote:
|
In our case,
|
I understand why it's not working for you, but this helper has On 16 February 2014 20:46, Alex Zepeda [email protected] wrote:
|
If you need a method in controllers (and you clearly do) don't make it as protected. |
Regardless of my motivation (the authentication system was not of my design), respond_to? :foo ? object.foo : nil is unsafe. That's what the pull request is designed to address. |
using send looks like a hack, which I don't want. You can pass true as the second argument to respond_to? to ignore protected methods. Please also add a spec for this case. |
The second argument to respond_to? is for private, not protected methods. http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F
The default for include_private means that private, but not protected methods will be excluded. This will / has changed in ruby 2.x. http://tenderlovemaking.com/2012/09/07/protected-methods-and-ruby-2-0.html As for making the method public, everything I've come across indicates that non-action methods on a controller should be marked protected or private. ex: http://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails http://stackoverflow.com/questions/896556/do-you-ever-use-protected-visibility-in-rails |
I was just bitten by this as well. |
Actually my problem may be similar but different. I have a current_user helper method on my application controller that works everywhere in my application. When I call current_user from within a public_activity view it returns nil instead. What I expect to happen is that views within app/views/public_activity/... will use the same current_user helper method as elsewhere in my application. |
Why are the views within |
Object#respond_to? will return true for protected methods. However
calling a protected method directly results in an exception. Using
Object#send allows a protected method (like current_user) to be
called.