summaryrefslogtreecommitdiff
path: root/app/models/node.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/node.rb')
-rw-r--r--app/models/node.rb65
1 files changed, 37 insertions, 28 deletions
diff --git a/app/models/node.rb b/app/models/node.rb
index 311c5c0..b41df06 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -170,10 +170,12 @@ class Node < ApplicationRecord
170 self.autosave.destroy 170 self.autosave.destroy
171 self.autosave_id = nil 171 self.autosave_id = nil
172 self.save! 172 self.save!
173 NodeAction.record!(:node => self, :user => current_user, :action => "discard_autosave")
173 elsif self.draft && self.head 174 elsif self.draft && self.head
174 self.draft.destroy 175 self.draft.destroy
175 self.draft_id = nil 176 self.draft_id = nil
176 self.save! 177 self.save!
178 NodeAction.record!(:node => self, :user => current_user, :action => "destroy_draft")
177 end 179 end
178 180
179 self.unlock! unless self.draft 181 self.unlock! unless self.draft
@@ -188,43 +190,50 @@ class Node < ApplicationRecord
188 end 190 end
189 end 191 end
190 192
191 def publish_draft! 193 def publish_draft! current_user = nil
192 # Return nil if nothing to publish and no staged changes 194 # Return nil if nothing to publish and no staged changes
193 return nil unless self.draft || staged_slug || staged_parent_id 195 return nil unless self.draft || staged_slug || staged_parent_id
194 196
195 if self.draft 197 ActiveRecord::Base.transaction do
196 self.head = self.draft 198 if self.draft
197 self.head.published_at ||= Time.now 199 previous_title = self.head ? Globalize.with_locale(I18n.default_locale) { self.head.title } : nil
198 self.head.save! 200 self.head = self.draft
199 self.draft = nil 201 self.head.published_at ||= Time.now
200 end 202 self.head.save!
201 203 self.draft = nil
202 if staged_slug && (staged_slug != slug)
203 self.slug = staged_slug
204 self.staged_slug = nil
205 end
206 204
207 if staged_parent_id && (staged_parent_id != parent_id) 205 new_title = Globalize.with_locale(I18n.default_locale) { self.head.title }
208 new_parent = Node.find(staged_parent_id) 206 NodeAction.record!(:node => self, :page => self.head, :user => current_user,
207 :action => "publish", :from => previous_title, :to => new_title)
208 end
209 209
210 if new_parent == self || self.descendants.include?(new_parent) 210 if staged_slug && (staged_slug != slug)
211 raise ActiveRecord::RecordInvalid.new(self), "Cannot move a node under itself or one of its own descendants" 211 self.slug = staged_slug
212 self.staged_slug = nil
212 end 213 end
213 214
214 self.staged_parent_id = nil 215 if staged_parent_id && (staged_parent_id != parent_id)
215 self.save! 216 new_parent = Node.find(staged_parent_id)
216 self.move_to_child_of(new_parent) 217
217 else 218 if new_parent == self || self.descendants.include?(new_parent)
218 unless self.save 219 raise ActiveRecord::RecordInvalid.new(self), "Cannot move a node under itself or one of its own descendants"
219 raise ActiveRecord::RecordInvalid.new(self) 220 end
221
222 self.staged_parent_id = nil
223 self.save!
224 self.move_to_child_of(new_parent)
225 else
226 unless self.save
227 raise ActiveRecord::RecordInvalid.new(self)
228 end
220 end 229 end
221 end
222 230
223 self.reload 231 self.reload
224 self.update_unique_name 232 self.update_unique_name
225 self.send(:update_unique_names_of_children) 233 self.send(:update_unique_names_of_children)
226 self.unlock! 234 self.unlock!
227 self 235 self
236 end
228 end 237 end
229 238
230 def restore_revision! revision 239 def restore_revision! revision