diff options
| -rw-r--r-- | app/models/node.rb | 35 | ||||
| -rw-r--r-- | app/models/page.rb | 1 | ||||
| -rw-r--r-- | test/unit/node_test.rb | 78 |
3 files changed, 97 insertions, 17 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index cd02657..bc48ac4 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -10,7 +10,6 @@ class Node < ActiveRecord::Base | |||
| 10 | 10 | ||
| 11 | # Class methods | 11 | # Class methods |
| 12 | 12 | ||
| 13 | |||
| 14 | # Returns a page for a given node. If no revision is supplied, it returns | 13 | # Returns a page for a given node. If no revision is supplied, it returns |
| 15 | # the last / current one. If a specific revision number is supplied, the | 14 | # the last / current one. If a specific revision number is supplied, the |
| 16 | # corresponding revision of that page is returned. Get the current / latest | 15 | # corresponding revision of that page is returned. Get the current / latest |
| @@ -37,28 +36,34 @@ class Node < ActiveRecord::Base | |||
| 37 | 36 | ||
| 38 | # Instance Methods | 37 | # Instance Methods |
| 39 | 38 | ||
| 39 | # check if there is a page which has a nil :published_at column | ||
| 40 | # if there is one - it is considered a draft | ||
| 40 | def draft | 41 | def draft |
| 41 | |||
| 42 | # check if there is a page which has a nil :published_at column | ||
| 43 | # if there is one - it is considered a draft else a new revision is | ||
| 44 | # created | ||
| 45 | |||
| 46 | if draft = pages.find_by_published_at(nil) | 42 | if draft = pages.find_by_published_at(nil) |
| 47 | draft | 43 | draft |
| 44 | end | ||
| 45 | end | ||
| 46 | |||
| 47 | def find_or_create_draft user | ||
| 48 | if draft && draft.user == user | ||
| 49 | draft | ||
| 50 | elsif draft && draft.user != user | ||
| 51 | raise "Page is locked" | ||
| 48 | else | 52 | else |
| 49 | # make a new fresh page with node reference | 53 | self.pages.create! :user_id => user.id |
| 50 | draft = Page.create( head.attributes ) | ||
| 51 | end | 54 | end |
| 52 | |||
| 53 | draft | ||
| 54 | end | 55 | end |
| 55 | 56 | ||
| 56 | def publish_draft! | 57 | def publish_draft! |
| 57 | self.head = self.draft | 58 | if self.draft |
| 58 | self.save! | 59 | self.head = self.draft |
| 59 | 60 | self.save! | |
| 60 | self.head.published_at = Time.now | 61 | |
| 61 | self.head.save! | 62 | self.head.published_at = Time.now |
| 63 | self.head.save! | ||
| 64 | else | ||
| 65 | nil | ||
| 66 | end | ||
| 62 | end | 67 | end |
| 63 | 68 | ||
| 64 | # returns an array with all parts of a unique_name rather than a string | 69 | # returns an array with all parts of a unique_name rather than a string |
diff --git a/app/models/page.rb b/app/models/page.rb index df8308b..5647ef9 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -8,6 +8,7 @@ class Page < ActiveRecord::Base | |||
| 8 | 8 | ||
| 9 | # Associations | 9 | # Associations |
| 10 | belongs_to :node | 10 | belongs_to :node |
| 11 | belongs_to :user | ||
| 11 | 12 | ||
| 12 | # Class Methods | 13 | # Class Methods |
| 13 | 14 | ||
diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 6e62447..d4317ca 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb | |||
| @@ -6,6 +6,9 @@ class NodeTest < ActiveSupport::TestCase | |||
| 6 | @root = Node.find(1) | 6 | @root = Node.find(1) |
| 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 | |||
| 10 | @user1 = User.create :login => 'demo' | ||
| 11 | @user2 = User.create :login => 'show' | ||
| 9 | end | 12 | end |
| 10 | 13 | ||
| 11 | def test_created_nodes_have_an_empty_draft_and_no_head | 14 | def test_created_nodes_have_an_empty_draft_and_no_head |
| @@ -15,6 +18,7 @@ class NodeTest < ActiveSupport::TestCase | |||
| 15 | assert !node.pages.empty? | 18 | assert !node.pages.empty? |
| 16 | assert_equal 1, node.pages.length | 19 | assert_equal 1, node.pages.length |
| 17 | assert_not_nil node.draft | 20 | assert_not_nil node.draft |
| 21 | assert_nil node.draft.user | ||
| 18 | assert_nil node.head | 22 | assert_nil node.head |
| 19 | end | 23 | end |
| 20 | 24 | ||
| @@ -23,10 +27,35 @@ class NodeTest < ActiveSupport::TestCase | |||
| 23 | node.move_to_child_of @root | 27 | node.move_to_child_of @root |
| 24 | 28 | ||
| 25 | assert node.publish_draft! | 29 | assert node.publish_draft! |
| 30 | end | ||
| 31 | |||
| 32 | def test_find_or_create_draft_if_no_draft_exists | ||
| 33 | node = Node.create :slug => "third_child" | ||
| 34 | node.move_to_child_of @root | ||
| 35 | node.publish_draft! | ||
| 36 | |||
| 37 | assert_not_nil node.find_or_create_draft( @user1 ) | ||
| 38 | end | ||
| 39 | |||
| 40 | def test_find_or_create_draft_if_draft_exists_and_is_owned_by_user | ||
| 41 | node = Node.create :slug => "third_child" | ||
| 42 | node.move_to_child_of @root | ||
| 43 | node.publish_draft! | ||
| 44 | |||
| 45 | node.find_or_create_draft @user1 | ||
| 46 | node.find_or_create_draft @user1 | ||
| 47 | end | ||
| 48 | |||
| 49 | def test_exception_if_draft_exists_but_locked_by_another_user | ||
| 50 | node = Node.create :slug => "third_child" | ||
| 51 | node.move_to_child_of @root | ||
| 52 | node.publish_draft! | ||
| 26 | 53 | ||
| 27 | draft = node.draft | 54 | node.find_or_create_draft @user1 |
| 28 | 55 | ||
| 29 | assert_equal 2, node.pages.length | 56 | assert_raise(RuntimeError) do |
| 57 | node.find_or_create_draft @user2 | ||
| 58 | end | ||
| 30 | end | 59 | end |
| 31 | 60 | ||
| 32 | def test_creation_of_unique_name | 61 | def test_creation_of_unique_name |
| @@ -72,4 +101,49 @@ class NodeTest < ActiveSupport::TestCase | |||
| 72 | assert_equal 1, two.revision | 101 | assert_equal 1, two.revision |
| 73 | assert_equal 2, three.revision | 102 | assert_equal 2, three.revision |
| 74 | end | 103 | end |
| 104 | |||
| 105 | def test_retrieving_page_current | ||
| 106 | updates = Node.create(:slug => 'updates') | ||
| 107 | updates.move_to_child_of @root | ||
| 108 | |||
| 109 | year = Node.create(:slug => '2008') | ||
| 110 | year.move_to_child_of updates | ||
| 111 | |||
| 112 | foo = Node.create(:slug => 'foo') | ||
| 113 | foo.move_to_child_of year | ||
| 114 | |||
| 115 | assert_not_nil Node.find_by_unique_name('updates/2008/foo') | ||
| 116 | |||
| 117 | # Note that there is already an initial, blank revision | ||
| 118 | foo.pages.create :title => "Version 2" | ||
| 119 | foo.pages.create :title => "Version 3" | ||
| 120 | foo.pages.create :title => "Version 4" | ||
| 121 | |||
| 122 | foo.head = foo.pages.last | ||
| 123 | foo.save! | ||
| 124 | |||
| 125 | page = Node.find_page("updates/2008/foo") | ||
| 126 | assert_equal page, foo.pages.find_by_revision(4) | ||
| 127 | end | ||
| 128 | |||
| 129 | def test_retrieving_page_by_revision | ||
| 130 | updates = Node.create(:slug => 'updates') | ||
| 131 | updates.move_to_child_of @root | ||
| 132 | |||
| 133 | year = Node.create(:slug => '2008') | ||
| 134 | year.move_to_child_of updates | ||
| 135 | |||
| 136 | foo = Node.create(:slug => 'foo') | ||
| 137 | foo.move_to_child_of year | ||
| 138 | |||
| 139 | assert_not_nil Node.find_by_unique_name('updates/2008/foo') | ||
| 140 | |||
| 141 | # Note that there is already an initial, blank revision | ||
| 142 | foo.pages.create :title => "Version 2" | ||
| 143 | foo.pages.create :title => "Version 3" | ||
| 144 | foo.pages.create :title => "Version 4" | ||
| 145 | |||
| 146 | page = Node.find_page("updates/2008/foo", 2) | ||
| 147 | assert_equal "Version 2", page.title | ||
| 148 | end | ||
| 75 | end | 149 | end |
