This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
/
page.rb
111 lines (88 loc) · 2.76 KB
/
page.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class Page < ActiveRecord::Base
UNPUBLISHED_PAGES = %w(sign_up_header sign_up_verify)
belongs_to :parent, :class_name => 'Page'
has_many :children, :class_name => 'Page', :foreign_key => 'parent_id', :dependent => :destroy
has_many :attachments
belongs_to :site
scope_by_site_id
attr_accessible :slug, :title, :body, :parent_id, :parent, :path, :published, :navigation, :raw
validates_presence_of :slug, :title, :body
validates_uniqueness_of :path, :scope => :site_id
validates_exclusion_of :slug, :in => %w(admin edit new)
validates_format_of :slug, :with => /^[a-z][a-z_]*$/
before_save :update_path
def update_path
if parent
self.path = parent.path + '/' + slug
else
self.path = slug
end
end
def name; title; end
def home?
path == 'home'
end
def body
if uncooked = read_attribute(:body)
cooked = Verse.link_references_in_text(uncooked)
cooked.gsub!(/\{\{([a-z\s'&,.]+)\}\}/i) do
"<a href=\"/search?name=#{CGI.escape($1)}\">#{$1}</a>"
end
cooked.gsub!(/(%5B%5B|\[\[)([a-z_]+)%7C([a-z_]+)(%5D%5D|\]\])/, "[[\\2|\\3]]")
cooked.gsub(/\[\[([a-z_]+)\|([a-z_]+)\]\]/) do
Setting.get($1.to_sym, $2.to_sym).to_s rescue '???'
end
end
end
def navigation_pages
if home?
Page.root_pages
else
children.find_all_by_published_and_navigation(true, true)
end
end
def for_members?
path =~ /^system\//
end
before_destroy :cannot_destroy_system_page
def cannot_destroy_system_page
if system?
errors.add(:base, 'Cannot delete system pages.')
return false
end
end
class << self
def find(id, *args)
if id.is_a?(String) and id !~ /^\d+$/
find_by_path(id).tap do |page|
raise ActiveRecord::RecordNotFound unless page
end
else
super
end
end
def find_by_id_or_path(id_or_path)
if id_or_path.is_a?(String) and id_or_path !~ /^\d+$/
find_by_path(id_or_path)
else
find_by_id(id_or_path)
end
end
def find_by_path(path)
find(:first, :conditions => ['path = ?', normalize_path(path)])
end
def normalize_path(path)
path = home_if_blank(path)
path.sub(%r{^/}, '').sub(%r{/$}, '').gsub(%r{//}, '/').gsub(/\s/, '').downcase
end
def home_if_blank(path)
path.to_s.empty? ? 'home' : path
end
def paths_and_ids
connection.select_all("select path, id from pages where path != '' and site_id = #{Site.current.id} order by path").map { |r| [r['path'], r['id'].to_i] }
end
def root_pages(include_home=false, published=true, navigation=true)
Page.find_all_by_parent_id_and_published_and_navigation(nil, published, navigation).select { |p| include_home or not p.home? }
end
end
end