Class: CloneKit::Rules::Remap

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

Overview

Utilizes the SharedIdMap stored in Redis to remap original ids to their new cloned values.

Given an original blog post being cloned:
  { title: "Title", author_id: 5 }
And a blog post rule:
  Remap.new(BlogPost, "Author" => "author_id")
And an author record that was cloned from => to:
  { id: 5, name: "Pat" } => { id: 6, name: "Pat" }
The cloned blog post will show the remapped author id:
  { title: "Title", author_id: 6 }

When a remapped id is missing, an error is added to the operation.

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.



21
22
23
24
25
# File 'lib/clone_kit/rules/remap.rb', line 21

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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clone_kit/rules/remap.rb', line 27

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