Class: Alchemy::Page::Publisher

Inherits:
Object
  • Object
show all
Defined in:
app/models/alchemy/page/publisher.rb

Overview

Handles publishing of pages

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Publisher

Returns a new instance of Publisher.



9
10
11
# File 'app/models/alchemy/page/publisher.rb', line 9

def initialize(page)
  @page = page
end

Instance Method Details

#publish!(public_on:) ⇒ Object

Copies all currently visible elements to the public version of page

Creates a new published version if none exists yet and updates the ‘published_at` timestamp of the page. `published_at` is used as a cache key.

Sends a publish notification to all registered publish targets



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/alchemy/page/publisher.rb', line 21

def publish!(public_on:)
  Page.transaction do
    PageMutex.with_lock!(@page) do
      version = public_version(public_on)
      DeleteElements.new(version.elements).call

      repository = page.draft_version.element_repository
      ActiveRecord::Base.no_touching do
        Element.acts_as_list_no_update do
          repository.visible.not_nested.each.with_index(1) do |element, position|
            Alchemy::DuplicateElement.new(element, repository: repository).call(
              page_version_id: version.id,
              position: position
            )
          end
        end
      end
      version.update(updated_at: public_on)
      page.update(published_at: public_on)
    end
  end

  Alchemy.publish_targets.each { |p| p.perform_later(page) }
end