From a660a7ee62e44314c12440d376166ccb0cf60d80 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 1 Feb 2009 17:10:20 +0100 Subject: added test for retrieving a specific revision via a given node. turned out it wasn't working the way I thought so I rewrote it to use only Fixnums --- app/controllers/content_controller.rb | 3 +-- app/models/node.rb | 16 ++++++++++++---- test/unit/node_test.rb | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index 5312ecd..278c5ab 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -12,7 +12,6 @@ class ContentController < ApplicationController :status => 404 ) end - + end - end diff --git a/app/models/node.rb b/app/models/node.rb index 5acf563..3564ce4 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -5,16 +5,24 @@ class Node < ActiveRecord::Base # Class methods - def self.find_page path, revision = :current + + # Returns a page for a given node. If no revision is supplied, it returns + # the last / current one. If a specific revision number is supplied, the + # corresponding revision of that page is returned. Get the current / latest + # revision with -1. It raises an Argument error if the revision is not a + # Fixnum + def self.find_page path, revision = -1 + unless revision.class == Fixnum + raise ArgumentError, "revision must be a Fixnum" + end node = Node.find_by_unique_name(path) if node - case revision - when :current + when -1 return node.pages.last - when /\d+/ + else return node.pages.find_by_revision revision end end diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 56012d6..5325daa 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -41,7 +41,23 @@ class NodeTest < ActiveSupport::TestCase 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 end def test_order_of_pages_by_revision -- cgit v1.3