How to create Rails 3 migration generator?

Hi,

I’d like to add Rails 3 compatible generator to acts_as_taggable_on
gem (all it does is call “migration_template” method) and got a
problem with “next_migration_number” method not being implemented.

For ActiveRecord this method is defined in
ActiveRecord::Generators::Base class, which inherits from NamedBase
class. How can I use it inside my generator, which inherits from
Rails::Generators::Base? How to use “hook_for :orm” here, so that the
proper “next_migration_number” method definition is be used?

Cheers,
Szymek

If you look at the scaffold generator, it is able to apply the correct
model/migration generator depending on the orm setting in
application.rb.
This is handled transparently as I see it. It simply calls the correct
migration and model generators with a name corresponding to the orm
setting.
Check out my http://github.com/kristianmandrup/mongo_model_r3 for an
example. This can be used by
http://github.com/kristianmandrup/very_nifty_generators
Which contains a scaffold generator. This calls the mongo_mapper
generator if orm is set to :mongo_mapper and so on…
Something similar should be possible with migrations.

Also see: http://guides.rails.info/generators.html for more details :wink:

Good luck!

Kristian

Thanks, but the problem is that if I add the following line to my
generator:

hook_for :orm, :as => “migration”

it invokes i.e. ActiveRecord::Generators::Migration and it says that
the name attribute is missing, probably because AR::G::Migration
inherits from AR::G::Base, which inherits from
Rails::Generators::NamedBase. I don’t want to specify migration name,
as it’s already hardcoded in my generator, I just want to add a proper
prefix (i.e. timestamp), which is added by the “next_migration_number”
method.

All I need for my generator is to be able to access
“migration_template” method and “next_migration_number” depending on
ORM selected. It works fine if I simply copy “next_migration_number”
method from ActiveRecord::Generators::Base, but I doubt it’s the
proper way to do it :slight_smile:

Here’s how it looks currently: http://gist.github.com/290534.

BTW. Are there Rails 3 API docs available anywhere? I can’t generate
my own, because I got the following error: “wrong constant name ./DOC/
TEMPLATE/HORO”

So the solution for now is to make the “next_migration_number” method
inside my generator to call a class method.

However, assuming that generators for other ORMs like DataMapper or
Sequel also follow this convention, how can I check which ORM is used
by the application, so that I can change the path to the template and
call the proper ActiveModel::Generarators::Base.next_migration_number
method?

It should be Rails::Generators::Migration.next_migration_number, not
ActiveModel::Generarators::Base.next_migration_number :slight_smile:

If you have a conventional migration, you can invoke the migration
generator explicitly:

class ActsAsTaggableOnMigrationGenerator < Rails::Generators::Base
invoke “migration”, %(add_fields_to_tags name:string label:string)
end

However, if you need a customized template, currently you have to copy
the logic that gets the migration number. Maybe we could expose that
as a class method, so you just need to do:

ActiveRecord::Generators::Base.next_migration_number

Feel free to work in a patch and assign to me when it’s done. If not
in mood for a patch, please create a ticket on Lighthouse and assign
to me, so I can tackle it later.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs