Module: ActiveRecordImporter::Importable::ClassMethods

Defined in:
lib/active_record_importer/importable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_importable(options = {}) ⇒ Object

#acts_as_importable

Make a model importable This will allow a model to use the importer

class User < ActiveRecord::Base
  acts_as_importable
end

You may also add options:

class User < ActiveRecord::Base
  acts_as_importable default_attributes: {first_name: 'Michael', surname: 'Nera'}
end


23
24
25
26
27
28
# File 'lib/active_record_importer/importable.rb', line 23

def acts_as_importable(options = {})
  @@importer_options = OptionsBuilder.build(options.merge(allowed_columns_hash(options)))

  include ActiveRecordImporter::Importable::InstanceMethods
  extend ActiveRecordImporter::Importable::SingletonMethods
end

#import!(options = {}) ⇒ Object

#import!

This method is called in the Import instance during execution of import You may also call this method without any import instance e.g.

User.import!(file: File.open(PATH_TO_FILE))

“insert” will be the default insert method for this If you want to use “upsert” or “error_duplicate”, define it in your importer options:

 class User < ActiveRecord::Base
   acts_as_importable insert_method: 'upsert',
                      find_options: ['email']
 end

Or you may use:
User.acts_as_importable insert_method: 'error_duplicate', find_options: ['email']


60
61
62
63
64
65
66
67
68
# File 'lib/active_record_importer/importable.rb', line 60

def import!(options = {})
  fail "#{self.name} is not importable" unless importable?

  import_object = options.fetch(:object, nil)
  execute = options.fetch(:execute, true)
  import_file = get_import_file(import_object, options)

  call_dispatcher(import_object, execute, import_file)
end

#importable?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_record_importer/importable.rb', line 34

def importable?
  false
end

#importer_optionsObject



30
31
32
# File 'lib/active_record_importer/importable.rb', line 30

def importer_options
  @@importer_options
end