LazyFixtures
Generator to create factory girl fixtures, just with a simple ActiveRecord object from your current database.
Installation
Add this line to your application's Gemfile:
gem 'lazy_fixtures'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lazy_fixtures
Usage
Inside your console simple type LazyFixtures.generate()
inside the parenthesis place an ActiveRecord object, and this command will generate the corresponding file inside your project.
Initialisation
If you are using this gem inside a rails project you can use the initialise folder you can add a file to configure some attributes that will help the gem work properly.
require 'factory_girl'
FactoryGirl.factories.clear
FactoryGirl.find_definitions
LazyFixtures.configure do |config|
config.factory_directory = "#{specify which folder are your fixtures save}"
config.factory_names = FactoryGirl.factories.map(&:name)
end
This lines will tell the gem where to store the files and it will give them a list with your actual Factory Girl fixtures, with this it will avoid factory girl exceptions.
Options
There are several options you can pass to the generator, the method will receive these options as a hash so the order is not redundant.
- nested => by default is set to false, set this to true and will traverse all the associations from your object and will create does fixtures for you as well.
- overwrite => by default is set to false, set this to true and will overwrite does file with same name inside the fixtures folder that you specify. If for some reason it find a file with the same name and the option is set to false it will ask you if you want to overwrite. That choice is up to you :-)
- create => by default is set to true, this options tell the generator to create the file, if is set to false it will not generate any file.
- skip_attr => this accept an array of string which will remove those attributes from the object.
- change_attr => this accepts a hash with new values for the fixture you want to set.
Examples
This will create a switch fixture:
LazyFixtures.generate(Switch.last)
This will create a switch fixture and traverse all the associations:
LazyFixtures.generate(Switch.last, nested: true)
This will create a switch fixture with skipped attributes:
LazyFixtures.generate(Switch.last, skip_attr:['age', 'name'])
This will create a switch fixture with custom values:
LazyFixtures.generate(Switch.last, change_attr:{'age' => 34, 'name' => 'John'})
Notes
This version is the first version, I want to add more functionality to it, skipping and changing nested attributes from the object, and many more. If you use the gem I see any errors or some functionality that might be useful please let me know. Thanks.
Contributing
- Fork it ( https://github.com/GustavoCaso/lazy_fixtures/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request