Projects

ModelFactory Rails Plugin

By matthias

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.

ModelFactory reads the required configuration information from a MODEL_FACTORY_MODELS constant that is a hash you have to specify before the require statements in your helper file:

1
2
3
4
5
6
7
8
9
10
 
  MODEL_FACTORY_MODELS = {
    :user => {
      :attribute1 => 'value1',
      :attribute2 => 'value2',
    },
    :another_model => {
      :attribute => 'value'
    }
  }
Now you can use the ModelFactory methods:
1
2
3
4
5
6
7
8
9
10
11
 
  
  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

released under the MIT license

 
Projects