class Page

CMS web page. Tree structure. Versioned. User render_page helper to look for Page before falling back on Rails templates. Pages uses ERb and can execute Ruby code just like a template, so admin users can do things like <% Person.destroy_all %>!

Uses ActsAsVersions, and should be moved to VestalVersions

Public Instance Methods

last_updated_by() click to toggle source
# File app/models/page.rb, line 43
def last_updated_by
  versions.last.try(:user) || created_by
end
set_body() click to toggle source

Can’t reliably set default value for MySQL text field

# File app/models/page.rb, line 48
def set_body
  self.body = "" unless body
  body
end
set_created_by() click to toggle source
# File app/models/page.rb, line 38
def set_created_by
  self.created_by = Person.current
  true
end
set_path() click to toggle source

Parent slug paths + slug

# File app/models/page.rb, line 29
def set_path
  # Ouch
  _ancestors = ancestors.reverse
  _ancestors.delete(self.parent)
  _ancestors << Page.find(self.parent_id) if self.parent_id
  
  self.path = (_ancestors << self).map(&:slug).join("/").gsub(%r^\//, "")
end
set_slug() click to toggle source

Friendly param. Last segment in path

# File app/models/page.rb, line 23
def set_slug
  self.slug = title.downcase.gsub(" ", "_") if slug.blank?
  slug
end
to_s() click to toggle source
# File app/models/page.rb, line 68
def to_s
  "#<Page #{id} #{title} #{slug} #{path}>"
end
update_parent() click to toggle source
# File app/models/page.rb, line 53
def update_parent
  if parent(true)
    parent.skip_version do
      parent.touch
    end
  end
  true
end
valid_parents() click to toggle source
# File app/models/page.rb, line 62
def valid_parents
  Page.all.delete_if { |page|
    page == self || descendants.include?(page)
  }
end