Have you ever wanted to generate a model without the need to configure or write extra stuff like Fixtures or Factories?
You've found the right gem!
This gem is inspired by Python's model-bakery and factory_bot_rails. Thanks for them for the development effort and the inspiration!
This gem let you fill a model object with fake data (just like model-bakery), according to the attribute's data type. By default, it handles the types defined by ActiveRecord.
Add this line to your application's Gemfile:
gem 'factory_bakery', git: 'https://github.com/mohammed-io/factory_bakery.git'
And then execute:
$ bundle install
Or install it yourself as (Coming soon):
$ gem install factory_bakery
This gem works only for the Active Record models that are backed by database. Because it depends on the MODEL_CLASS.attribute_types
method which is available only on the models.
Once you install it, you will get 2 global functions bake
and bake
.
The first one generates the model without touching the database. While the second one updates the record in the database.
The time you want to make a fake model object, you can use:
bake(MODEL_CLASS, {specific_attribute: 'specific_value' })
# OR
bake!(MODEL_CLASS, {specific_attribute: 'specific_value' }) # Saves on the db
The MODEL_CLASS
is the model that you want to fill with fake values. For instance, if you want to fill the User
with fake data, but with a valid specific email, it can be as following:
bake(User, email: '[email protected]')
# OR
bake!(User, email: '[email protected]')
Then you should get a result like this:
#<User id: nil, email: "[email protected]", first_name: "pw7meuwq16zinefai0qhq8btqg8zqe", last_name: "qthti7af61n5rlvb8njg3ygncmsofz", phone: "vs8e4lpy63sdv6u", address: "agseb6fjt51who6yel0o6h0t7b1ndr3abn7u0yyc">
The data is just gibberish at the moment. This how the basic generator works. Which can be extended or improved later.
Because the password will be hashed, then it will take time to do the calculation, thus will make the generation slower.
To skip it, simply add password: nil
as the following:
bake(User, email: '[email protected]', password: nil)
# OR
bake!(User, email: '[email protected]', password: nil)
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
- Write some test.
- Smart handle of the relations.
- Use symbols along with Model classes.
- How to write custom generators?
- Support non-active record models?
Bug reports and pull requests are welcome on GitHub at https://github.com/mohammed-io/factory_bakery. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the FactoryBakery project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.