Class: CloneKit::Rules::Remap

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

Direct Known Subclasses

SafeRemap

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Remap.



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

def initialize(model_name, remap_hash = {})
  self.remap_hash = remap_hash
  self.model_name = model_name
end

Instance Method Details

#fix(_old_id, attributes) ⇒ Object



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

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) unless id.blank? }.compact
                        else
                          remap(klass, attributes[att])
                        end
    end
  end
end