52 lines
982 B
Ruby
52 lines
982 B
Ruby
module ActsAsCsvImport::Hook
|
|
def acts_as_csv_import(*args)
|
|
@csv_options = args.extract_options!
|
|
|
|
|
|
|
|
def self.import_csv_fields
|
|
@csv_options[:fields]
|
|
end
|
|
|
|
def self.import_csv(list, import_csv)
|
|
# puts self.instance_methods
|
|
|
|
if self.methods.include?(:custom_csv_import)
|
|
custom_csv_import(list, import_csv)
|
|
else
|
|
default_import_csv(list, import_csv)
|
|
end
|
|
|
|
end
|
|
|
|
def self.default_import_csv(list, import_csv)
|
|
list.each do |row|
|
|
n = self.new
|
|
|
|
row.each do |key, value|
|
|
|
|
if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal
|
|
eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')"
|
|
else
|
|
eval "n.#{key} = value"
|
|
end
|
|
|
|
|
|
end
|
|
|
|
n.save
|
|
|
|
import_csv.import_csv_elements << ImportCsvElement.new(:element => n)
|
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|