Sha256: e8028b374db5dcfac2450fdd7e7f1d255cf7df09d15e3768d767e2ec3ee1f17b

Contents?: true

Size: 1.36 KB

Versions: 51

Compression:

Stored size: 1.36 KB

Contents

# 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_ingredients: false,
          autogenerate_nested_elements: false,
          tags: source_element.tags
        )

      new_element = Element.new(attributes)
      new_element.ingredients = source_element.ingredients.map(&:dup)
      new_element.save!

      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

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
alchemy_cms-7.3.5 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.8 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.1.13 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.0.16 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.3.4 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.3.3 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.3.2 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.7 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.3.1 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.3.0 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.6 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.5 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.1.12 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.0.15 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.4 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.1.11 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.3 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.1.10 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.2.2 app/services/alchemy/duplicate_element.rb
alchemy_cms-7.1.9 app/services/alchemy/duplicate_element.rb