Sha256: a2446a5156ad15af950319a2d7c137250c52d0095084ffaadba9ebb63a190b2c

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

module Effective
  class Permalink < ApplicationRecord
    self.table_name = (EffectivePages.permalinks_table_name || :permalinks).to_s

    if defined?(PgSearch)
      include PgSearch::Model

      multisearchable against: [:title, :summary]
    end

    has_one_attached :attachment
    has_one_purgable :attachment

    acts_as_tagged
    acts_as_slugged

    acts_as_trackable if respond_to?(:acts_as_trackable)
    log_changes if respond_to?(:log_changes)

    scope :deep, -> { 
      base = with_attached_attachment
      base = base.includes(:pg_search_document) if defined?(PgSearch)
      base
    }

    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
      "/link/#{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

7 entries across 7 versions & 1 rubygems

Version Path
effective_pages-3.13.4 app/models/effective/permalink.rb
effective_pages-3.13.3 app/models/effective/permalink.rb
effective_pages-3.13.2 app/models/effective/permalink.rb
effective_pages-3.13.1 app/models/effective/permalink.rb
effective_pages-3.13.0 app/models/effective/permalink.rb
effective_pages-3.12.0 app/models/effective/permalink.rb
effective_pages-3.11.0 app/models/effective/permalink.rb