Module: CsvRails::Import::ClassMethods
- Defined in:
- lib/csv_rails/import.rb
Instance Method Summary collapse
- #csv_import(body, opts = {}) ⇒ Object
- #inverse_human_attriute_name(attribute, options = {}) ⇒ Object
- #tsv_import(body, opts = {}, &block) ⇒ Object
Instance Method Details
#csv_import(body, opts = {}) ⇒ Object
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 |
# File 'lib/csv_rails/import.rb', line 35 def csv_import(body, opts={}) fields = opts.delete(:fields) find_key = opts.has_key?(:find_key) ? opts.delete(:find_key) : :id records = [] all_green = true translated = false self.transaction do CSV.parse(body, opts).each.with_index do |row, i| unless fields fields = row next end unless translated fields.map!{|f| inverse_human_attriute_name(f) } translated = true end attributes = ActiveSupport::HashWithIndifferentAccess.new(Hash[fields.zip(row)]) record = if find_key self.where(:"#{find_key}" => attributes[find_key.to_s]).first_or_initialize else self.new end record.attributes = attributes.select{|k, v| self.attribute_method?(k) } if block_given? val = yield record, attributes, i next if val == false end if all_green && record.valid? record.save! else all_green = false end records << record end raise ActiveRecord::Rollback unless all_green end records end |
#inverse_human_attriute_name(attribute, options = {}) ⇒ Object
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 |
# File 'lib/csv_rails/import.rb', line 5 def inverse_human_attriute_name(attribute, = {}) defaults = [:defaults] || [] parts = attribute.to_s.split(".") attribute = parts.pop namespace = parts.join("/") unless parts.empty? if defaults.blank? if namespace lookup_ancestors.each do |klass| defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}" end defaults << :"#{self.i18n_scope}.attributes.#{namespace}" else lookup_ancestors.each do |klass| defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}" end end defaults << :"attributes" end entry = nil defaults.each do |k| map = I18n.translate(k) if map.is_a?(Hash) entry = map.invert[attribute] break end end entry || attribute.underscore.gsub(/ /, '_') end |
#tsv_import(body, opts = {}, &block) ⇒ Object
74 75 76 |
# File 'lib/csv_rails/import.rb', line 74 def tsv_import(body, opts={}, &block) csv_import(body, opts.merge(col_sep: "\t"), &block) end |