| 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
# 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
# File app/models/page.rb, line 47
47: def set_body
48: self.body = "" unless self.body
49: end
Parent slug paths + slug
# 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
# File app/models/page.rb, line 20
20: def set_slug
21: self.slug = self.title.downcase.gsub(" ", "_") if slug.blank?
22: end
# File app/models/page.rb, line 69
69: def to_s
70: "#<Page #{id} #{title} #{slug} #{path}>"
71: end
# 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