class SpreadsheetImport::ActiveRecordImporter::BaseImporter

Constants

CALLBACKS_TO_SKIP

Attributes

scoped_unique[R]
skip_callbacks[R]
skip_validations[R]
unique_by_attributes[R]
update_existing_record[R]

Public Class Methods

new(model, data_processor, options ={}) click to toggle source
Calls superclass method SpreadsheetImport::BaseImporter::new
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 9
def initialize(model, data_processor, options ={})
  super
  @unique_by_attributes = options[:unique_by_attributes]
  @update_existing_record = if options[:update_existing_record].nil?
    !unique_by_attributes.nil?
  else
    options[:update_existing_record]
  end
  @scoped_unique = options[:scoped_unique]
  @skip_validations = options[:skip_validations]
  @skip_callbacks = options[:skip_callbacks]
end

Protected Class Methods

name() click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 72
def self.name
  "#{superclass.name}NoValidation"
end

Public Instance Methods

create_or_update_record(data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 26
def create_or_update_record(data)
  if unique_by_attributes
    if update_existing_record
      duplicate_records = find_duplicate_for_unique_by_attributes(data)
      duplicate_records.present? ? update_record(duplicate_records, data) : create_record(data)
    end
  else
    create_record(data)
  end
end
create_record(data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 37
def create_record(data)
  record = anoymous_model.new(data)
  unless record.save
    handle_validation_failure(record, data)
  end
  record
end
find_duplicate_for_unique_by_attributes(data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 22
def find_duplicate_for_unique_by_attributes(data)
  scoped_model.where(data.slice(*unique_by_attributes))
end
update_only_if_data_changed(records, data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 49
def update_only_if_data_changed(records, data)
  records = [records] if records.is_a?(ActiveRecord::Base)
  records.each do |record|
    if data.any? { |name, value| record.read_attribute(name) != value }
      record.update_attributes(data)
    end
  end
end
update_record(records, data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 45
def update_record(records, data)
  update_only_if_data_changed(records, data)
end

Protected Instance Methods

anoymous_model() click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 66
def anoymous_model
  @anoymous_model ||= if skip_validations && skip_callbacks
    table_name = model.table_name
    Class.new(ActiveRecord::Base) { self.table_name = table_name }
  elsif skip_validations
    Class.new(model) do
      def self.name
        "#{superclass.name}NoValidation"
      end
      reset_callbacks :validate
      reset_callbacks :validation
    end
  elsif skip_callbacks
    Class.new(model) do
      def self.name
        "#{superclass.name}NoCallback"
      end
      CALLBACKS_TO_SKIP.each { |name| reset_callbacks(name) }
    end
  else
    model
  end
end
handle_validation_failure(record, data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 60
def handle_validation_failure(record, data); end
scoped_model() click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/base_importer.rb, line 62
def scoped_model
  scoped_unique ? anoymous_model.send(scoped_unique) : anoymous_model
end