Forum: Radiant CMS ActionView helpers in a behavior - I think I got it!

26ed4a46e321f6f0f77a55b228a47145?d=identicon&s=25 Jay Levitt (Guest)
on 2006-07-18 01:38
(Received via mailing list)
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
Fc559d53343b6d7fae00a04fcf0777dc?d=identicon&s=25 Andrew Hodgkinson (pond)
on 2006-07-18 13:04
Jay Levitt 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.
26ed4a46e321f6f0f77a55b228a47145?d=identicon&s=25 Jay Levitt (Guest)
on 2006-07-18 19:07
(Received via mailing list)
Andrew Hodgkinson wrote:
> Jay Levitt 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
Fc559d53343b6d7fae00a04fcf0777dc?d=identicon&s=25 Andrew Hodgkinson (pond)
on 2006-07-18 19:21
Jay Levitt 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.
26ed4a46e321f6f0f77a55b228a47145?d=identicon&s=25 Jay Levitt (Guest)
on 2006-07-18 19:53
(Received via mailing list)
Andrew Hodgkinson 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 Levitt
6ec6f77ea603dd75b3a7a7775b059e79?d=identicon&s=25 John W. Long (Guest)
on 2006-07-19 04:59
(Received via mailing list)
Jay Levitt 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 Long
http://wiseheartdesign.com
26ed4a46e321f6f0f77a55b228a47145?d=identicon&s=25 Jay Levitt (Guest)
on 2006-07-19 05:02
(Received via mailing list)
John W. Long wrote:
> Jay Levitt 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
6ec6f77ea603dd75b3a7a7775b059e79?d=identicon&s=25 John W. Long (Guest)
on 2006-07-19 05:18
(Received via mailing list)
Jay Levitt 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 Long
http://wiseheartdesign.com
This topic is locked and can not be replied to.