This project is archived and is in readonly mode.
Rails 3 generators won't invoke custom generators
Reported by Paul Odeon | December 9th, 2010 @ 01:43 PM
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 = "utf-8"
config.filter_parameters += [:password]
config.generators do |g|
g.helper :my_helper
end
~ rails generate scaffold Post body:text ... error my_helper [not found]
end end
Comments and changes to this ticket
-
Piotr Sarnacki December 10th, 2010 @ 09:05 AM
Confirmed on both 3.0.3 and master. I can take a look at that.
-
Paul Odeon December 10th, 2010 @ 09:09 AM
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
-
Piotr Sarnacki December 10th, 2010 @ 09:42 AM
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.
-
Paul Odeon December 10th, 2010 @ 10:02 AM
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.
-
Paul Odeon December 10th, 2010 @ 10:26 AM
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.
-
José Valim December 10th, 2010 @ 10:34 AM
- 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?
-
Paul Odeon December 10th, 2010 @ 11:32 AM
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.
-
José Valim December 10th, 2010 @ 11:48 AM
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.
-
Piotr Sarnacki December 10th, 2010 @ 11:56 AM
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?
-
Paul Odeon December 10th, 2010 @ 12:56 PM
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?
-
Piotr Sarnacki December 10th, 2010 @ 02:01 PM
- 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 :)
-
Paul Odeon December 10th, 2010 @ 02:28 PM
Should I issue a pull request in the same way as you've done for the generator patch?
-
Piotr Sarnacki December 10th, 2010 @ 02:30 PM
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.
-
Paul Odeon December 10th, 2010 @ 02:33 PM
Ok, I will give it a try and hopefully it gets accepted!
Cheers
Paul
-
José Valim December 10th, 2010 @ 03:09 PM
Documentation patches can be pushed straight to lifo/docrails project on github
-
Paul Odeon December 10th, 2010 @ 03:38 PM
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?
-
José Valim December 10th, 2010 @ 03:45 PM
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
-
Piotr Sarnacki December 10th, 2010 @ 03:53 PM
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.
-
Piotr Sarnacki December 11th, 2010 @ 09:55 AM
- Tag set to patch
-
Repository December 11th, 2010 @ 10:42 AM
- State changed from new to resolved
(from [7891de893951c780a1732747d430c33e998dd573]) Allow to generate namespaced generators [#6140 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/7891de893951c780a1732747d430c... -
Repository December 15th, 2010 @ 08:40 PM
(from [e6928e8c35bfc22425916f334e9482f955bc8907]) Allow to generate namespaced generators [#6140 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/e6928e8c35bfc22425916f334e948...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 6140 Rails 3 generators won't invoke custom generators (from [7891de893951c780a1732747d430c33e998dd573]) Allow t...
- 6140 Rails 3 generators won't invoke custom generators (from [e6928e8c35bfc22425916f334e9482f955bc8907]) Allow t...