Module: RailsAdminImport::Import

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Base
Defined in:
lib/rails_admin_import/import.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#after_import_save(*args) ⇒ Object



163
164
165
# File 'lib/rails_admin_import/import.rb', line 163

def after_import_save(*args)
  # Meant to be overridden to do special actions
end

#before_import_save(*args) ⇒ Object



159
160
161
# File 'lib/rails_admin_import/import.rb', line 159

def before_import_save(*args)
  # Meant to be overridden to do special actions
end

#import_belongs_to_data(associated_map, row, map) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/rails_admin_import/import.rb', line 189

def import_belongs_to_data(associated_map, row, map)
  self.class.belongs_to_fields.each do |key|
    if map.has_key?(key) && row[map[key]] != ""
      self.send("#{key}_id=", associated_map[key][row[map[key]]])
    end
  end
end

#import_displayObject



167
168
169
# File 'lib/rails_admin_import/import.rb', line 167

def import_display
  self.id
end

#import_files(row, map) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rails_admin_import/import.rb', line 171

def import_files(row, map)
  if self.new_record? && self.valid?
    self.class.file_fields.each do |key|
      if map[key] && !row[map[key]].nil?
        begin
          # Strip file
          row[map[key]] = row[map[key]].gsub(/\s+/, "")
          format = row[map[key]].match(/[a-z0-9]+$/)
          open("#{Rails.root}/tmp/#{self.permalink}.#{format}", 'wb') { |file| file << open(row[map[key]]).read }
          self.send("#{key}=", File.open("#{Rails.root}/tmp/#{self.permalink}.#{format}"))
        rescue Exception => e
          self.errors.add(:base, "Import error: #{e.inspect}")
        end
      end
    end
  end
end

#import_many_data(associated_map, row, map) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rails_admin_import/import.rb', line 197

def import_many_data(associated_map, row, map)
  self.class.many_fields.each do |key|
    values = []

    map[key] ||= []
    map[key].each do |pos|
      if row[pos] != "" && associated_map[key][row[pos]]
        values << associated_map[key][row[pos]]
      end
    end

    if values.any?
      self.send("#{key.to_s.pluralize}=", values)
    end
  end
end