diff options
| author | hukl <hukl@eight.local> | 2009-02-08 14:06:31 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-02-08 14:06:31 +0100 |
| commit | bb9d9ccd57ce5673b52a1856dfd32337f4ae4b22 (patch) | |
| tree | 1e2de680b5aec6badb6f7778ded00cef99d7c6e5 | |
| parent | 56e1df622f8cb6fb558b83fc4d2836ad0aece7d7 (diff) | |
refactored refined concept of nodes and pages into
the node model
| -rw-r--r-- | app/models/node.rb | 27 | ||||
| -rw-r--r-- | test/unit/node_test.rb | 84 |
2 files changed, 58 insertions, 53 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 4e865e3..cd02657 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -4,6 +4,10 @@ class Node < ActiveRecord::Base | |||
| 4 | has_many :pages, :order => "revision ASC" | 4 | has_many :pages, :order => "revision ASC" |
| 5 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id | 5 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id |
| 6 | 6 | ||
| 7 | # Callbacks | ||
| 8 | |||
| 9 | after_create :initialize_empty_page | ||
| 10 | |||
| 7 | # Class methods | 11 | # Class methods |
| 8 | 12 | ||
| 9 | 13 | ||
| @@ -43,12 +47,20 @@ class Node < ActiveRecord::Base | |||
| 43 | draft | 47 | draft |
| 44 | else | 48 | else |
| 45 | # make a new fresh page with node reference | 49 | # make a new fresh page with node reference |
| 46 | draft = Page.new( :node_id => id) | 50 | draft = Page.create( head.attributes ) |
| 47 | end | 51 | end |
| 48 | 52 | ||
| 49 | draft | 53 | draft |
| 50 | end | 54 | end |
| 51 | 55 | ||
| 56 | def publish_draft! | ||
| 57 | self.head = self.draft | ||
| 58 | self.save! | ||
| 59 | |||
| 60 | self.head.published_at = Time.now | ||
| 61 | self.head.save! | ||
| 62 | end | ||
| 63 | |||
| 52 | # returns an array with all parts of a unique_name rather than a string | 64 | # returns an array with all parts of a unique_name rather than a string |
| 53 | def unique_path | 65 | def unique_path |
| 54 | unique_name.split("/") rescue unique_name | 66 | unique_name.split("/") rescue unique_name |
| @@ -64,4 +76,17 @@ class Node < ActiveRecord::Base | |||
| 64 | self.unique_name = path.join("/") | 76 | self.unique_name = path.join("/") |
| 65 | self.save | 77 | self.save |
| 66 | end | 78 | end |
| 79 | |||
| 80 | protected | ||
| 81 | |||
| 82 | # Creates an empty page, associates it to the given node and sets its | ||
| 83 | # published_at date so it isn't considered a draft. Look up the draft | ||
| 84 | # method! | ||
| 85 | def initialize_empty_page | ||
| 86 | if self.pages.empty? | ||
| 87 | self.pages.create! | ||
| 88 | end | ||
| 89 | end | ||
| 67 | end | 90 | end |
| 91 | |||
| 92 | |||
diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index a0fee7f..6e62447 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb | |||
| @@ -7,69 +7,49 @@ class NodeTest < ActiveSupport::TestCase | |||
| 7 | @first_child = Node.find(2) | 7 | @first_child = Node.find(2) |
| 8 | @second_child = Node.find(3) | 8 | @second_child = Node.find(3) |
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | def test_creation_of_unique_name | 11 | def test_created_nodes_have_an_empty_draft_and_no_head |
| 12 | node = Node.create :slug => 'child' | 12 | node = Node.create :slug => "third_child" |
| 13 | node.move_to_child_of @root | 13 | node.move_to_child_of @root |
| 14 | node.reload | ||
| 15 | assert_equal 'child', node.unique_name | ||
| 16 | 14 | ||
| 17 | node = Node.create :slug => 'deep_child' | 15 | assert !node.pages.empty? |
| 18 | node.move_to_child_of @first_child | 16 | assert_equal 1, node.pages.length |
| 19 | node.reload | 17 | assert_not_nil node.draft |
| 20 | assert_equal 'first_child/deep_child', node.unique_name | 18 | assert_nil node.head |
| 21 | end | 19 | end |
| 22 | 20 | ||
| 23 | def test_retrieving_page_current | 21 | def test_create_new_draft_of_published_page |
| 24 | updates = Node.create(:slug => 'updates') | 22 | node = Node.create :slug => "third_child" |
| 25 | updates.move_to_child_of @root | 23 | node.move_to_child_of @root |
| 26 | |||
| 27 | year = Node.create(:slug => '2008') | ||
| 28 | year.move_to_child_of updates | ||
| 29 | |||
| 30 | foo = Node.create(:slug => 'foo') | ||
| 31 | foo.move_to_child_of year | ||
| 32 | |||
| 33 | assert_not_nil Node.find_by_unique_name('updates/2008/foo') | ||
| 34 | 24 | ||
| 35 | foo.pages.create :title => "Version 1" | 25 | assert node.publish_draft! |
| 36 | foo.pages.create :title => "Version 2" | ||
| 37 | foo.pages.create :title => "Version 3" | ||
| 38 | 26 | ||
| 39 | foo.head = foo.pages.last | 27 | draft = node.draft |
| 40 | foo.save! | ||
| 41 | 28 | ||
| 42 | page = Node.find_page("updates/2008/foo") | 29 | assert_equal 2, node.pages.length |
| 43 | assert_equal page, foo.pages.find_by_revision(3) | ||
| 44 | end | 30 | end |
| 45 | 31 | ||
| 46 | def test_retrieving_page_by_revision | 32 | def test_creation_of_unique_name |
| 47 | updates = Node.create(:slug => 'updates') | 33 | node = Node.create :slug => 'child' |
| 48 | updates.move_to_child_of @root | 34 | node.move_to_child_of @root |
| 49 | 35 | node.reload | |
| 50 | year = Node.create(:slug => '2008') | 36 | assert_equal 'child', node.unique_name |
| 51 | year.move_to_child_of updates | 37 | |
| 52 | 38 | node = Node.create :slug => 'deep_child' | |
| 53 | foo = Node.create(:slug => 'foo') | 39 | node.move_to_child_of @first_child |
| 54 | foo.move_to_child_of year | 40 | node.reload |
| 55 | 41 | assert_equal 'first_child/deep_child', node.unique_name | |
| 56 | assert_not_nil Node.find_by_unique_name('updates/2008/foo') | ||
| 57 | |||
| 58 | foo.pages.create :title => "Version 1" | ||
| 59 | foo.pages.create :title => "Version 2" | ||
| 60 | foo.pages.create :title => "Version 3" | ||
| 61 | |||
| 62 | page = Node.find_page("updates/2008/foo", 2) | ||
| 63 | assert_equal "Version 2", page.title | ||
| 64 | end | 42 | end |
| 65 | 43 | ||
| 66 | def test_order_of_pages_by_revision | 44 | def test_order_of_pages_by_revision |
| 45 | # This test should make sure the order is the same on different db's | ||
| 46 | |||
| 67 | one = @second_child.pages.create :title => "one" | 47 | one = @second_child.pages.create :title => "one" |
| 68 | two = @second_child.pages.create :title => "two" | 48 | two = @second_child.pages.create :title => "two" |
| 69 | three = @second_child.pages.create :title => "three" | 49 | three = @second_child.pages.create :title => "three" |
| 70 | 50 | ||
| 71 | @second_child.pages.reload | 51 | @second_child.pages.reload |
| 72 | 52 | ||
| 73 | assert_equal [1,2,3], @second_child.pages.map { |x| x.revision } | 53 | assert_equal [1,2,3], @second_child.pages.map { |x| x.revision } |
| 74 | end | 54 | end |
| 75 | 55 | ||
| @@ -77,17 +57,17 @@ class NodeTest < ActiveSupport::TestCase | |||
| 77 | one = @second_child.pages.create :title => "one" | 57 | one = @second_child.pages.create :title => "one" |
| 78 | two = @second_child.pages.create :title => "two" | 58 | two = @second_child.pages.create :title => "two" |
| 79 | three = @second_child.pages.create :title => "three" | 59 | three = @second_child.pages.create :title => "three" |
| 80 | 60 | ||
| 81 | assert_equal 1, one.revision | 61 | assert_equal 1, one.revision |
| 82 | assert_equal 2, two.revision | 62 | assert_equal 2, two.revision |
| 83 | assert_equal 3, three.revision | 63 | assert_equal 3, three.revision |
| 84 | 64 | ||
| 85 | assert_equal three, @second_child.pages.last | 65 | assert_equal three, @second_child.pages.last |
| 86 | 66 | ||
| 87 | assert one.move_to_bottom | 67 | assert one.move_to_bottom |
| 88 | 68 | ||
| 89 | one.reload; two.reload; three.reload; | 69 | one.reload; two.reload; three.reload; |
| 90 | 70 | ||
| 91 | assert_equal 3, one.revision | 71 | assert_equal 3, one.revision |
| 92 | assert_equal 1, two.revision | 72 | assert_equal 1, two.revision |
| 93 | assert_equal 2, three.revision | 73 | assert_equal 2, three.revision |
