Repository URL to install this package:
|
Version:
4.3.0 ▾
|
module Evergreen
class Page < ActiveRecord::Base
extend FriendlyId
self.table_name = "evergreen_pages"
class << self
def register_system_page(identifier, title)
define_singleton_method("#{identifier}_page") { where(title: title).first_or_initialize }
end
def find_by_path(request_path)
find_by! path: request_path
end
end
scope :published, -> { where('drafted IS NULL or drafted = ?', false) }
scope :drafted, -> { where(drafted: true) }
scope :hidden, -> { where(hidden: true) }
scope :visible, -> { published.where('hidden IS NULL or hidden = ?', false) }
acts_as_nested_set scope: 'type'
friendly_id :navigation_label, use: :scoped, scope: :parent
validates_uniqueness_of :path, if: :using_custom_path
validates_presence_of :path, if: :using_custom_path
after_save :update_path!, :update_descendants_path
after_move :update_path!
def navigation_label
self[:navigation_label].present? ? self[:navigation_label] : title
end
def to_param
path
end
def published?
not drafted?
end
def state
published? ? "published" : "drafted"
end
def update_path!
return path if using_custom_path
update_column :path, generate_path
path
end
def inherited_attribute(*method_chain)
value = method_chain.inject(self, :try)
return value if value.present?
upward_ancestors.detect { |ancestor| value = method_chain.inject(ancestor, :try) }
value
end
def upward_ancestors
ancestors.sort_by(&:level).reverse
end
def should_generate_new_friendly_id?
title_changed? || navigation_label_changed?
end
private
def generate_path
slug_ancestry.join('/')
end
def slug_ancestry
self_and_ancestors.map(&:slug).reject(&:blank?)
end
def update_descendants_path
reload.descendants.each(&:update_path!)
end
end
end