Class: CloneKit::Rules::Remap

Inherits:
CloneKit::Rule show all
Defined in:
lib/clone_kit/rules/remap.rb

Direct Known Subclasses

SafeRemap

Instance Attribute Summary

Attributes inherited from CloneKit::Rule

#id_generator

Instance Method Summary collapse

Constructor Details

#initialize(model_name, remap_hash = {}, id_generator: nil) ⇒ Remap

Returns a new instance of Remap.



6
7
8
9
10
# File 'lib/clone_kit/rules/remap.rb', line 6

def initialize(model_name, remap_hash = {}, id_generator: nil)
  super(id_generator: id_generator)
  self.remap_hash = remap_hash
  self.model_name = model_name
end

Instance Method Details

#fix(_old_id, attributes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/clone_kit/rules/remap.rb', line 12

def fix(_old_id, attributes)
  remap_hash.each do |klass, remap_attributes|
    Array.wrap(remap_attributes).each do |att|
      next unless try?(attributes, att)

      attributes[att] = if attributes[att].is_a?(Array)
                          attributes[att].map { |id| remap(klass, id) if id.present? }.compact
                        else
                          remap(klass, attributes[att])
                        end
    end
  end
end