Sha256: 3ab66c6bb6f64e1f06b10c0e6f4c7298d38a88590e86c260c64bf8290668bcaa

Contents?: true

Size: 1.52 KB

Versions: 30

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require_dependency "alchemy/page"

module Alchemy
  class Page < BaseRecord
    # Handles publishing of pages
    class Publisher
      def initialize(page)
        @page = page
      end

      # 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
      #
      def publish!(public_on:)
        Page.transaction 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
          page.update(published_at: public_on)
        end

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

      private

      attr_reader :page

      # Load the pages public version or create one
      def public_version(public_on)
        page.public_version || page.versions.create!(public_on: public_on)
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
alchemy_cms-6.1.10 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.5 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.4 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.3 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.9 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.2 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.8 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.7 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.1 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.0 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.0.pre.rc1 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.6 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.0.pre.c app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.0.pre.b app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.5 app/models/alchemy/page/publisher.rb
alchemy_cms-6.0.14 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.4 app/models/alchemy/page/publisher.rb
alchemy_cms-7.0.0.pre.a app/models/alchemy/page/publisher.rb
alchemy_cms-6.0.13 app/models/alchemy/page/publisher.rb
alchemy_cms-6.1.3 app/models/alchemy/page/publisher.rb