module ActiveRecordImporter::Importable::ClassMethods

Public Instance Methods

acts_as_importable(options = {}) click to toggle source

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
# 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 = {}) click to toggle source

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']
# 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?() click to toggle source
# File lib/active_record_importer/importable.rb, line 34
def importable?
  false
end
importer_options() click to toggle source
# File lib/active_record_importer/importable.rb, line 30
def importer_options
  @@importer_options
end

Private Instance Methods

all_columns() click to toggle source
# File lib/active_record_importer/importable.rb, line 98
def all_columns
  return [] unless (self.respond_to?(:table_exists?) && self.table_exists?)
  self.columns.map { |column| column.name.to_sym }
end
allowed_columns(options = {}) click to toggle source
# File lib/active_record_importer/importable.rb, line 94
def allowed_columns(options = {})
  all_columns + store_accessors(options) - excluded_columns(options)
end
allowed_columns_hash(options = {}) click to toggle source
# File lib/active_record_importer/importable.rb, line 88
def allowed_columns_hash(options = {})
  {
    importable_columns: allowed_columns(options)
  }
end
call_dispatcher(import_object = nil, execute = true, file = nil) click to toggle source
# File lib/active_record_importer/importable.rb, line 72
def call_dispatcher(import_object = nil, execute = true, file = nil)
  ActiveRecordImporter::Dispatcher.new(
    importable: self,
    import: import_object,
    execute: execute,
    import_file: file
  ).call
end
excluded_columns(options = {}) click to toggle source
# File lib/active_record_importer/importable.rb, line 111
def excluded_columns(options = {})
  columns = [:id]
  columns + options.fetch(:exclude_columns, [])
end
get_import_file(import, options = {}) click to toggle source
# File lib/active_record_importer/importable.rb, line 81
def get_import_file(import, options = {})
  file = options.fetch(:file, nil) || import.try(:import_file)
  fail Errors::MissingImportFile.new unless file
  file
end
importable_columns() click to toggle source
# File lib/active_record_importer/importable.rb, line 107
def importable_columns
  importer_options.importable_columns
end
store_accessors(options = {}) click to toggle source
# File lib/active_record_importer/importable.rb, line 103
def store_accessors(options = {})
  options.fetch(:store_accessors, [])
end