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.

Rails 3 generators won't invoke custom generators

#6140

I'm following the instructions from Rails Guides: http://guides.rubyonrails.org/generators.html on creating a custom generator

Have also tried on edge rails with the same results

~ ruby -v ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
~ rails -v Rails 3.0.3
~ rails new testapp ... ~ cd testapp ~ rails generate generator my_helper ... ~ mate config/application.rb

require File.expand_path('../boot', FILE)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
module Testapp
  class Application < Rails::Application

config.encoding = &quot;utf-8&quot;
config.filter_parameters += [:password]
config.generators do |g|
  g.helper          :my_helper
end



end end
~ rails generate scaffold Post body:text ... error my_helper [not found]

Reported by Paul Odeon · December 9th, 2010 @ 01:43 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Piotr Sarnacki
    Piotr Sarnacki

    Confirmed on both 3.0.3 and master. I can take a look at that.

    December 10th, 2010 @ 09:05 AM

  2. Paul Odeon
    Paul Odeon

    Thanks and sorry about the dodgy formatting.

    I'm still looking into it myself and I've got as far as the Rails::Generators.find_by_namespace class method which I'm guessing is the source of the problem.

    P

    December 10th, 2010 @ 09:09 AM

  3. Piotr Sarnacki
    Piotr Sarnacki

    It seems that you need to have generator in form of MyHelper::ScaffoldGenerator or Rails::MyHelperGenerator, I think that's because there is no hook for :my_helper in scaffold generator.

    December 10th, 2010 @ 09:42 AM

  4. Paul Odeon
    Paul Odeon

    I can confirm that setting up the generator in this way works.

    Is this a feature or a bug?

    I think the expectation would be that the default rails generated generators work out of the box with the rails generators.

    This would mean that either the generated generators are created in the form MyHelper::ScaffoldGenerator, or that the search path for generators is expanded to include toplevel generators like MyHelperGenerator.

    As to which is the right way to go, I'm not sure, although I'd lean towards expanding the search path.

    December 10th, 2010 @ 10:02 AM

  5. Paul Odeon
    Paul Odeon

    I have created a patch which will expand the search path to include toplevel generators like MyHelperGenerator.

    I'm not sure if it will break anything else though and I'm unsure how to proceed from here.

    December 10th, 2010 @ 10:26 AM

  6. José Valim
    José Valim
    • Importance changed from to Low

    It is a feature. Rails needs to look for things inside namespaces, otherwise we would easily have conflicts. Maybe we should update the guide instead?

    December 10th, 2010 @ 10:34 AM

  7. Paul Odeon
    Paul Odeon

    Perhaps, but it doesn't seem possible to plug in the generated generators without modifying their name and putting them into a new module.

    This doesn't seem right.

    I understand rails needs to namespace classes, but by default rails generators are put in the global namespace.

    Unless this is changed it seems to me that the generator namespace search should be expanded to search the global namespace for its generators.

    December 10th, 2010 @ 11:32 AM

  8. José Valim
    José Valim

    Paul, it searched the global namespace but that was causing bugs, like loading improper generators, or even generators from other frameworks, like Merb. If you want to use a generator as a Rails hook, it needs to be inside the Rails namespace.

    Note that all Rails generators are namespaced inside "Rails::". But they are allowed to be invoked as "rails:generator" as shortcut.

    Generators that are not used as hooks, like devise, do not need to be namespaced.

    December 10th, 2010 @ 11:48 AM

  9. Piotr Sarnacki
    Piotr Sarnacki

    I think that best solution to that, would be to add proper namespacing to generators, so you can do:

    rails g generator rails/my_helper
    

    Currently it will generate lib/generators/my_helper instead of lib/generators/rails/my_helper.

    Also, documentation change would be nice.

    Paul: do you want to work on that or should I prepare the patch myself?

    December 10th, 2010 @ 11:56 AM

  10. Paul Odeon
    Paul Odeon

    Yes, I'd agree with that.

    As I've never submitted a patch myself before now I'd prefer to defer to you on this.

    I could however do the documentation side of it?

    December 10th, 2010 @ 12:56 PM

  11. Piotr Sarnacki
    Piotr Sarnacki
    • Assigned user set to José Valim

    I believe that it should work like that: https://github.com/rails/rails/pull/131

    Paul: feel free to update guide, I'm not native speaker, so you'll probably do it better :)

    December 10th, 2010 @ 02:01 PM

  12. Paul Odeon
    Paul Odeon

    Should I issue a pull request in the same way as you've done for the generator patch?

    December 10th, 2010 @ 02:28 PM

  13. Piotr Sarnacki
    Piotr Sarnacki

    Frankly, I don't know what is current best approach - I like pull requests as they are easier to review because of github's interface and you can easier commit additional changes.

    December 10th, 2010 @ 02:30 PM

  14. Paul Odeon
    Paul Odeon

    Ok, I will give it a try and hopefully it gets accepted!

    Cheers

    Paul

    December 10th, 2010 @ 02:33 PM

  15. José Valim
    José Valim

    Documentation patches can be pushed straight to lifo/docrails project on github

    December 10th, 2010 @ 03:09 PM

  16. Paul Odeon
    Paul Odeon

    Great, I'll start work on it now, but I'm assuming I don't want to push to docrails until the patch has been accepted?

    December 10th, 2010 @ 03:38 PM

  17. José Valim
    José Valim

    You can push documentation only patches straight to docrails. There is no need for the patch to be accepted. You may want to take a look at the documentation guidelines before doing so:

    http://guides.rubyonrails.org/api_documentation_guidelines.html

    December 10th, 2010 @ 03:45 PM

  18. Piotr Sarnacki
    Piotr Sarnacki

    José Valim: I believe that Paul was talking about my patch: https://github.com/rails/rails/pull/131

    I'm not sure if this is desired behavior but I would expect it to work like that.

    December 10th, 2010 @ 03:53 PM

  19. Piotr Sarnacki
  20. Repository
  21. Repository