Class: Transfer::Generators::Mongoid

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

Instance Attribute Summary

Attributes inherited from Base

#klass

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after, #before, #column_present?, #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/mongoid.rb', line 3

def self.supports? klass
  defined?(Mongoid) && klass.ancestors.include?(Mongoid::Document)
end

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/transfer/generators/mongoid.rb', line 13

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 if options[:failure_strategy] == :rollback
  model
end

#transactionObject



7
8
9
10
11
# File 'lib/transfer/generators/mongoid.rb', line 7

def transaction
  yield
rescue
  klass.delete_all
end