From bb9d9ccd57ce5673b52a1856dfd32337f4ae4b22 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 8 Feb 2009 14:06:31 +0100 Subject: refactored refined concept of nodes and pages into the node model --- app/models/node.rb | 27 +++++++++++++++- 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 has_many :pages, :order => "revision ASC" belongs_to :head, :class_name => "Page", :foreign_key => :head_id + # Callbacks + + after_create :initialize_empty_page + # Class methods @@ -43,12 +47,20 @@ class Node < ActiveRecord::Base draft else # make a new fresh page with node reference - draft = Page.new( :node_id => id) + draft = Page.create( head.attributes ) end draft end + def publish_draft! + self.head = self.draft + self.save! + + self.head.published_at = Time.now + self.head.save! + end + # returns an array with all parts of a unique_name rather than a string def unique_path unique_name.split("/") rescue unique_name @@ -64,4 +76,17 @@ class Node < ActiveRecord::Base self.unique_name = path.join("/") self.save end + + protected + + # Creates an empty page, associates it to the given node and sets its + # published_at date so it isn't considered a draft. Look up the draft + # method! + def initialize_empty_page + if self.pages.empty? + self.pages.create! + end + end end + + 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 @first_child = Node.find(2) @second_child = Node.find(3) end - - def test_creation_of_unique_name - node = Node.create :slug => 'child' + + def test_created_nodes_have_an_empty_draft_and_no_head + node = Node.create :slug => "third_child" node.move_to_child_of @root - node.reload - assert_equal 'child', node.unique_name - node = Node.create :slug => 'deep_child' - node.move_to_child_of @first_child - node.reload - assert_equal 'first_child/deep_child', node.unique_name + assert !node.pages.empty? + assert_equal 1, node.pages.length + assert_not_nil node.draft + assert_nil node.head end - def test_retrieving_page_current - updates = Node.create(:slug => 'updates') - updates.move_to_child_of @root - - year = Node.create(:slug => '2008') - year.move_to_child_of updates - - foo = Node.create(:slug => 'foo') - foo.move_to_child_of year - - assert_not_nil Node.find_by_unique_name('updates/2008/foo') + def test_create_new_draft_of_published_page + node = Node.create :slug => "third_child" + node.move_to_child_of @root - foo.pages.create :title => "Version 1" - foo.pages.create :title => "Version 2" - foo.pages.create :title => "Version 3" + assert node.publish_draft! - foo.head = foo.pages.last - foo.save! + draft = node.draft - page = Node.find_page("updates/2008/foo") - assert_equal page, foo.pages.find_by_revision(3) + assert_equal 2, node.pages.length end - def test_retrieving_page_by_revision - updates = Node.create(:slug => 'updates') - updates.move_to_child_of @root - - year = Node.create(:slug => '2008') - year.move_to_child_of updates - - foo = Node.create(:slug => 'foo') - foo.move_to_child_of year - - assert_not_nil Node.find_by_unique_name('updates/2008/foo') - - foo.pages.create :title => "Version 1" - foo.pages.create :title => "Version 2" - foo.pages.create :title => "Version 3" - - page = Node.find_page("updates/2008/foo", 2) - assert_equal "Version 2", page.title + def test_creation_of_unique_name + node = Node.create :slug => 'child' + node.move_to_child_of @root + node.reload + assert_equal 'child', node.unique_name + + node = Node.create :slug => 'deep_child' + node.move_to_child_of @first_child + node.reload + assert_equal 'first_child/deep_child', node.unique_name end def test_order_of_pages_by_revision + # This test should make sure the order is the same on different db's + one = @second_child.pages.create :title => "one" two = @second_child.pages.create :title => "two" three = @second_child.pages.create :title => "three" - + @second_child.pages.reload - + assert_equal [1,2,3], @second_child.pages.map { |x| x.revision } end @@ -77,17 +57,17 @@ class NodeTest < ActiveSupport::TestCase one = @second_child.pages.create :title => "one" two = @second_child.pages.create :title => "two" three = @second_child.pages.create :title => "three" - + assert_equal 1, one.revision assert_equal 2, two.revision assert_equal 3, three.revision - + assert_equal three, @second_child.pages.last - + assert one.move_to_bottom - + one.reload; two.reload; three.reload; - + assert_equal 3, one.revision assert_equal 1, two.revision assert_equal 2, three.revision -- cgit v1.3