public
Created — forked from wtnabe/README.md

import seed data from .csv or .yml for Rails 2.3.4+

  • Download Gist
README.md
Markdown

Rails SeedImporter

  1. Put this code at #{Rails.root}/db/seeds.rb
  2. type rake db:setup

Limitation

It supports only `id' primary key.

seeds.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
# -*- mode: ruby; coding: utf-8 -*-
class SeedImporter
def initialize
@model = nil
@with_id = false
@dragonfly_fields = []
@paperclip_fields = []
end
 
def run
Dir.glob( File.join( seed_dir, '**/*.{yml,csv}' ) ).sort.each { |seed|
send( "import_#{File.extname( seed )[1..-1]}_seed", seed )
}
end
 
def seed_dir
ENV['SEED_DIR'] || File.dirname(__FILE__) + '/seeds'
end
 
def fcsv_opts
{ :headers => true,
:header_converters => :downcase }
end
 
def import_csv_seed( file )
init_model( file, '.csv' )
FasterCSV.table( file, fcsv_opts ) { |csv|
@with_id = csv.headers.include?( 'id' )
}
if ( @with_id )
_import_csv( file ) { |row| create_and_save_with_id( row.to_hash ) }
else
_import_csv( file ) { |row| create_and_save( row.to_hash ) }
end
end
 
def _import_csv( file, &block )
FasterCSV.open( file, fcsv_opts ).each { |csv| block.call( csv ) }
end
 
def import_yml_seed( file )
init_model( file, '.yml' )
YAML.load_file( file ).each_pair { |k, v|
if ( v.has_key?( 'id' ) )
create_and_save_with_id( v )
else
create_and_save( v )
end
}
end
 
def to_model( file, suffix )
File.basename( file, suffix ).sub( /\A[0-9]+(_\.)?/, '' ).classify
end
 
def init_model( file, suffix )
@model = Object.const_get( to_model( file, suffix ) )
init_file_fields
end
 
def create_and_save( data )
save( create( data ) )
end
 
def create_and_save_with_id( data )
record = create( data )
record['id'] = data['id']
save( record )
end
 
def create( data )
attach_file_when_need( data )
end
 
def attach_file_when_need( data )
record = @model.new( data )
 
if ( @dragonfly_fields.size > 0 )
@dragonfly_fields.each { |e|
col = e.to_s
file = data[e] || data[e.to_s]
path = File.join( seed_dir, file )
record.send( "#{col}=",
open( path ).read ) if file and File.exist?( path )
}
end
if ( @paperclip_fields.size > 0 )
@paperclip_fields.each { |e|
col = e.to_s
file = data[e] || data[e.to_s]
path = File.join( seed_dir, file )
record.send( "#{col}=",
open( path ) ) if file and File.exist?( path )
}
end
 
record
end
 
def save( record )
record.save!
end
 
def init_file_fields
if ( defined? ::Dragonfly )
@dragonfly_fields = @model.dragonfly_apps_for_attributes.keys if @model.dragonfly_apps_for_attributes
end
if ( defined? ::Paperclip )
@paperclip_fields = @model.attachment_definitions.keys if @model.attachment_definitions
end
end
end
 
SeedImporter.new.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.