import_profile
1.14 KB
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
#!/usr/bin/env ruby
require 'rubygems'
require "#{File.dirname(__FILE__)}/../config/environment"
def import_profile(config)
puts "Importing profile"
config.keys.each do |record_type|
puts record_type.to_s
puts "#{config[record_type].size} registros"
if record_type == :article_version
klass = Article::Version
else
klass = record_type.to_s.camelize.constantize
end
config[record_type.to_sym].each do |attrs|
settings = attrs.keys.select { |k| attrs[k.to_sym].is_a? Hash }
names = attrs.except(*settings).keys
values = names.map {|k| ActiveRecord::Base.send(:sanitize_sql_array, ['?', attrs[k.to_sym]]) }
sql = 'insert into %s(%s) values (%s)' % [klass.table_name, names.join(','), values.join(',')]
klass.connection.execute(sql)
record = klass.find(attrs[:id])
settings.each do |setting|
record.send("#{setting}=", attrs[setting.to_sym])
record.save
end
end
end
end
if ARGV.blank?
puts "--- Please provide the yaml configuration file ---"
exit 0
else
$config_file = ARGV[0]
config = YAML::load_file($config_file)
import_profile config
end