Class: Pageflow::NestedRevisionComponentCopy
- Inherits:
-
Object
- Object
- Pageflow::NestedRevisionComponentCopy
- Defined in:
- app/models/pageflow/nested_revision_component_copy.rb
Overview
Service object for copying nested revision components between records using bulk INSERT … SELECT statements.
When ‘reset_perma_ids` is false we reuse the perma_id values from the source revision. Parents and their copies are joined via perma_id so that foreign keys of the newly inserted records point at the duplicated parents.
If ‘reset_perma_ids` is true we compute an offset for each association. The offset ensures that the minimum perma_id of the records being copied becomes `entry.perma_id_counter + 1`. This yields a range of new perma_ids above the current maximum perma_id of the entry while preserving the relative order of ids within the copied set.
Example for a simple one level association:
INSERT INTO test_nested_revision_components (parent_id, perma_id, text)
SELECT NEW_ROOT_ID, old_items.perma_id + OFFSET, old_items.text
FROM test_nested_revision_components AS old_items
WHERE old_items.parent_id = OLD_ROOT_ID;
Example for a deeply nested association where nested items have parents of the same type:
INSERT INTO test_deeply_nested_revision_components (parent_id, perma_id)
SELECT new_children.id,
old_deep_items.perma_id + OFFSET,
old_deep_items.text
FROM test_deeply_nested_revision_components AS old_deep_items
JOIN test_nested_revision_components AS old_children
ON old_children.id = old_deep_items.parent_id
JOIN test_nested_revision_components AS new_children
ON new_children.parent_id = NEW_ROOT_ID
AND new_children.perma_id = old_children.perma_id + CHILD_OFFSET
WHERE old_children.parent_id = OLD_ROOT_ID;
Defined Under Namespace
Classes: Model
Instance Method Summary collapse
-
#initialize(from:, to:, reset_perma_ids: false, connection: ActiveRecord::Base.connection) ⇒ NestedRevisionComponentCopy
constructor
A new instance of NestedRevisionComponentCopy.
- #perform_for(revision_components:) ⇒ Object
- #perform_for_nested_revision_components ⇒ Object
Constructor Details
#initialize(from:, to:, reset_perma_ids: false, connection: ActiveRecord::Base.connection) ⇒ NestedRevisionComponentCopy
Returns a new instance of NestedRevisionComponentCopy.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/pageflow/nested_revision_component_copy.rb', line 38 def initialize(from:, to:, reset_perma_ids: false, connection: ActiveRecord::Base.connection) @root_class = from.class @old_root_id = from.id @new_root_id = to.id @reset_perma_ids = reset_perma_ids @connection = connection @offsets = Hash.new(0) @entry = to.entry_for_auto_generated_perma_id end |
Instance Method Details
#perform_for(revision_components:) ⇒ Object
50 51 52 |
# File 'app/models/pageflow/nested_revision_component_copy.rb', line 50 def perform_for(revision_components:) perform(Model.for(revision_components:)) end |
#perform_for_nested_revision_components ⇒ Object
54 55 56 |
# File 'app/models/pageflow/nested_revision_component_copy.rb', line 54 def perform_for_nested_revision_components perform(Model.for_nested_revision_components(@root_class)) end |