Skip to content

Instantly share code, notes, and snippets.

@aantix
Created January 16, 2011 12:14
Show Gist options
  • Save aantix/781737 to your computer and use it in GitHub Desktop.
Save aantix/781737 to your computer and use it in GitHub Desktop.
Takes a seeded model and dumps out the data in a format that can be used for testing factories
# 523 : ~/Projects/runfatboy2 $ rails runner ~/factories_from_db.rb Exercise 1 2 3
#
# Factory.define :exercise_incline press do |r|
# r.name 'Incline Press'
# r.url 'http://video.google.com/googleplayer.swf?docId=-5762262560903802111&hl=en'
# r.verb 'Lift'
# r.measurement_type_id 3
# r.exercise_type_id 1
# r.user_id 2
# r.start_weight 0.8
# r.female_factor 0.41
# r.incremental_factor 0.999999999999
# r.default_dow 1
# r.order_num 2
# r.default_workout true
# r.exercise_listing true
# end
model = Object::const_get(ARGV[0])
recs = ARGV.inject([]) do |records, arg|
unless arg == ARGV[0] # Skip first arg
rec = model.find(arg) rescue nil
records.push(rec) unless rec.nil?
end
records
end
recs.each do |rec|
n = rec.respond_to?(:name) ? "_#{rec.name.underscore}" : ''
sql = "Factory.define :#{model.to_s.underscore}#{n} do |r|\n"
model.column_names.each do |c|
col = c.to_sym
next if col == :id
val = rec.send(c.to_sym)
val = "'#{val}'" if val.class.to_s == 'String'
sql << "\tr.#{c} #{val}\n"
end
sql << "end\n"
puts sql
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment