Tuesday, November 6, 2007

Merb Generators Now Read Your Mind

Three days ago I blogged about the new generators in merb. Meh, that's so 3 days ago ;)

Update: If you've installed these gems from trunk since 0.3.7 and before the official release of 0.4.0 you should first uninstall the merb, merb_active_record, merb_data_mapper and merb_sequel gems before you install them again.

Update #2:use_orm :active_record and :data_mapper have been changed to :activerecord and :datamapper respectively.

ivey and wycats, regulars on #merb and all round good guys today rolled up their sleeves and removed the ugly contstant Merb::GENERATOR_SCOPE that you had to modify to get the generators working. They also came up with a way to implement default model and resource_controller generators in merb.

How to use it now?

Now when you setup your brand new merb app, you go into config/dependencies.rb as usual but instead of
dependency "merb_data_mapper"
you use
use_orm :data_mapper
# and
use_test :rspec
This will setup the dependency and also sort out what generators your app will use. You've got a good selection of generators now.

  • migration - a migration file

  • model - a model in the correct format + migration + tests / specs

  • controller - a blank controller + tests / specs

  • resource_controller - a controller setup with basic CRUD actions + tests / specs

  • resource - model and resource_controller and all things associated with them

So the one you'd normall use is resource.
ruby script/generate resource my_resource attr:type attr:type
This will generate a model and resource controller complete with tests / specs and migrations in the syntax of your selected ORM.

You can also use all of these generators even if you don't use an ORM at all. In that case it will just give you plain vanilla ruby classes with attr_accessors

Neat. These generators are very new though and I'm sure the content of the generated files can be improved. If you find something that you think needs to be changed please raise a ticket preferably with a patch.

wycats blog has a great outline of the major new features of the imminent version 0.4.0 of merb. If your interested you should check it out.