Class Page
In: app/models/page.rb
Parent: ActiveRecord::Base

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

Methods

Public Instance methods

[Source]

    # File app/models/page.rb, line 34
34:   def set_author
35:     # Yeah, yeah, a big side-effect.
36:     unless self.author_id
37:       system_person = Person.find_by_name("System")
38:       password = rand.to_s
39:       unless system_person
40:         system_person = Person.create!(:name => "System")
41:       end
42:       self.author = system_person
43:     end
44:   end

Can‘t reliably set default value for MySQL text field

[Source]

    # File app/models/page.rb, line 47
47:   def set_body
48:     self.body = "" unless self.body
49:   end

Parent slug paths + slug

[Source]

    # File app/models/page.rb, line 25
25:   def set_path
26:     # Ouch
27:     _ancestors = ancestors.reverse
28:     _ancestors.delete(self.parent)
29:     _ancestors << Page.find(self.parent_id) if self.parent_id
30:     
31:     self.path = (_ancestors << self).map(&:slug).join("/").gsub(/^\//, "")
32:   end

Friendly param. Last segment in path

[Source]

    # File app/models/page.rb, line 20
20:   def set_slug
21:     self.slug = self.title.downcase.gsub(" ", "_") if slug.blank?
22:   end

[Source]

    # File app/models/page.rb, line 69
69:   def to_s
70:     "#<Page #{id} #{title} #{slug} #{path}>"
71:   end

[Source]

    # File app/models/page.rb, line 51
51:   def update_parent
52:     if parent(true)
53:       parent.without_revision do
54:         parent.update_attribute(:updated_at, Time.zone.now)
55:       end
56:     end
57:   end

[Source]

    # File app/models/page.rb, line 59
59:   def valid_parents
60:     Page.find(:all).delete_if { |page|
61:       page == self || descendants.include?(page)
62:     }
63:   end

[Source]

    # File app/models/page.rb, line 65
65:   def version
66:     self.lock_version
67:   end

[Validate]