summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-09-10 12:00:03 +0200
committerhukl <contact@smyck.org>2009-09-10 12:00:03 +0200
commit8afffc09bcaa4d1c77f1ca575fd5978423631bff (patch)
treecc9c4d8d402337903584d845f5f2df6afdc52ba1 /app
parentf7ce8c245fde3f6202103ae614b181c0ba44ed93 (diff)
refactored the create method to actually produce meaningful error messages when something goes wrong. added tests for corner cases
Diffstat (limited to 'app')
-rw-r--r--app/controllers/nodes_controller.rb36
-rw-r--r--app/models/node.rb3
-rw-r--r--app/views/nodes/create.html.erb2
3 files changed, 28 insertions, 13 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb
index 0d4a3fe..b34c19e 100644
--- a/app/controllers/nodes_controller.rb
+++ b/app/controllers/nodes_controller.rb
@@ -26,23 +26,24 @@ class NodesController < ApplicationController
26 def new 26 def new
27 @node = Node.new params[:node] 27 @node = Node.new params[:node]
28 end 28 end
29 29
30 def create 30 def create
31 parent = case params[:kind] 31 params[:title] ||= ""
32 when "top_level" then Node.root
33 when "update" then Update.find_or_create_parent
34 when "generic" then Node.find(params[:parent_id])
35 end
36 32
37 if parent 33 @node = Node.new
38 @node = parent.children.create(:slug => params[:title].parameterize.to_s) 34 @node.parent_id = find_parent
35 @node.slug = params[:title].parameterize.to_s
36
37 if @node.save
39 @node.draft.update_attributes(:title => params[:title]) 38 @node.draft.update_attributes(:title => params[:title])
40 redirect_to(edit_node_path(@node)) 39 redirect_to(edit_node_path(@node))
41 else 40 else
42 render :action => :new 41 debugger
42 #@node.errors.add_to_base("Titel zu kurz") if @node.errors["slug"]
43 render :new
43 end 44 end
44 end 45 end
45 46
46 def show 47 def show
47 @page = Node.find(params[:id]).pages.last 48 @page = Node.find(params[:id]).pages.last
48 end 49 end
@@ -105,4 +106,19 @@ class NodesController < ApplicationController
105 def find_node 106 def find_node
106 @node = Node.find(params[:id]) 107 @node = Node.find(params[:id])
107 end 108 end
109
110 def find_parent
111 case params[:kind]
112 when "top_level"
113 Node.root.id
114 when "update"
115 Update.find_or_create_parent.id
116 when "generic"
117 if params[:parent_id] && Node.find(params[:parent_id])
118 params[:parent_id]
119 else
120 nil
121 end
122 end
123 end
108end 124end
diff --git a/app/models/node.rb b/app/models/node.rb
index 057248e..fa9d519 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -16,7 +16,8 @@ class Node < ActiveRecord::Base
16 after_save :update_unique_names_of_children 16 after_save :update_unique_names_of_children
17 17
18 # Validations 18 # Validations
19 # validates_length_of :slug, :within => 3..40 19 validates_length_of :slug, :within => 1..255
20 validates_presence_of :slug
20 validates_uniqueness_of :slug, :scope => :parent_id 21 validates_uniqueness_of :slug, :scope => :parent_id
21 22
22 # Index for Fulltext Search 23 # Index for Fulltext Search
diff --git a/app/views/nodes/create.html.erb b/app/views/nodes/create.html.erb
deleted file mode 100644
index 05b6282..0000000
--- a/app/views/nodes/create.html.erb
+++ /dev/null
@@ -1,2 +0,0 @@
1<h1>Nodes#create</h1>
2<p>Find me in app/views/nodes/create.html.erb</p>