Sha256: 6c6cb1bac30cf4cc72cf9438a9d86524716674e1bb6eef4e0874cecb8d4fdc1e

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Alchemy
  class PageVersion < BaseRecord
    belongs_to :page, class_name: "Alchemy::Page", inverse_of: :versions, touch: true

    has_many :elements, -> { order(:position) },
      class_name: "Alchemy::Element",
      inverse_of: :page_version

    scope :drafts, -> { where(public_on: nil).order(updated_at: :desc) }
    scope :published, -> { where.not(public_on: nil).order(public_on: :desc) }

    def self.public_on(time = Time.current)
      where("#{table_name}.public_on <= :time AND " \
            "(#{table_name}.public_until IS NULL " \
            "OR #{table_name}.public_until >= :time)", time: time)
    end

    before_destroy :delete_elements

    # Determines if this version is public
    #
    # Takes the two timestamps +public_on+ and +public_until+
    # and returns true if the time given (+Time.current+ per default)
    # is in this timespan.
    #
    # @param time [DateTime] (Time.current)
    # @returns Boolean
    def public?(time = Time.current)
      already_public_for?(time) && still_public_for?(time)
    end

    # Determines if this version is already public for given time
    # @param time [DateTime] (Time.current)
    # @returns Boolean
    def already_public_for?(time = Time.current)
      !public_on.nil? && public_on <= time
    end

    # Determines if this version is still public for given time
    # @param time [DateTime] (Time.current)
    # @returns Boolean
    def still_public_for?(time = Time.current)
      public_until.nil? || public_until >= time
    end

    def element_repository
      ElementsRepository.new(elements.includes({ contents: :essence }, :tags))
    end

    private

    def delete_elements
      DeleteElements.new(elements).call
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
alchemy_cms-6.0.5 app/models/alchemy/page_version.rb
alchemy_cms-6.0.4 app/models/alchemy/page_version.rb
alchemy_cms-6.0.3 app/models/alchemy/page_version.rb
alchemy_cms-6.0.2 app/models/alchemy/page_version.rb
alchemy_cms-6.0.1 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc7 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc6 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc5 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc4 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc3 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc2 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.rc1 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.b6 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.b5 app/models/alchemy/page_version.rb
alchemy_cms-6.0.0.pre.b4 app/models/alchemy/page_version.rb