summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-03-01 13:56:24 +0100
committerhukl <contact@smyck.org>2009-03-01 13:56:24 +0100
commitc7757522c2750675fab6c4324453e237b863d84c (patch)
tree3cc50a7f8e5c5ab9dbad1957863422a772dddf14
parent62db596a0807059afe321930c2015b30f8b0e6d9 (diff)
refactored drafts which are now identified by a draft_id on the node rather than by guessing it. i was really that close to make that mistake again.
-rw-r--r--app/controllers/nodes_controller.rb3
-rw-r--r--app/models/node.rb35
-rw-r--r--db/migrate/20090301104237_add_draft_id_to_nodes.rb9
-rw-r--r--test/fixtures/nodes.yml3
-rw-r--r--test/fixtures/pages.yml15
-rw-r--r--test/functional/nodes_controller_test.rb19
-rw-r--r--test/unit/node_test.rb55
7 files changed, 106 insertions, 33 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb
index 9ba91f6..a403b95 100644
--- a/app/controllers/nodes_controller.rb
+++ b/app/controllers/nodes_controller.rb
@@ -12,7 +12,8 @@ class NodesController < ApplicationController
12 12
13 def index 13 def index
14 @nodes = Node.root.descendants.paginate( 14 @nodes = Node.root.descendants.paginate(
15 :include => :head, 15 :include => :head,
16 :include => :draft,
16 :page => params[:page], 17 :page => params[:page],
17 :per_page => 25, 18 :per_page => 25,
18 :order => 'id DESC' 19 :order => 'id DESC'
diff --git a/app/models/node.rb b/app/models/node.rb
index e2cb564..99a2d84 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -5,6 +5,7 @@ class Node < ActiveRecord::Base
5 # Associations 5 # Associations
6 has_many :pages, :order => "revision ASC" 6 has_many :pages, :order => "revision ASC"
7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id 7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id
8 belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id
8 has_many :permissions 9 has_many :permissions
9 10
10 # Callbacks 11 # Callbacks
@@ -41,22 +42,14 @@ class Node < ActiveRecord::Base
41 42
42 # Instance Methods 43 # Instance Methods
43 44
44 # check if there is a page which has a nil :published_at column 45
45 # if there is one - it is considered a draft
46 def draft
47 if draft = pages.find_by_published_at(nil)
48 draft
49 end
50 end
51
52 def find_or_create_draft user 46 def find_or_create_draft user
53 if draft && draft.user == user 47 if draft && draft.user == user
54 draft 48 draft
55 elsif draft && draft.user.nil? 49 elsif draft && draft.user.nil?
56 tmp_draft = draft 50 draft.user = user
57 tmp_draft.user = user 51 save
58 tmp_draft.save 52 draft
59 tmp_draft
60 elsif draft && draft.user != user 53 elsif draft && draft.user != user
61 raise "Page is locked" 54 raise "Page is locked"
62 else 55 else
@@ -65,16 +58,18 @@ class Node < ActiveRecord::Base
65 end 58 end
66 59
67 def create_new_draft user 60 def create_new_draft user
68 page = self.pages.create! 61 empty_page = self.pages.new
62 empty_page.user = user
63 empty_page.save!
64
65 self.draft = empty_page
69 66
70 if self.head 67 if self.head
71 clone_attributes_to page 68 clone_attributes_to draft
72 end 69 end
73 70
74 page.user = user 71 self.save
75 page.save 72 draft
76 page.reload
77 page
78 end 73 end
79 74
80 def clone_attributes_to page 75 def clone_attributes_to page
@@ -98,6 +93,7 @@ class Node < ActiveRecord::Base
98 def publish_draft! 93 def publish_draft!
99 if self.draft 94 if self.draft
100 self.head = self.draft 95 self.head = self.draft
96 self.draft = nil
101 self.save! 97 self.save!
102 98
103 self.head.published_at = Time.now 99 self.head.published_at = Time.now
@@ -131,7 +127,8 @@ class Node < ActiveRecord::Base
131 # that draft and publishes it. 127 # that draft and publishes it.
132 def initialize_empty_page 128 def initialize_empty_page
133 if self.pages.empty? 129 if self.pages.empty?
134 self.pages.create! 130 self.draft = self.pages.create!
131 self.save
135 end 132 end
136 end 133 end
137end 134end
diff --git a/db/migrate/20090301104237_add_draft_id_to_nodes.rb b/db/migrate/20090301104237_add_draft_id_to_nodes.rb
new file mode 100644
index 0000000..a0ea672
--- /dev/null
+++ b/db/migrate/20090301104237_add_draft_id_to_nodes.rb
@@ -0,0 +1,9 @@
1class AddDraftIdToNodes < ActiveRecord::Migration
2 def self.up
3 add_column :nodes, :draft_id, :integer
4 end
5
6 def self.down
7 remove_column :nodes, :draft_id
8 end
9end
diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml
index 642d937..8574c1b 100644
--- a/test/fixtures/nodes.yml
+++ b/test/fixtures/nodes.yml
@@ -14,6 +14,7 @@ first_child:
14 lft: 2 14 lft: 2
15 rgt: 3 15 rgt: 3
16 parent_id: 1 16 parent_id: 1
17 draft_id: 100
17 slug: first_child 18 slug: first_child
18 unique_name: first_child 19 unique_name: first_child
19 20
@@ -22,6 +23,7 @@ second_child:
22 lft: 4 23 lft: 4
23 rgt: 5 24 rgt: 5
24 parent_id: 1 25 parent_id: 1
26 draft_id: 101
25 slug: second_child 27 slug: second_child
26 unique_name: second_child 28 unique_name: second_child
27 29
@@ -30,5 +32,6 @@ third_child:
30 lft: 6 32 lft: 6
31 rgt: 7 33 rgt: 7
32 parent_id: 1 34 parent_id: 1
35 draft_id: 102
33 slug: third_child 36 slug: third_child
34 unique_name: third_child \ No newline at end of file 37 unique_name: third_child \ No newline at end of file
diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml
index 614b9a6..8171b38 100644
--- a/test/fixtures/pages.yml
+++ b/test/fixtures/pages.yml
@@ -8,4 +8,19 @@ one:
8two: 8two:
9 id: 2 9 id: 2
10 revision: 1 10 revision: 1
11 node_id: 4
12
13draft1:
14 id: 100
15 revision: 1
16 node_id: 2
17
18draft2:
19 id: 101
20 revision: 1
21 node_id: 3
22
23draft3:
24 id: 102
25 revision: 1
11 node_id: 4 \ No newline at end of file 26 node_id: 4 \ No newline at end of file
diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb
index d6d579d..85f151a 100644
--- a/test/functional/nodes_controller_test.rb
+++ b/test/functional/nodes_controller_test.rb
@@ -1,8 +1,21 @@
1require 'test_helper' 1require 'test_helper'
2 2
3class NodesControllerTest < ActionController::TestCase 3class NodesControllerTest < ActionController::TestCase
4 # Replace this with your real tests. 4
5 test "the truth" do 5 include AuthenticatedTestHelper
6 assert true 6
7 def test_get_index
8 login_as :quentin
9 get :index
10 assert_response :success
11 end
12
13 def test_update_a_draft
14 test_node = Node.create! :slug => "test_node"
15 test_node.move_to_child_of Node.root
16
17 login_as :quentin
18
19
7 end 20 end
8end 21end
diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb
index 549cccd..2558ca3 100644
--- a/test/unit/node_test.rb
+++ b/test/unit/node_test.rb
@@ -11,6 +11,21 @@ class NodeTest < ActiveSupport::TestCase
11 @user2 = User.create :login => 'show', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar' 11 @user2 = User.create :login => 'show', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar'
12 end 12 end
13 13
14 def test_returning_existing_drafts
15 test_node = Node.create! :slug => "test_node"
16 test_node.move_to_child_of Node.root
17
18 assert_not_nil test_node.draft
19 assert_equal 1, test_node.pages.length
20 assert_nil test_node.draft.user
21
22 3.times do
23 test_node.find_or_create_draft @user1
24 end
25
26 assert_equal 1, test_node.pages.length
27 end
28
14 def test_user_gets_assigned_to_unlocked_draft 29 def test_user_gets_assigned_to_unlocked_draft
15 assert_not_nil @first_child.draft 30 assert_not_nil @first_child.draft
16 assert_nil @first_child.draft.user 31 assert_nil @first_child.draft.user
@@ -34,6 +49,7 @@ class NodeTest < ActiveSupport::TestCase
34 end 49 end
35 50
36 def test_publish_draft_on_a_node_without_a_draft_returns_nil 51 def test_publish_draft_on_a_node_without_a_draft_returns_nil
52
37 assert @first_child.publish_draft! 53 assert @first_child.publish_draft!
38 assert_nil @first_child.publish_draft! 54 assert_nil @first_child.publish_draft!
39 end 55 end
@@ -127,34 +143,34 @@ class NodeTest < ActiveSupport::TestCase
127 143
128 def test_order_of_pages_by_revision 144 def test_order_of_pages_by_revision
129 # This test should make sure the order is the same on different db's 145 # This test should make sure the order is the same on different db's
130 146 # Remember, there is already an empty draft
131 one = @second_child.pages.create :title => "one"
132 two = @second_child.pages.create :title => "two" 147 two = @second_child.pages.create :title => "two"
133 three = @second_child.pages.create :title => "three" 148 three = @second_child.pages.create :title => "three"
149 four = @second_child.pages.create :title => "four"
134 150
135 @second_child.pages.reload 151 @second_child.pages.reload
136 152
137 assert_equal [1,2,3], @second_child.pages.map { |x| x.revision } 153 assert_equal [1,2,3,4], @second_child.pages.map { |x| x.revision }
138 end 154 end
139 155
140 def test_behavior_of_acts_as_list 156 def test_behavior_of_acts_as_list
141 one = @second_child.pages.create :title => "one"
142 two = @second_child.pages.create :title => "two" 157 two = @second_child.pages.create :title => "two"
143 three = @second_child.pages.create :title => "three" 158 three = @second_child.pages.create :title => "three"
159 four = @second_child.pages.create :title => "four"
144 160
145 assert_equal 1, one.revision
146 assert_equal 2, two.revision 161 assert_equal 2, two.revision
147 assert_equal 3, three.revision 162 assert_equal 3, three.revision
163 assert_equal 4, four.revision
148 164
149 assert_equal three, @second_child.pages.last 165 assert_equal four, @second_child.pages.last
150 166
151 assert one.move_to_bottom 167 assert two.move_to_bottom
152 168
153 one.reload; two.reload; three.reload; 169 two.reload; three.reload; four.reload;
154 170
155 assert_equal 3, one.revision 171 assert_equal 4, two.revision
156 assert_equal 1, two.revision
157 assert_equal 2, three.revision 172 assert_equal 2, three.revision
173 assert_equal 3, four.revision
158 end 174 end
159 175
160 def test_retrieving_page_current 176 def test_retrieving_page_current
@@ -201,4 +217,23 @@ class NodeTest < ActiveSupport::TestCase
201 page = Node.find_page("updates/2008/foo", 2) 217 page = Node.find_page("updates/2008/foo", 2)
202 assert_equal "Version 2", page.title 218 assert_equal "Version 2", page.title
203 end 219 end
220
221 # Thats a lengthy test to make sure everything works as it should, it was
222 # created during a bug hunt
223 def test_creating_new_draft
224 test_node = Node.create! :slug => "test_node"
225 test_node.move_to_child_of Node.root
226 test_node.draft.user = @user1
227 test_node.save
228 assert test_node.publish_draft!
229 test_node.reload
230 assert_equal 1, test_node.pages.length
231 assert_not_nil test_node.head
232 assert_nil test_node.draft
233 test_node.find_or_create_draft @user1
234 test_node.reload
235 assert_equal 2, test_node.pages.length
236 assert_not_nil test_node.draft
237 assert test_node.head != test_node.draft
238 end
204end 239end