-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2066 from mamhoff/faster-element-duplication
Faster element duplication
- Loading branch information
Showing
12 changed files
with
169 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class DuplicateElement | ||
SKIPPED_ATTRIBUTES_ON_COPY = [ | ||
"cached_tag_list", | ||
"created_at", | ||
"creator_id", | ||
"position", | ||
"id", | ||
"folded", | ||
"updated_at", | ||
"updater_id", | ||
].freeze | ||
|
||
attr_reader :source_element, :repository | ||
|
||
def initialize(source_element, repository: source_element.page_version.element_repository) | ||
@source_element = source_element | ||
@repository = repository | ||
end | ||
|
||
def call(differences = {}) | ||
attributes = source_element.attributes.with_indifferent_access | ||
.except(*SKIPPED_ATTRIBUTES_ON_COPY) | ||
.merge(differences) | ||
.merge( | ||
autogenerate_contents: false, | ||
autogenerate_nested_elements: false, | ||
tags: source_element.tags, | ||
) | ||
|
||
new_element = Element.create(attributes) | ||
|
||
source_element.contents.map do |content| | ||
Content.copy(content, element: new_element) | ||
end | ||
|
||
nested_elements = repository.children_of(source_element) | ||
Element.acts_as_list_no_update do | ||
nested_elements.each.with_index(1) do |nested_element, position| | ||
self.class.new(nested_element, repository: repository).call( | ||
parent_element: new_element, | ||
page_version: new_element.page_version, | ||
position: position | ||
) | ||
end | ||
end | ||
|
||
new_element | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::DuplicateElement do | ||
let(:element) do | ||
create(:alchemy_element, :with_contents, tag_list: "red, yellow") | ||
end | ||
let(:differences) { {} } | ||
subject { described_class.new(element).call(differences) } | ||
|
||
it "should not create contents from scratch" do | ||
expect(subject.contents.count).to eq(element.contents.count) | ||
end | ||
|
||
context "with differences" do | ||
let(:new_page_version) { create(:alchemy_page_version) } | ||
let(:differences) { { page_version_id: new_page_version.id } } | ||
|
||
it "should create a new record with all attributes of source except given differences" do | ||
expect(subject.page_version_id).to eq(new_page_version.id) | ||
end | ||
end | ||
|
||
it "should make copies of all contents of source" do | ||
expect(subject.contents).not_to be_empty | ||
expect(subject.contents.pluck(:id)).not_to eq(element.contents.pluck(:id)) | ||
end | ||
|
||
it "the copy should include source element tags" do | ||
expect(subject.tag_list).to eq(element.tag_list) | ||
end | ||
|
||
context "with nested elements" do | ||
let(:element) do | ||
create(:alchemy_element, :with_contents, :with_nestable_elements, { | ||
tag_list: "red, yellow", | ||
page: create(:alchemy_page), | ||
}) | ||
end | ||
|
||
before do | ||
element.nested_elements << create(:alchemy_element, name: "slide") | ||
end | ||
|
||
it "should copy nested elements" do | ||
expect(subject.nested_elements).to_not be_empty | ||
end | ||
|
||
context "copy to new page version" do | ||
let(:new_page_version) { create(:alchemy_page_version) } | ||
let(:differences) { { page_version_id: new_page_version.id } } | ||
|
||
it "should set page_version id to new page_version's id" do | ||
subject.nested_elements.each do |nested_element| | ||
expect(nested_element.page_version_id).to eq(new_page_version.id) | ||
end | ||
end | ||
end | ||
|
||
context "copy to new page version" do | ||
let(:public_version) do | ||
element.page.versions.create!(public_on: Time.current) | ||
end | ||
let(:differences) { { page_version_id: public_version.id } } | ||
|
||
it "sets page_version id" do | ||
subject.nested_elements.each do |nested_element| | ||
expect(nested_element.page_version_id).to eq(public_version.id) | ||
end | ||
end | ||
end | ||
end | ||
end |