From 0566800f0f22829fcdb14c383a0388b4f1ca60f0 Mon Sep 17 00:00:00 2001 From: hukl Date: Mon, 2 Mar 2009 21:23:20 +0100 Subject: refactoring of the page cloning and draft creating --- app/models/node.rb | 31 ++++--------------------------- app/models/page.rb | 18 ++++++++++++++++++ test/functional/nodes_controller_test.rb | 2 ++ 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index 2e711cf..7f2bca6 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -59,35 +59,12 @@ class Node < ActiveRecord::Base def create_new_draft user empty_page = self.pages.new empty_page.user = user - empty_page.save! - self.draft = empty_page - - if self.head - clone_attributes_to draft - end + empty_page.clone_attributes_from self.head + self.draft = empty_page self.save - draft - end - - def clone_attributes_to page - page.tag_list = self.head.tag_list.join(", ") - - locale_before = I18n.locale - - I18n.available_locales.each do |l| - next if l == :root - I18n.locale = l - page.title = self.head.title - page.abstract = self.head.abstract - page.body = self.head.body - end - - I18n.locale = locale_before - - page.save - page + self.draft.reload end def publish_draft! @@ -113,7 +90,7 @@ class Node < ActiveRecord::Base parent.nil? && [slug] || parent.path_to_root.push(slug) end - def update_unique_name + def update_unique_name path = self.path_to_root[1..-1] self.unique_name = path.join("/") self.save diff --git a/app/models/page.rb b/app/models/page.rb index 2cf9d72..7ac8f35 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -45,4 +45,22 @@ class Page < ActiveRecord::Base end # Instance Methods + + def clone_attributes_from page + return nil unless page + + self.tag_list = page.tag_list.join(", ") + + locale_before = I18n.locale + + I18n.available_locales.each do |l| + next if l == :root + I18n.locale = l + self.title = page.title + self.abstract = page.abstract + self.body = page.body + end + + I18n.locale = locale_before + end end \ No newline at end of file diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb index afe7576..20090d0 100644 --- a/test/functional/nodes_controller_test.rb +++ b/test/functional/nodes_controller_test.rb @@ -37,6 +37,8 @@ class NodesControllerTest < ActionController::TestCase get :edit, :id => node.id assert_response :success + assert_select("#page_title[value=Hello]") + assert_select("#page_body", "World") node.reload assert_equal 2, node.pages.length -- cgit v1.3