The Model Plus project is an attempt to improve on the default Rails model generator.

Its starting point is a patch that I wrote to extend the built-in model generator to create association declarations. That patch has been tabled in favor of writing a proof-of-concept plugin instead.

Here’s the current usage instructions:

Stubs out a new model. Pass the model name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

Attribute pairs are column_name:sql_type arguments specifying the model’s attributes. Timestamps are added by default, so you don’t have to specify them by hand as ‘created_at:datetime updated_at:datetime’.

You don’t have to think up every attribute up front, but it helps to sketch out a few so you can start working with the model immediately.

If you include an attribute with a type of :references, :belongs_to,
has_many, :has_one, or :has_and_belongs_to_many, the model will contain the appropriate association declaration.

You can also decorate a type with suffixes that specify further code generation in the model:


    title:string+a     => Adds to an attr_accessible list
    title:string+p     => validates_presence_of :title
    posted:boolean+ap  => attr_accessible and 
       validates_inclusion_of :posted :in => [true, false]
    rating:float+n     => validates_numericality_of :rating
    hits:integer+i     => validates_numericality_of :hits, 
                            :only_integer => true
    name:string+u      => validates_uniqueness_of :name

This generates a model class in app/models, a unit test in test/unit, a test fixture in test/fixtures/singular/name.yml, and a migration in db/migrate.

Fork me on GitHub