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
]

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



37
38
39
# File 'app/services/alchemy/copy_page.rb', line 37

def initialize(page:)
  @page = page
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



33
34
35
# File 'app/services/alchemy/copy_page.rb', line 33

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:



46
47
48
49
50
51
52
53
54
55
# File 'app/services/alchemy/copy_page.rb', line 46

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