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/controllers/nodes_controller.rb | 5 ++--- app/models/node.rb | 25 ++++++++++++++++++------- app/models/page.rb | 24 ++++++++++-------------- 3 files changed, 30 insertions(+), 24 deletions(-) (limited to 'app') diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index f773c6a..1796090 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -78,9 +78,8 @@ class NodesController < ApplicationController def unlock # TODO that actually has to be implemented in the model, once we have # permissions - if draft = @node.draft - draft.user = nil - draft.save + if @node.user + @node.unlock! flash[:notice] = "Node unlocked" else flash[:notice] = "Cannot unlock" 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 diff --git a/app/models/page.rb b/app/models/page.rb index 4c4536e..58f9682 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -87,27 +87,23 @@ class Page < ActiveRecord::Base def clone_attributes_from page return nil unless page - + + self.reload + # Clone untranslated attributes - self.tag_list = page.tag_list.join(", ") self.template_name = page.template_name self.published_at = page.published_at - # Clone translated attributes + # Getting rid of the auto-generated empty translations + self.globalize_translations.delete_all - locale_before = I18n.locale - - I18n.available_locales.each do |l| - next if l == :root - I18n.locale = l - page.reload - self.title = page.title unless page.title.try(:fallback?) - self.abstract = page.abstract unless page.abstract.try(:fallback?) - self.body = page.body unless page.body.try(:fallback?) + # Clone translated attributes + page.globalize_translations.each do |translation| + self.globalize_translations.create!(translation.attributes) end - - I18n.locale = locale_before + + self.save end def public? -- cgit v1.3