-
Notifications
You must be signed in to change notification settings - Fork 527
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
Needs to provide "controller" for helper_method helpers #12
Comments
I think this is an easy fix, but I need to build a sample devise app and try it out. I'm an all-OmniAuth guy these days :) |
Cool. You shouldn't even need to build a devise app; just look at how devise uses helper_method, and create a helper the same way. (Which means you can even add a spec for it!) |
I ran into this same issue, helpers which require access to controller. Have you thought about sending the controller's |
I have tried all kinds of hackery to figure out the current controller without having to pass it in during the decorator instantiation, but all efforts have so far failed. |
I actually did this yesterday morning after making the comment above, essentially replacing helpers with the controller's I wonder if storing the current controller in a before filter in a thread local variable would work? |
This is essentially what we're doing in our app. Icky, but it works:
|
I'll probably end up combining this with my prior Essentially:
I just wonder, is this somehow against the design philosophy of this gem? |
|
Sorry didn't mean to be confusing there. It doesn't exist, it's just something I wrote as an example to what I was trying to do. I did implement it this afternoon, along with my override of
Working perfectly for me so far. |
Oh shit, I think I understand what you're doing here. This could be a really excellent idea that solves multiple problems. I need to experiment more. |
+1 If that actually works, it should enable |
No gotchas so far. I've been using this approach for a few months now and it's all worked fine. That's essentially the entirety of the code in the above comment. The |
I just released 0.8.0 using this technique. Would you let me know if it helps the original issue? |
@jcasimir so it looks like this may actually work but it broke something else. The way the code is currently written, it's saying that there will definitely be a What ends up happening is that, in the decorator's unit tests, I see one of two options, either:
Thoughts? |
I'm getting some odd behaviour: |
Ok, I decided to go with #2 above. This is what I've come up with so far and it seems to be working okay after about 20 minutes of messing with it:
Then in my spec all I have to do is:
The source for this was a SO question here: http://stackoverflow.com/questions/4262044/rails-3-how-to-render-erb-template-in-rake-task The only other problem with doing it this way is that all of the custom application helpers need to be included manually. @jcasimir can we use some of the code that was originally used in My initial thought was to put this in the README but I'm concerned that the 'knowledge of the masses' won't be respected there. In other words, if there's a problem, people are likely to change it in their app and not modify the README to be correct. Now I'm thinking it'd be better off as |
After implementing the code snippets by @numbers1311407, my issue is gone... thanks! It would be nice to have it work out of the box, though -- especially for newbie Rails devs like myself. ;) |
Arg, I see some issues that the changes have brought up. I will do my best to carve out some time tomorrow night. |
@jcasimir: I think the issue @rocketscientist was having with the persisting |
@numbers1311407 Yup, you're right, I had the same issue and "Thread.current[:current_view_context] = self.view_context" seems to solve the problem. I'm not familiar with the project though, Am I missing something? |
More notes regarding testing the decorators in isolation: To use anything which requires configuration variables from the controller, you'll need to create a controller instance and pass that to your ActionView instance. Adding on to what I mentioned above, you would have:
Using this, you should be able to use things like |
@jfelchner What if you went at this from the opposite approach, delegating to a stubbed controller for
|
Looks like we've got this covered. |
@numbers1311407 Awesome. I'm not very familiar with how all that stuff is wired up. Your solution worked perfectly. Thank you! |
For me, this works, quick and dirty:
|
@fpellanda I don't understand what's happening here. I don't know which version you're using but in current versions
|
As far as I can see in my source code (draper 0.9.5)
And this would return:
Which has, in my application, has not the local variables from the controller set. But your right, this would be nicer:
|
The point is that |
I know they should, but they are really not the same: I'm using Rails 3.1.3 and draper 0.9.5. How to reproduce:
remove javascript stuff from application layout (gave me an error) Add to post_decorator.rb
Decorate @post in show action of posts_controller.rb:
When you now create a post and goto show, you will see in the title, that the My output of http://127.0.0.1:3000/posts/1 : Title: This means, if you want to use @post in a helper, it will be nil. |
This is a good catch. The problem you're seeing happens because the
In most cases it's a non-issue because, well, the intention of In other words, if your decorators are dependent on action-specific instance variables, you're probably doing it wrong (in this case, _That being said_, the current implementation is problematic, if only because it introduces the possibility for inconsistent behavior based on filter order. Perhaps it should only store a reference to the current controller, rather than caching a Something like my original suggestion:
|
Yes I know that I shouldn't use @post in the helper - this was just an example, in my case I set an instance variable in a before filter to change behaviour in some helper methods. |
I'm using Devise, which generates its helpers using
AbstractController#helper_method
. That works like so:So the helper ends up wanting to call
controller
, and that's nil since you don't proxy it. Any ideas how to solve it?The text was updated successfully, but these errors were encountered: