Sha256: cab61f3a870f8ee0ed94a928ece78a2bc1678ccc3b38da589e9c7734bbfa22fe

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module Effective
  class Permalink < ApplicationRecord
    if defined?(PgSearch)
      include PgSearch::Model

      multisearchable against: [:title, :summary]
    end

    self.table_name = (EffectivePages.permalinks_table_name || :permalinks).to_s

    has_one_attached :attachment
    has_one_purgable :attachment

    acts_as_slugged

    log_changes if respond_to?(:log_changes)

    scope :deep, -> { with_attached_attachment }

    effective_resource do
      title        :string
      slug         :string

      url          :string

      summary      :text

      timestamps
    end

    validate  :attachment_and_url_cannot_both_be_present

    validates :title,       presence: true
    validates :attachment,  presence: true, if: -> { url.blank?          }
    validates :url,         presence: true, if: -> { attachment.blank?   }, url: true

    public

    def to_s
      title.presence || model_name.human
    end

    def redirect_path
      "/permalinks/#{slug}"
    end

    def target
      url.present? ? :url : :attachment
    end

    private

    def attachment_and_url_cannot_both_be_present
      if url.present? && (attachment.attached? && !attachment.marked_for_destruction?)
        self.errors.add(:base, 'Attachment and URL cannot both be present')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
effective_pages-3.7.6 app/models/effective/permalink.rb
effective_pages-3.7.5 app/models/effective/permalink.rb
effective_pages-3.7.4 app/models/effective/permalink.rb
effective_pages-3.7.3 app/models/effective/permalink.rb