Class: Transfer::Generators::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/transfer/generators/active_record.rb

Instance Attribute Summary

Attributes inherited from Base

#klass

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after, #before, #initialize

Constructor Details

This class inherits a constructor from Transfer::Generators::Base

Class Method Details

.supports?(klass) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/transfer/generators/active_record.rb', line 3

def self.supports? klass
  defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
end

Instance Method Details

#column_present?(name) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/transfer/generators/active_record.rb', line 7

def column_present? name
  super(name) || klass.column_names.include?(name.to_s)
end

#create(attributes, row, options = {}, callbacks = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/transfer/generators/active_record.rb', line 15

def create attributes, row, options={}, callbacks={}
  model = klass.new attributes, :without_protection => true

  model.instance_exec(row, &callbacks[:before_save]) if callbacks[:before_save]
  save_options = options.select{|key| key == :validate }

  model.save! save_options
  options[:success].call(model, row) if options[:success]
  model.instance_exec(row, &callbacks[:after_save]) if callbacks[:after_save]
  model
rescue Exception => e
  options[:failure].call(model, row, e) if options[:failure]
  raise ActiveRecord::Rollback if options[:failure_strategy] == :rollback
  model
end

#transaction(&block) ⇒ Object



11
12
13
# File 'lib/transfer/generators/active_record.rb', line 11

def transaction &block
  klass.transaction &block
end