Module: ImportExport::ModelMethods::ClassMethods
- Defined in:
- lib/import_export.rb
Instance Method Summary collapse
-
#acts_as_importable(options = {}) ⇒ Object
any method placed here will apply to classes.
- #export ⇒ Object
- #import(filename, context) ⇒ Object
- #index_of(fieldname) ⇒ Object
- #read_csv(filename) ⇒ Object
Instance Method Details
#acts_as_importable(options = {}) ⇒ Object
any method placed here will apply to classes
12 13 14 15 16 17 |
# File 'lib/import_export.rb', line 12 def acts_as_importable( = {}) cattr_accessor :import_fields, :export_fields self.import_fields = [:import_fields] self.export_fields = [:export_fields] send :include, InstanceMethods end |
#export ⇒ Object
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 |
# File 'lib/import_export.rb', line 61 def export export_fields = self.import_fields || self.export_fields ImportExport::CSV.generate do |csv| csv << export_fields.map{|f| f.split('.')[0]} self.find_each(:batch_size => 2000) do |element| collection = [] export_fields.each do |field_name| begin if field_name.include?('.') method_names = field_name.gsub(/!/,'').split('.').compact sub_element = element method_names.each do |method_name| if sub_element || sub_element.respond_to?(method_name) sub_element = sub_element.send(method_name) else break end end collection << sub_element else collection << element.send(field_name) end rescue Exception => e Rails.logger.info ">>>>>>>>> Exception Caught ImportExport >>>>>>>>>>>" Rails.logger.error e. Rails.logger.error e.backtrace collection << nil end end csv << collection end end end |
#import(filename, context) ⇒ Object
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 |
# File 'lib/import_export.rb', line 19 def import(filename, context) collection = [] headers, *data = self.read_csv(filename) scope_object = context[:scoped] ActiveRecord::Base.transaction do data.each_with_index do |data_row, index| data_row.map{|d| d.strip! if d} begin class_or_association = scope_object ? scope_object.send(self.table_name) : self if key_field = context[:find_existing_by] key_value = data_row[index_of(key_field)] element = class_or_association.send("find_by_#{key_field}", key_value) || class_or_association.new else element = class_or_association.new end Rails.logger.info "#{element.new_record? ? "Creating new" : "Updating existing"} record from #{data_row.inspect}" self.import_fields.each_with_index do |field_name, field_index| if field_name.include?('.') assign_association(element, field_name, field_index, context, data_row) else element.send "#{field_name}=", data_row[field_index] end end element.save! collection << element rescue Exception => e e1 = e.exception("Invalid data found at line #{index + 2} : " + e.) e1.set_backtrace(e.backtrace) Rails.logger.error e1. Rails.logger.error e1.backtrace.join("\n") raise e1 end end end return collection end |
#index_of(fieldname) ⇒ Object
110 111 112 113 |
# File 'lib/import_export.rb', line 110 def index_of(fieldname) @import_field_indices ||= {} @import_field_indices[fieldname] ||= self.import_fields.index{ |f| f.to_s == fieldname.to_s } end |
#read_csv(filename) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/import_export.rb', line 97 def read_csv(filename) if File.exist?(filename) collection = ImportExport::CSV.parse(File.open(filename, 'rb')) collection = collection.map{|w| w} unless collection.nil? collection = [] if collection.nil? return collection else raise ArgumentError, "File does not exist." end end |