diff options
| author | hukl <contact@smyck.org> | 2009-03-02 21:23:20 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-02 21:23:20 +0100 |
| commit | 0566800f0f22829fcdb14c383a0388b4f1ca60f0 (patch) | |
| tree | 72f32fc5c3b8e0f4da1b06feca24bfad4c3cbb2e | |
| parent | fecf81e546b174ee5a8fb939b234aae2a1cb91b0 (diff) | |
refactoring of the page cloning and draft creating
| -rw-r--r-- | app/models/node.rb | 31 | ||||
| -rw-r--r-- | app/models/page.rb | 18 | ||||
| -rw-r--r-- | 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 | |||
| 59 | def create_new_draft user | 59 | def create_new_draft user |
| 60 | empty_page = self.pages.new | 60 | empty_page = self.pages.new |
| 61 | empty_page.user = user | 61 | empty_page.user = user |
| 62 | empty_page.save! | ||
| 63 | 62 | ||
| 64 | self.draft = empty_page | 63 | empty_page.clone_attributes_from self.head |
| 65 | |||
| 66 | if self.head | ||
| 67 | clone_attributes_to draft | ||
| 68 | end | ||
| 69 | 64 | ||
| 65 | self.draft = empty_page | ||
| 70 | self.save | 66 | self.save |
| 71 | draft | 67 | self.draft.reload |
| 72 | end | ||
| 73 | |||
| 74 | def clone_attributes_to page | ||
| 75 | page.tag_list = self.head.tag_list.join(", ") | ||
| 76 | |||
| 77 | locale_before = I18n.locale | ||
| 78 | |||
| 79 | I18n.available_locales.each do |l| | ||
| 80 | next if l == :root | ||
| 81 | I18n.locale = l | ||
| 82 | page.title = self.head.title | ||
| 83 | page.abstract = self.head.abstract | ||
| 84 | page.body = self.head.body | ||
| 85 | end | ||
| 86 | |||
| 87 | I18n.locale = locale_before | ||
| 88 | |||
| 89 | page.save | ||
| 90 | page | ||
| 91 | end | 68 | end |
| 92 | 69 | ||
| 93 | def publish_draft! | 70 | def publish_draft! |
| @@ -113,7 +90,7 @@ class Node < ActiveRecord::Base | |||
| 113 | parent.nil? && [slug] || parent.path_to_root.push(slug) | 90 | parent.nil? && [slug] || parent.path_to_root.push(slug) |
| 114 | end | 91 | end |
| 115 | 92 | ||
| 116 | def update_unique_name | 93 | def update_unique_name |
| 117 | path = self.path_to_root[1..-1] | 94 | path = self.path_to_root[1..-1] |
| 118 | self.unique_name = path.join("/") | 95 | self.unique_name = path.join("/") |
| 119 | self.save | 96 | 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 | |||
| 45 | end | 45 | end |
| 46 | 46 | ||
| 47 | # Instance Methods | 47 | # Instance Methods |
| 48 | |||
| 49 | def clone_attributes_from page | ||
| 50 | return nil unless page | ||
| 51 | |||
| 52 | self.tag_list = page.tag_list.join(", ") | ||
| 53 | |||
| 54 | locale_before = I18n.locale | ||
| 55 | |||
| 56 | I18n.available_locales.each do |l| | ||
| 57 | next if l == :root | ||
| 58 | I18n.locale = l | ||
| 59 | self.title = page.title | ||
| 60 | self.abstract = page.abstract | ||
| 61 | self.body = page.body | ||
| 62 | end | ||
| 63 | |||
| 64 | I18n.locale = locale_before | ||
| 65 | end | ||
| 48 | end \ No newline at end of file | 66 | 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 | |||
| 37 | 37 | ||
| 38 | get :edit, :id => node.id | 38 | get :edit, :id => node.id |
| 39 | assert_response :success | 39 | assert_response :success |
| 40 | assert_select("#page_title[value=Hello]") | ||
| 41 | assert_select("#page_body", "World") | ||
| 40 | 42 | ||
| 41 | node.reload | 43 | node.reload |
| 42 | assert_equal 2, node.pages.length | 44 | assert_equal 2, node.pages.length |
