Plugins - ModelFactory
Add to favoritesModelFactory
by Matthias Hennemeyer <mhennemeyer@gmail.com>
Introduction
Create objects with ease:
create_user, create_model :attribute => 'value', ...
ModelFactory is available as a Rails plugin. It provides a way to construct model objects from a configuration hash.
The special thing about ModelFactory is that it will also create belongs_to associates.
ModelFactory will first try to find the specified associated model object by id. If the attempt to find it raises an not found error it will try to construct the specified associated model object from the configuration hash.
Usage
ModelFactory reads the required configuration information from a
MODEL_FACTORY_MODELS constant that is a hash you have to specify before the require statement in your
helper file:
MODEL_FACTORY_MODELS = {
:user => {
:attribute1 => 'value1',
:attribute2 => 'value2',
},
:another_model => {
:attribute => 'value'
}
}
require 'model_factory'
Now you can use the ModelFactory methods:
create_user # => #User attribute1=value1, attribute2=value2
create_model # => #Model attribute=value
create_model :user_id => 1 # => #Model attribute=value, user_id=1
This will create a user object from the
MODEL_FACTORY_MODELS hash if there is no
user with id=1 in the database.
INSTALL:
$ ruby script/plugin install git://github.com/mhennemeyer/model_factory.git
Copyright © 2008 Matthias Hennemeyer, released under the MIT license
http://workunitgroup.com/2008/5/30/modelfactory-rails-plugin
git://github.com/mhennemeyer/model_factory
Rails' (MIT)
Testing
