From 06ec666fa8fad0aafe7d8e505f6e92b729fccbce Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 22 Mar 2009 17:14:08 +0100 Subject: Finally! The cloning of pages for creating new drafts is now a lot cleaner. I had to search for a while to figure out a better way. Thank you svenfuchs and joshmh for the support! --- app/models/node.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'app/models/node.rb') diff --git a/app/models/node.rb b/app/models/node.rb index 0f34b73..04d98b5 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -11,7 +11,8 @@ class Node < ActiveRecord::Base belongs_to :user, :foreign_key => :locking_user_id # Callbacks - after_create :initialize_empty_page + after_create :initialize_empty_page + before_save :check_for_changed_slug # Validations # validates_length_of :slug, :within => 3..40 @@ -59,7 +60,7 @@ class Node < ActiveRecord::Base end def create_new_draft user - empty_page = self.pages.new + empty_page = self.pages.create empty_page.user = user empty_page.clone_attributes_from self.head @@ -96,17 +97,17 @@ class Node < ActiveRecord::Base self.save end + def unlock! + self.user = nil + self.save + end + protected def lock_for! current_user self.user = current_user self.save end - def unlock! - self.user = nil - self.save - end - # Creates an empty page and associates it to the given node. This means # freshly created node has an empty draft. A user can create nodes as he # wants to which will not appear on the public page until the author edits @@ -117,6 +118,16 @@ class Node < ActiveRecord::Base self.save end end + + def check_for_changed_slug + if parent and changed.include? "slug" + self.update_unique_name + + if tmp_descendants = descendants + tmp_descendants.each { |descendant| descendant.update_unique_name } + end + end + end end -- cgit v1.3