Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ Disable output

To disable output from Seed Fu, set `SeedFu.quiet = true`.

Enable validate
--------------

To enable model validations from Seed Fu, set `SeedFu.validate = true`.

Handling large seed files
-------------------------

Expand Down
5 changes: 5 additions & 0 deletions lib/seed-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module SeedFu
# `Rails.root/db/fixtures/Rails.env`
@@fixture_paths = ['db/fixtures']

mattr_accessor :validate

# Set `SeedFu.validation = true` to validate seeds
@@validate = false

# Load seed data from files
# @param [Array] fixture_paths The paths to look for seed files in
# @param [Regexp] filter If given, only filenames matching this expression will be loaded
Expand Down
4 changes: 3 additions & 1 deletion lib/seed-fu/seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Seeder
# particular record.
# @param [Hash] options
# @option options [Boolean] :quiet (SeedFu.quiet) If true, output will be silenced
# @option options [Boolean] :validate (SeedFu.validate) If true, creations will be validated
# @option options [Boolean] :insert_only (false) If true then existing records which match the
# constraints will not be updated, even if the seed data has changed
def initialize(model_class, constraints, data, options = {})
Expand All @@ -24,6 +25,7 @@ def initialize(model_class, constraints, data, options = {})
@options = options.symbolize_keys

@options[:quiet] ||= SeedFu.quiet
@options[:validate] ||= SeedFu.validate

validate_constraints!
validate_data!
Expand Down Expand Up @@ -71,7 +73,7 @@ def seed_record(data)
else
record.assign_attributes(data)
end
record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
record.save!(:validate => @options[:validate]) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
record
end

Expand Down