This gem allows you to 'freeze' your ActiveRecord models to create repeatable datasets for development.
Add this line to your application's Gemfile:
gem 'deep_freezer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install freezer
Be sure to load your freezer classes in development.rb
Dir.glob(Rails.root.join("lib", "freezers", "*.rb")).each do |file|
require file
end
Create an initializer and set the path for fixtures to be saved
Freezer::Base.fixture_path = Rails.root.join("db", "seeds")
Define a DeepFreezer
for your model
class PostFreezer < DeepFreezer::Base
freeze :id,
:title,
:body,
:created_at,
:updated_at
end
Attributes can be overrode at time of freeze, similar to ActiveModel Serializers, by defining a method with the same name as the attribute name.
class PostFreezer < DeepFreezer::Base
freeze :id,
:title,
:body,
:created_at,
:updated_at
def title
"Frozen Title"
end
end
And then write a script to select and freeze the records you want:
posts = Post.all.limit(10)
posts.map { |p | PostFreezer.new(p).freeze }
This will result in a posts.yml
file in db/seeds
which can be loaded by adding DeepFreezer::Defrost.load!
to your seeds.rb
file.
Fixtures can be deleted manually in the directory, or by running Freezers::Base.reset!
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 tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/itison/freezer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Freezer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.