Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

app/helpers within plugin not being mixed in

#1905

I have a plugin (engine) with an app directory. Everything works fine except the plugin/app/helpers/* are not being mixed in.

To get the project to run I have to copy the modules to the app/helpers directory.

The helpers are being loaded however (I created a ZHelper in plugin/app/helpers/z_helper.rb and it was defined within the console)

Not sure how best to proceed - any advice?

BTW this is using the edge gem from gems.rubyonrails.org

Reported by Jonah Fox · February 7th, 2009 @ 12:25 PM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Jonah Fox
    Jonah Fox

    I found that by explicitly including the helper from plugin/init.rb, like:

    require File.join(File.dirname(FILE), "app", "helpers", "users_helper.rb")

    Is this expected behaviour ?

    Thanks

    February 7th, 2009 @ 03:02 PM

  2. Jonah Fox
    Jonah Fox

    --- meant to say ---

    I found that it works if I explicitly include the helper from plugin/init.rb, like:

    require File.join(File.dirname(FILE), "app", "helpers", "users_helper.rb")

    Is this expected behaviour ?

    Thanks

    February 7th, 2009 @ 03:03 PM

  3. Jonah Fox
    Jonah Fox

    In fact this only works if there's already a users_helper.rb within the main project.

    If not then I have to do:

    ActionView::Base.send :include, UsersHelper;

    within init.rb.

    I think the same problem of having to explicitly include files may be true of models aswell.

    Hope this helps

    Jonah

    February 7th, 2009 @ 03:26 PM

  4. Andrew White
    Andrew White

    Whether it's the desired behaviour for helper :all to include all of the helpers in engines as well as application helpers is something that probably needs to be discussed on the rails core mailing list. However the change is easy if you want to create a patch - just add "#{RAILS_ROOT}/vendor/plugins//app/helpers//*_helper.rb" to the Dir[] call in ActionController::Helpers.all_application_helpers.

    You don't need to call ActionView::Base.send in the engine's init.rb file - you can just use helper :helper_name in ApplicationController. It's arguable that this the preferred behaviour since the developer may be using a engine developed by someone else and may be unaware of conflicting helper methods defined in a engine's helper file.

    February 7th, 2009 @ 07:05 PM

  5. Andrew White
    Andrew White

    That path should've been: "#{RAILS_ROOT}/vendor/plugins/**/app/helpers/**/*_helper.rb"

    February 7th, 2009 @ 07:10 PM

  6. Josh N. Abbott
    Josh N. Abbott

    It would seem to me that in order to remain consistent with the fact that controllers, models and views are getting included, the helpers should be included by default as well.

    Thanks for the patch either way.

    -- Josh

    March 26th, 2009 @ 07:49 PM

  7. shm
    shm

    it seems though, that the helper modules inside plugins/engines are added to the load_path, so if you specify your helper manually inside your controllers, it should work fine.

    helper :my_plugin_helper

    ..only the "helper :all" approach doesn't work for helper-files inside engines.

    (the "helper" method in your controller 'constantizes' the corresponding module-name and includes it into ActionView::Base).

    March 28th, 2009 @ 07:18 PM

  8. James Adam
    James Adam

    It does seem sensible that helper :all would load all helpers, even those in plugins.

    One minor wrinkle is that it still wouldn't be possible for gems to provide helpers, unless they're stored in the vendor directory. This would be quite easy to implement if Ticket #2526 was deemed worthy.

    Regardless, adding helpers underneath vendor/plugins/**/app/helpers and vendor/gems/**/app/helpers seems like a great first step.

    May 2nd, 2009 @ 03:30 PM

  9. adlongwell
    adlongwell

    +1 I agree with James.

    May 5th, 2009 @ 06:59 PM

  10. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open

    +1, plugins should expose their helper dirs

    May 5th, 2009 @ 07:08 PM

  11. jesse (at jesseclark)
    jesse (at jesseclark)

    +1 for plugin helpers getting picked up by helpers :all

    May 18th, 2009 @ 06:54 PM

  12. Daniel Guettler
    Daniel Guettler
    • Tag changed from engines, helpers, plugins to engines, helpers, patch, plugins, test

    +1, I had this added in older Rails versions as well. I attached an actual patch to make this happen.

    July 24th, 2009 @ 08:37 PM

  13. Jeremy Kemper
    Jeremy Kemper
    • Assigned user set to Jeremy Kemper

    Rather than have two separate settings for specific directories, how about changing this to an array of helper paths instead?

    August 1st, 2009 @ 07:54 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  15. Victor Costan
    Victor Costan

    It's really sadist to have bugs like this lying around. I spent over an hour trying to figure out what's broken in my plugin, because I was getting my controllers included but not my helpers.

    May 28th, 2010 @ 07:53 AM

  16. Andrea Zaupa
    Andrea Zaupa
    • Importance changed from to

    This could help ?

    only mixin in all helpers in the plugins with ApplicationController.helper .

    in init.rb

    Include hook code here

    config.to_prepare do

    (Dir[File.dirname(FILE) + "/app/helpers/_helper.rb"]+Dir[File.dirname(FILE) + "/app/helpers/**/_helper.rb"]).each do |file|

    h ="Admin::#{File.basename(file, '.rb').gsub(/_helper$/, '').camelize}Helper".constantize
    ApplicationController.helper h
    

    end

    end

    July 22nd, 2010 @ 11:26 AM

  17. Daniel Guettler
    Daniel Guettler

    @Jeremy : The reason why I kept them separate is because they need slightly different treatment to actually get the helpers. Therefor I think it is best to keep this separated. I'm using this patch in production since I posted it without any problems. I think this patch should be applied to the 2.x branch to save people the time of debugging it.

    July 22nd, 2010 @ 01:53 PM

  18. Antonio Tapiador
    Antonio Tapiador

    +1 for plugin helpers getting picked up by helpers :all This is what I expected, I was in the same situation than Victor!

    September 24th, 2010 @ 03:52 PM

  19. Piotr Sarnacki
    Piotr Sarnacki

    Could you check your gems/plugins against rails/master? It should already work there: http://github.com/rails/rails/blob/master/railties/test/railties/en... I did this as a part of my work on extending engines.

    September 25th, 2010 @ 12:40 AM

  20. José Valim
    José Valim
    • State changed from open to resolved
    • Assigned user changed from Jeremy Kemper to José Valim

    Fixed on master.

    October 11th, 2010 @ 05:00 PM

  21. Piotr Sarnacki
    Piotr Sarnacki

    For those who can't use rails master and don't want to wait for 3.1, I've created small railtie that fixes problem described in that ticket: http://github.com/drogus/rails_helpers_fix

    Just add that line to Gemfile and run bundle install:

    gem "rails_helpers_fix"
    

    October 12th, 2010 @ 03:31 PM

  22. Robert Glaser
    Robert Glaser

    Thanks for providing this Hotfix, Piotr! But there's an issue with that: If there's a helper FooHelper in the main app, and a FooHelper in the engine/plugin, this doesn't work. It only works if the engine/plugin helpers is being renamed to an unused name.

    October 19th, 2010 @ 11:37 AM

  23. Piotr Sarnacki
    Piotr Sarnacki

    Oops, that's weird. I'll fix it today.

    October 19th, 2010 @ 12:50 PM

  24. Piotr Sarnacki
    Piotr Sarnacki

    Robert Glaser: After looking at code and rethinking that, I think the only sane way to handle that is namespacing helpers in your plugin. From Ruby point of view, declaring module twice will work since second one will just reopen first module, but it will be hard to change a way of loading helper files in Rails.

    October 19th, 2010 @ 01:40 PM