Cuttings allows you to create seed data with a simple DSL that aims to shorten your seeding code and keeping it readable.
Attachment seeding is made to work with Paperclip's way to assing files.
Add to your Gemfile and bundle install :
gem 'cuttings'Then create the directory structure with the install generator :
rails generate cuttings:installYour can use Cuttings in your db/seeds.rb file, or inside rake tasks,
depending on which method you prefer.
If you choose the db/seeds.rb file way, just use the API as explained in the
DSL section.
If you choose the rake task way, a generator is provided to help you organize
your seed tasks.
But a generator is provided to help you create separated seed data accessible
with rake tasks.
rails generate cuttings usersWhich will generate the following file in lib/seeds/tasks/users.rake
namespace :seed do
task users: :environment do
Cuttings.plant do
# Add your seeding code here
end
end
endnamespace :seed do
task users: :environment do
Cuttings.plant do
truncate %w(Visit)
seed 'User' do
create(
name: 'Jean Jean',
email: 'jean@jean.com',
avatar: attachment('user.png')
)
end
end
end
endThe DSL is quite simple, and gives you just the tools you need to get a clean and readable seed file.
To empty model tables, you can use the #truncate method followed by a list
of model names.
This is used to empty tables from models you won't seed, and is equivalent to
calling .destroy_all on all provided models.
Cuttings.plant do
truncate %w(Visit)
endTo seed a model, you'll use the #seed method.
The #seed method automatically truncates the table of the model.
If you don't want it to do so, you can pass truncate: false as the second
argument.
seed 'User', truncate: false do
...
endTo create your model's instances, you'll have to call #create inside the
#seed method block, and give it a hash of attributes to assign.
seed 'Product' do
create(name: 'Rope', price: 12.90)
endTo seed attachments, start by adding your attachment to your
lib/seeds/attachments folder.
You'll then be able to use the #attachment helper method from inside
the #seed method block to pass it to your model's creation.
seed 'User' do
create(
name: 'Jean Jean',
email: 'jean@jean.com',
avatar: attachment('user.png')
)
endThis project rocks and uses MIT-LICENSE.