summaryrefslogtreecommitdiff
path: root/app/models/node.rb
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2011-02-10 14:19:19 +0100
committerhukl <contact@smyck.org>2011-02-10 14:19:19 +0100
commit65276f391645b6f7c786cb7a4b1161b3560a0d6d (patch)
tree3ff5371b62388456f3215e64ad2a331b93a357cf /app/models/node.rb
parent7379daad1c73bd3610ed296436250b417ac3673d (diff)
added body to the index and removed spaces
Diffstat (limited to 'app/models/node.rb')
-rw-r--r--app/models/node.rb89
1 files changed, 45 insertions, 44 deletions
diff --git a/app/models/node.rb b/app/models/node.rb
index db44b71..6c11fed 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -1,7 +1,7 @@
1class Node < ActiveRecord::Base 1class Node < ActiveRecord::Base
2 # Mixins and Plugins 2 # Mixins and Plugins
3 acts_as_nested_set 3 acts_as_nested_set
4 4
5 # Associations 5 # Associations
6 has_many :pages, :order => "revision ASC" 6 has_many :pages, :order => "revision ASC"
7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id 7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id
@@ -9,55 +9,56 @@ class Node < ActiveRecord::Base
9 has_many :permissions 9 has_many :permissions
10 has_one :event 10 has_one :event
11 belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id 11 belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id
12 12
13 # Callbacks 13 # Callbacks
14 after_create :initialize_empty_page 14 after_create :initialize_empty_page
15 before_save :check_for_changed_slug 15 before_save :check_for_changed_slug
16 after_save :update_unique_names_of_children 16 after_save :update_unique_names_of_children
17 17
18 # Validations 18 # Validations
19 validates_length_of :slug, :within => 1..255, :unless => "parent_id.nil?" 19 validates_length_of :slug, :within => 1..255, :unless => "parent_id.nil?"
20 validates_presence_of :slug, :unless => "parent_id.nil?" 20 validates_presence_of :slug, :unless => "parent_id.nil?"
21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => "parent_id.nil?" 21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => "parent_id.nil?"
22 validates_presence_of :parent_id, :unless => "Node.root.nil?" 22 validates_presence_of :parent_id, :unless => "Node.root.nil?"
23 23
24 validate :borders # This should never ever happen. 24 validate :borders # This should never ever happen.
25 25
26 # Index for Fulltext Search 26 # Index for Fulltext Search
27 define_index do 27 define_index do
28 indexes head.translations.title 28 indexes head.translations.title
29 indexes slug 29 indexes slug
30 indexes unique_name 30 indexes unique_name
31 indexes head.translations.body
31 end 32 end
32 33
33 # Class methods 34 # Class methods
34 35
35 # Returns a page for a given node. If no revision is supplied, it returns 36 # Returns a page for a given node. If no revision is supplied, it returns
36 # the last / current one. If a specific revision number is supplied, the 37 # the last / current one. If a specific revision number is supplied, the
37 # corresponding revision of that page is returned. Get the current / latest 38 # corresponding revision of that page is returned. Get the current / latest
38 # revision with -1. It raises an Argument error if the revision is not a 39 # revision with -1. It raises an Argument error if the revision is not a
39 # Fixnum 40 # Fixnum
40 def self.find_page path, revision = -1 41 def self.find_page path, revision = -1
41 unless revision.class == Fixnum 42 unless revision.class == Fixnum
42 raise ArgumentError, "revision must be a Fixnum" 43 raise ArgumentError, "revision must be a Fixnum"
43 end 44 end
44 45
45 node = Node.find_by_unique_name(path) 46 node = Node.find_by_unique_name(path)
46 47
47 if node 48 if node
48 case revision 49 case revision
49 when -1 50 when -1
50 return node.head 51 return node.head
51 else 52 else
52 return node.pages.find_by_revision( revision ) 53 return node.pages.find_by_revision( revision )
53 end 54 end
54 end 55 end
55 56
56 nil 57 nil
57 end 58 end
58 59
59 # Instance Methods 60 # Instance Methods
60 61
61 def find_or_create_draft current_user 62 def find_or_create_draft current_user
62 if draft && self.lock_owner == current_user 63 if draft && self.lock_owner == current_user
63 draft 64 draft
@@ -69,7 +70,7 @@ class Node < ActiveRecord::Base
69 draft 70 draft
70 elsif draft && self.lock_owner != current_user 71 elsif draft && self.lock_owner != current_user
71 raise( 72 raise(
72 LockedByAnotherUser, 73 LockedByAnotherUser,
73 "Page is locked by another user who is working on it! " \ 74 "Page is locked by another user who is working on it! " \
74 "Last modification: #{draft.updated_at.to_s(:db)}" 75 "Last modification: #{draft.updated_at.to_s(:db)}"
75 ) 76 )
@@ -78,42 +79,42 @@ class Node < ActiveRecord::Base
78 create_new_draft current_user 79 create_new_draft current_user
79 end 80 end
80 end 81 end
81 82
82 def create_new_draft user 83 def create_new_draft user
83 empty_page = self.pages.create! 84 empty_page = self.pages.create!
84 empty_page.user = (self.head ? self.head.user : user) 85 empty_page.user = (self.head ? self.head.user : user)
85 empty_page.editor = user 86 empty_page.editor = user
86 empty_page.save 87 empty_page.save
87 88
88 empty_page.clone_attributes_from self.head 89 empty_page.clone_attributes_from self.head
89 90
90 self.draft = empty_page 91 self.draft = empty_page
91 self.save 92 self.save
92 self.draft.reload 93 self.draft.reload
93 end 94 end
94 95
95 def publish_draft! 96 def publish_draft!
96 if self.draft 97 if self.draft
97 self.head = self.draft 98 self.head = self.draft
98 self.head.published_at ||= Time.now 99 self.head.published_at ||= Time.now
99 self.head.save! 100 self.head.save!
100 101
101 self.draft = nil 102 self.draft = nil
102 103
103 if staged_slug && (staged_slug != slug) 104 if staged_slug && (staged_slug != slug)
104 self.slug = staged_slug 105 self.slug = staged_slug
105 end 106 end
106 107
107 if staged_parent_id && (staged_parent_id != parent_id) 108 if staged_parent_id && (staged_parent_id != parent_id)
108 self.parent_id = staged_parent_id 109 self.parent_id = staged_parent_id
109 end 110 end
110 111
111 self.save! 112 self.save!
112 self.unlock! 113 self.unlock!
113 self 114 self
114 end 115 end
115 end 116 end
116 117
117 def restore_revision! revision 118 def restore_revision! revision
118 if page = self.pages.find_by_revision(revision) 119 if page = self.pages.find_by_revision(revision)
119 self.head = page 120 self.head = page
@@ -122,31 +123,31 @@ class Node < ActiveRecord::Base
122 nil 123 nil
123 end 124 end
124 end 125 end
125 126
126 # returns an array with all parts of a unique_name rather than a string 127 # returns an array with all parts of a unique_name rather than a string
127 def unique_path 128 def unique_path
128 unique_name.split("/") rescue [unique_name] 129 unique_name.split("/") rescue [unique_name]
129 end 130 end
130 131
131 # returns array with pages up to root excluding root 132 # returns array with pages up to root excluding root
132 def path_to_root 133 def path_to_root
133 parent.nil? ? [slug] : parent.path_to_root.push(slug) 134 parent.nil? ? [slug] : parent.path_to_root.push(slug)
134 end 135 end
135 136
136 def current_unique_name 137 def current_unique_name
137 path = path_to_root[1..-1] # excluding root 138 path = path_to_root[1..-1] # excluding root
138 self.unique_name = path.join("/") 139 self.unique_name = path.join("/")
139 end 140 end
140 141
141 def update_unique_name 142 def update_unique_name
142 current_unique_name 143 current_unique_name
143 self.save 144 self.save
144 end 145 end
145 146
146 def locked? 147 def locked?
147 !self.lock_owner.nil? 148 !self.lock_owner.nil?
148 end 149 end
149 150
150 def unlock! 151 def unlock!
151 if self.lock_owner 152 if self.lock_owner
152 self.lock_owner = nil 153 self.lock_owner = nil
@@ -154,23 +155,23 @@ class Node < ActiveRecord::Base
154 self 155 self
155 end 156 end
156 end 157 end
157 158
158 def title 159 def title
159 head ? head.title : draft.title 160 head ? head.title : draft.title
160 end 161 end
161 162
162 def update_unique_names? 163 def update_unique_names?
163 !children.empty? && !children.first.path_to_root.include?(self.slug) 164 !children.empty? && !children.first.path_to_root.include?(self.slug)
164 end 165 end
165 166
166 def head? 167 def head?
167 head_id 168 head_id
168 end 169 end
169 170
170 def update? 171 def update?
171 unique_path.length == 3 && unique_path[0] == "updates" 172 unique_path.length == 3 && unique_path[0] == "updates"
172 end 173 end
173 174
174 # Returns immutable node id for all new nodes so that the atom feed entry ids 175 # Returns immutable node id for all new nodes so that the atom feed entry ids
175 # stay the same eventhough the slug or positions changes. 176 # stay the same eventhough the slug or positions changes.
176 # Can be removed after a year or so ;) 177 # Can be removed after a year or so ;)
@@ -178,13 +179,13 @@ class Node < ActiveRecord::Base
178 new_id_format_date = "2009-11-14".to_time 179 new_id_format_date = "2009-11-14".to_time
179 self.created_at < new_id_format_date ? unique_path : id 180 self.created_at < new_id_format_date ? unique_path : id
180 end 181 end
181 182
182 protected 183 protected
183 def lock_for! current_user 184 def lock_for! current_user
184 self.lock_owner = current_user 185 self.lock_owner = current_user
185 self.save 186 self.save
186 end 187 end
187 188
188 # Creates an empty page and associates it to the given node. This means 189 # Creates an empty page and associates it to the given node. This means
189 # freshly created node has an empty draft. A user can create nodes as he 190 # freshly created node has an empty draft. A user can create nodes as he
190 # wants to which will not appear on the public page until the author edits 191 # wants to which will not appear on the public page until the author edits
@@ -195,14 +196,14 @@ class Node < ActiveRecord::Base
195 self.save 196 self.save
196 end 197 end
197 end 198 end
198 199
199 def check_for_changed_slug 200 def check_for_changed_slug
200 if parent and changed.include? "slug" 201 if parent and changed.include? "slug"
201 self.unique_name = current_unique_name 202 self.unique_name = current_unique_name
202 end 203 end
203 end 204 end
204 205
205 # Watch out recursion ahead! update_unique_name itself triggers this 206 # Watch out recursion ahead! update_unique_name itself triggers this
206 # after_save callback which invokes update_unique_name on its children. 207 # after_save callback which invokes update_unique_name on its children.
207 # Hopefully until no childrens occur 208 # Hopefully until no childrens occur
208 def update_unique_names_of_children 209 def update_unique_names_of_children
@@ -212,7 +213,7 @@ class Node < ActiveRecord::Base
212 end 213 end
213 end 214 end
214 end 215 end
215 216
216 def borders 217 def borders
217 if lft && rgt && (lft > rgt) 218 if lft && rgt && (lft > rgt)
218 errors.add("Fuck!. lft should never be smaller than rgt") 219 errors.add("Fuck!. lft should never be smaller than rgt")