Class: Alchemy::CopyPage

Inherits:
Object
  • Object
show all
Defined in:
app/services/alchemy/copy_page.rb

Overview

Creates a copy of given source page.

Also copies all elements included in source page.

Note:

It prevents the element auto generator from running.

Constant Summary collapse

DEFAULT_ATTRIBUTES_FOR_COPY =
{
  autogenerate_elements: false,
  public_on: nil,
  public_until: nil,
  locked_at: nil,
  locked_by: nil
}
SKIPPED_ATTRIBUTES_ON_COPY =
%w[
  id
  updated_at
  created_at
  creator_id
  updater_id
  lft
  rgt
  depth
  urlname
  cached_tag_list
  title
  meta_description
  meta_keywords
]
METADATA_ATTRIBUTES_TO_COPY =

Metadata to copy via nested attributes (title is derived from page.name)

(Alchemy::PageVersion:: - %w[title]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page:) ⇒ CopyPage

Returns a new instance of CopyPage.

Parameters:

  • page (Alchemy::Page)

    The source page the copy is taken from



43
44
45
# File 'app/services/alchemy/copy_page.rb', line 43

def initialize(page:)
  @page = page
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



39
40
41
# File 'app/services/alchemy/copy_page.rb', line 39

def page
  @page
end

Instance Method Details

#call(changed_attributes:) ⇒ Alchemy::Page

Parameters:

  • changed_attributes (Hash)

    A optional hash with attributes that take precedence over the source attributes

Returns:



52
53
54
55
56
57
58
59
60
61
# File 'app/services/alchemy/copy_page.rb', line 52

def call(changed_attributes:)
  Alchemy::Page.transaction do
    new_page = Alchemy::Page.new(attributes_from_source_for_copy(changed_attributes))
    new_page.tag_list = page.tag_list
    if new_page.save!
      Alchemy::Page.copy_elements(page, new_page)
    end
    new_page
  end
end