ActionView helpers in a behavior - I think I got it!

Here’s the relevant parts of a navigation-tab plugin I’m in the process
of hacking together… and I have just successfully called image_path
from it. Hope this helps someone.

class Behavior::Base
define_tags do
tag “navtab” do |tag|
tab = NavTab.new(tag.attr[‘label’], @request)
# …
end
end
end

class NavTab
include ActionView::Helpers::AssetTagHelper

def initialize (label, request)
@label = label

@controller = ActionController::Base.new
@controller.request = request
p image_path('hi')

end

end

Jay L. wrote:

@controller = ActionController::Base.new
@controller.request = request

I think you might have to be careful with certain helper method calls -
ISTR seeing something about this on IRC. In particular some people
seemed to think that url_for() might give trouble.

Andrew H. wrote:

Jay L. wrote:

@controller = ActionController::Base.new
@controller.request = request

I think you might have to be careful with certain helper method calls -
ISTR seeing something about this on IRC. In particular some people
seemed to think that url_for() might give trouble.

Thanks. If I run into any weird side effects I’ll start by assuming
they’re not my fault now…

Jay

Jay L. wrote:

Thanks. If I run into any weird side effects I’ll start by assuming
they’re not my fault now…

Well, kind of; I think the “Right Answer” might be to modify the
Behavior API so that Radiant’s existing ActionController::Base instance
gets passed in as a parameter rather than generating a new
instantiation. You could default the parameter to ‘nil’ and use a new
instance of ActionController::Base if need be, for cases where your
modified Behavior code got installed into an older Radiant version.

Andrew H. wrote:

modified Behavior code got installed into an older Radiant version.

That’d be really nice, actually. I think that’s what I was
(unknowingly) asking for last month, when I got the advice to create a
dummy controller. John, do you think it might be possible to get at the
current ActionController from a behavior?

Jay L.

Jay L. wrote:

That’d be really nice, actually. I think that’s what I was
(unknowingly) asking for last month, when I got the advice to create a
dummy controller. John, do you think it might be possible to get at the
current ActionController from a behavior?

I’m open to the idea but am unsure that this is the best approach.


John L.
http://wiseheartdesign.com

John W. Long wrote:

Jay L. wrote:

That’d be really nice, actually. I think that’s what I was
(unknowingly) asking for last month, when I got the advice to create a
dummy controller. John, do you think it might be possible to get at the
current ActionController from a behavior?

I’m open to the idea but am unsure that this is the best approach.

What might be better, then? An awful lot of seemingly-unrelated helpers
in Rails need a controller, and if a dummy one causes problems, I can’t
see any other option than the real controller.

Jay

Jay L. wrote:

John W. Long wrote:

I’m open to the idea but am unsure that this is the best approach.

What might be better, then? An awful lot of seemingly-unrelated helpers
in Rails need a controller, and if a dummy one causes problems, I can’t
see any other option than the real controller.

But that’s just it. I’m not sure that behaviors should have full access
to the controller. It seems like it might be better for them not to rely
on that knowledge.

Sorry to be ambivalent. I will need to think about this.


John L.
http://wiseheartdesign.com