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
Also see: http://guides.rails.info/generators.html for more details ;)
Good luck!
Kristian
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 :)
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"
On Jan 30, 12:11 am, Kristian <kmand...@gmail.com> wrote:
> 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 myhttp://github.com/kristianmandrup/mongo_model_r3for an
> example. This can be used byhttp://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.htmlfor more details ;)
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.
> > Also see:http://guides.rails.info/generators.htmlformore details ;)
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?
> > > Also see:http://guides.rails.info/generators.htmlformoredetails ;)