Sha256: 4c35775e82c7c59a8afe2fddb125bdd1b0f3ab2d9dfd6ff8f28e19cd79941815

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

module Kms
  class Page < ActiveRecord::Base
    include Liquor::Dropable
    include CompileTemplates

    INDEX_SLUG = "index"
    INDEX_FULLPATH = ""

    scope :published, lambda { where(published: true) }
    scope :listed, lambda { where(hidden: false) }
    scope :not_templatable, lambda { where(templatable: false) }

    belongs_to :template

    validates :slug, uniqueness: true, presence: true

    before_save :build_fullpath

    before_create :set_position

    has_ancestry

    def index?
      slug == INDEX_SLUG
    end

    def build_fullpath
      if index?
        self.fullpath = INDEX_FULLPATH # self.slug
      else
        slugs = (ancestors + [self]).map(&:slug)
        slugs.shift unless slugs.size == 1
        self.fullpath = File.join slugs.compact
      end
    end

    def set_position
      if parent.present?
        max = parent.children.maximum(:position)
      else
        max = Page.where('ancestry IS ?', nil).maximum(:position)
      end
      self.position = max ? max + 1 : 0
    end

    # fetch items for templatable page
    def fetch_items
      templatable_type.constantize.all
    end

    # fetch item by slug
    def fetch_item!(slug)
      return nil unless templatable?
      templatable_type.constantize.find_by_slug!(slug)
    end

    def register_id
      "#{self.class.name.parameterize("_")}_#{id}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kms-0.7.0 app/models/kms/page.rb
kms-0.6.0 app/models/kms/page.rb
kms-0.5.0 app/models/kms/page.rb
kms-0.4.2 app/models/kms/page.rb
kms-0.4.1 app/models/kms/page.rb
kms-0.4.0 app/models/kms/page.rb