Hi,
I've been trying to develop a Rails gem/plugin with generators for
Rails 3. I first had a separate gem project with a gem statement in my
Gemfile with a :path option to point at it. But then I had to run $
rake install on each change in my gem to have Rails pick up on it. I
have now instead put my generators inside RAILS_ROOT/lib so they are
easier to test/develop.
lib/generators
- ability
ability_generator.rb
- clear
- config
When I run $ rails g
...
AuthAssist:
auth_assist:ability
auth_assist:clear
auth_assist:config
$ rails g auth_assist:ability
Could not find generator auth_assist:ability.
What am I doing wrong here? Must the generators be in a gem/plugin to
be picked up by Rails?
What is the best/easiest approach for developing plugins/generators
with Rails 3?
Thanks!
Hi,
I’ve been trying to develop a Rails gem/plugin with generators for
Rails 3. I first had a separate gem project with a gem statement in my
Gemfile with a :path option to point at it. But then I had to run $
rake install on each change in my gem to have Rails pick up on it. I
have now instead put my generators inside RAILS_ROOT/lib so they are
easier to test/develop.
lib/generators
-
ability
ability_generator.rb
-
clear
-
config
When I run $ rails g
…
AuthAssist:
auth_assist:ability
auth_assist:clear
auth_assist:config
$ rails g auth_assist:ability
Could not find generator auth_assist:ability.
What am I doing wrong here? Must the generators be in a gem/plugin to
be picked up by Rails?
What is the best/easiest approach for developing plugins/generators
with Rails 3?
Thanks!
This might help… http://github.com/indirect/rails3-generators
I have not really looked into generators recently but I am guessing not much has changed.
Anuj
I worked it out myself. It had to do with the fact that the generators
for some reason require the namespace to match a containing folder,
much like the Java package mechanism.
lib/generators
- auth_asisst
- ability
ability_generator.rb
# ability_generator.rb
module AuthAssist
module Generators
class AbilityGenerator < Rails::Generators::Base
...
A bit akward IMO, that a generator can be picked up as available but
then can't be found when attempting to run it.
Would be nice with a better error message such as "can't find x:y in
generators/x/y_generator.rb as expected."
I worked it out myself. It had to do with the fact that the generators
for some reason require the namespace to match a containing folder,
much like the Java package mechanism.
lib/generators
-
auth_asisst
-
ability
ability_generator.rb
ability_generator.rb
module AuthAssist
module Generators
class AbilityGenerator < Rails::Generators::Base
…
A bit akward IMO, that a generator can be picked up as available but
then can’t be found when attempting to run it.
Would be nice with a better error message such as "can’t find x:y in
generators/x/y_generator.rb as expected."
Yeah, I have felt the need for a proper error message otherwise you can’t really tell what is going on. I will add a Lighthouse ticket for the same.
Anuj
On another note:
I would like to have available the methods used in Rails 3 templates.
I see many generators including Rails::Generators::Migration and using
the method migration_template to create a new migration.
What I would like is for my generator to create the options for
running a basic migration command, say:
$ rails g migration add_user_name_to_user user_name:string
But to be run from inside my migration. I tried the following
migration 'add_user_name_to_user user_name:string'
But the migration method is not available by default and I can't seem
to find it anywhere... any idea how to do this?
I could hack it using "run" which works, but produces:
run rails g migration add_admin_field_to_user admin:boolean from "."
Which just doesn't feel right
Thanks!
On another note:
I would like to have available the methods used in Rails 3 templates.
I see many generators including Rails::Generators::Migration and using
the method migration_template to create a new migration.
What I would like is for my generator to create the options for
running a basic migration command, say:
$ rails g migration add_user_name_to_user user_name:string
But to be run from inside my migration. I tried the following
migration ‘add_user_name_to_user user_name:string’
But the migration method is not available by default and I can’t seem
to find it anywhere… any idea how to do this?
I could hack it using “run” which works, but produces:
run rails g migration add_admin_field_to_user admin:boolean from “.”
Which just doesn’t feel right
Thanks!
Just have a look at rails3-generators in github. It has got generators for datamapper, haml and so on. I am sure you can find clues there.
On a further note. I can't seem to get any .rb files in RAILS_ROOT/lib
to load when I start my rails server.
Say I have a file
# lib/auth_assistant.rb
puts "Hello from AuthAssist"
On a further note. I can’t seem to get any .rb files in RAILS_ROOT/lib
to load when I start my rails server.
Say I have a file
lib/auth_assistant.rb
puts “Hello from AuthAssist”
–
In the old days of Rails 2.3+ you would put a require statement in
application.rb, but I think in Rails 3 all files in lib should be
loaded by default!?
How do I configure my lib files to be loaded in Rails 3?
I think you still need to require it in the application.rb file. I might be wrong as I have not been following the Rails-core development as much so don’t take my word for it.
Anuj
It turned out I had to include the require statement in an
initializer. Putting it in the application.rb didn't work (not found
error). I has to due with the load order I'm sure.
Thanks for the help