Module: FlatMap::ModelMapper::Skipping

Included in:
FlatMap::ModelMapper
Defined in:
lib/flat_map/model_mapper/skipping.rb

Overview

This helper module slightly enhances ofunctionality of the OpenMapper::Skipping module for most commonly used ActiveRecord targets.

Instance Method Summary collapse

Instance Method Details

#skip!Object

Extend original #skip! method for Rails-models-based targets

Note that this will mark the target record as destroyed if it is a new record. Thus, this record will not be a subject of Rails associated validation procedures, and will not be saved as an associated record.

Returns:

  • (Object)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flat_map/model_mapper/skipping.rb', line 15

def skip!
  super
  if target.is_a?(ActiveRecord::Base)
    if target.new_record?
      # Using the instance variable directly as {ActiveRecord::Base#delete}
      # will freeze the record.
      target.instance_variable_set('@destroyed', true)
    else
      # Using reload instead of reset_changes! to reset associated nested
      # model changes also
      target.reload
    end
  end
end

#use!Object

Extend original #use! method for Rails-models-based targets, as acoompanied to #skip! method.

Returns:

  • (Object)


34
35
36
37
38
39
40
41
42
43
# File 'lib/flat_map/model_mapper/skipping.rb', line 34

def use!
  super
  if target.is_a?(ActiveRecord::Base)
    if target.new_record?
      target.instance_variable_set('@destroyed', false)
    else
      all_nested_mountings.each(&:use!)
    end
  end
end