diff options
| author | hukl <contact@smyck.org> | 2009-09-08 20:06:44 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-09-08 20:06:44 +0200 |
| commit | b48ee28e586fd75e84e33fabfcba70c05d32a606 (patch) | |
| tree | 3654bd300fa8a2a387c50b53d23c794da49762d0 | |
| parent | b981d83e5e005817a67b11ccf92dd65bb506bcd4 (diff) | |
lots of gui improvements for creating nodes
| -rw-r--r-- | app/controllers/nodes_controller.rb | 14 | ||||
| -rw-r--r-- | app/views/nodes/new.html.erb | 12 | ||||
| -rw-r--r-- | lib/Update.rb | 17 | ||||
| -rw-r--r-- | public/javascripts/admin_search.js | 22 | ||||
| -rw-r--r-- | test/functional/nodes_controller_test.rb | 51 |
5 files changed, 101 insertions, 15 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index f8b8058..a23354c 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb | |||
| @@ -28,15 +28,17 @@ class NodesController < ApplicationController | |||
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | def create | 30 | def create |
| 31 | @node = Node.new( params[:node] ) | 31 | parent = case params[:kind] |
| 32 | 32 | when "top_level" then Node.root | |
| 33 | parent = Node.find(params[:parent_id]) | 33 | when "update" then Update.find_or_create_parent |
| 34 | when "generic" then Node.find(params[:parent_id]) | ||
| 35 | end | ||
| 34 | 36 | ||
| 35 | if parent and @node.save | 37 | if parent |
| 36 | @node.move_to_child_of parent | 38 | @node = parent.children.create(:slug => params[:title].parameterize.to_s) |
| 39 | @node.draft.update_attributes(:title => params[:title]) | ||
| 37 | redirect_to(edit_node_path(@node)) | 40 | redirect_to(edit_node_path(@node)) |
| 38 | else | 41 | else |
| 39 | @node.errors.add("Parent node") | ||
| 40 | render :action => :new | 42 | render :action => :new |
| 41 | end | 43 | end |
| 42 | end | 44 | end |
diff --git a/app/views/nodes/new.html.erb b/app/views/nodes/new.html.erb index 935e34f..e11030e 100644 --- a/app/views/nodes/new.html.erb +++ b/app/views/nodes/new.html.erb | |||
| @@ -5,16 +5,20 @@ | |||
| 5 | 5 | ||
| 6 | <p>What kind of node do you want to create?</p> | 6 | <p>What kind of node do you want to create?</p> |
| 7 | 7 | ||
| 8 | <% form_for nodes_path do %> | 8 | <% form_tag nodes_path do %> |
| 9 | <table id="new_node"> | 9 | <table id="new_node"> |
| 10 | <tr> | 10 | <tr> |
| 11 | <td class="description">Type</td> | 11 | <td class="description">Type</td> |
| 12 | <td> | 12 | <td> |
| 13 | <div> | 13 | <div> |
| 14 | <%= radio_button_tag :kind, "generic" %> | 14 | <%= radio_button_tag :kind, "top_level" %> |
| 15 | Generic ( can be created anywhere ) | 15 | Top Level |
| 16 | </div> | 16 | </div> |
| 17 | <p> | 17 | <p> |
| 18 | <%= radio_button_tag :kind, "generic", :selected => true %> | ||
| 19 | Generic ( can be created anywhere ) | ||
| 20 | </p> | ||
| 21 | <p> | ||
| 18 | <%= radio_button_tag :kind, "update" %> | 22 | <%= radio_button_tag :kind, "update" %> |
| 19 | Update / Press release ( is automatically created in /updates ) | 23 | Update / Press release ( is automatically created in /updates ) |
| 20 | </p> | 24 | </p> |
| @@ -24,7 +28,7 @@ | |||
| 24 | <td class="description">Title</td> | 28 | <td class="description">Title</td> |
| 25 | <td><%= text_field_tag :title %></td> | 29 | <td><%= text_field_tag :title %></td> |
| 26 | </tr> | 30 | </tr> |
| 27 | <tr> | 31 | <tr id="parent_search_field"> |
| 28 | <td class="description">Parent</td> | 32 | <td class="description">Parent</td> |
| 29 | <td> | 33 | <td> |
| 30 | <%= text_field_tag :parent_search_term %> | 34 | <%= text_field_tag :parent_search_term %> |
diff --git a/lib/Update.rb b/lib/Update.rb new file mode 100644 index 0000000..480bb0b --- /dev/null +++ b/lib/Update.rb | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | module Update | ||
| 2 | |||
| 3 | def self.find_or_create_parent | ||
| 4 | current_year = Time.now.year.to_s | ||
| 5 | |||
| 6 | if parent = Node.find_by_unique_name("updates/#{current_year}") | ||
| 7 | parent | ||
| 8 | else | ||
| 9 | unless updates = Node.find_by_unique_name("updates") | ||
| 10 | updates = Node.root.children.create(:slug => "updates") | ||
| 11 | end | ||
| 12 | parent = updates.children.create(:slug => current_year) | ||
| 13 | parent | ||
| 14 | end | ||
| 15 | end | ||
| 16 | |||
| 17 | end \ No newline at end of file | ||
diff --git a/public/javascripts/admin_search.js b/public/javascripts/admin_search.js index 1800506..f670273 100644 --- a/public/javascripts/admin_search.js +++ b/public/javascripts/admin_search.js | |||
| @@ -100,6 +100,8 @@ menu_items = { | |||
| 100 | 100 | ||
| 101 | parent_search = { | 101 | parent_search = { |
| 102 | initialize_search : function() { | 102 | initialize_search : function() { |
| 103 | parent_search.initialize_radio_buttons(); | ||
| 104 | |||
| 103 | $("#parent_search_term").bind("keyup", function() { | 105 | $("#parent_search_term").bind("keyup", function() { |
| 104 | if ($(this).attr("value")) { | 106 | if ($(this).attr("value")) { |
| 105 | $.ajax({ | 107 | $.ajax({ |
| @@ -118,10 +120,11 @@ parent_search = { | |||
| 118 | } | 120 | } |
| 119 | }); | 121 | }); |
| 120 | }, | 122 | }, |
| 123 | |||
| 121 | show_results : function(results) { | 124 | show_results : function(results) { |
| 122 | $("#search_results").empty(); | 125 | $("#search_results").empty(); |
| 123 | for (result in results) { | 126 | for (result in results) { |
| 124 | var link = $(("<a href='#' rel='" + results[result].id+ "'>"+ results[result].title + "</a>")); | 127 | var link = $(("<a href='#'>"+ results[result].title + "</a>")); |
| 125 | $(link).bind("click", parent_search.link_closure(results[result])); | 128 | $(link).bind("click", parent_search.link_closure(results[result])); |
| 126 | 129 | ||
| 127 | 130 | ||
| @@ -134,10 +137,10 @@ parent_search = { | |||
| 134 | 137 | ||
| 135 | } | 138 | } |
| 136 | }, | 139 | }, |
| 140 | |||
| 137 | link_closure : function(node) { | 141 | link_closure : function(node) { |
| 138 | var barf = function(){ | 142 | var barf = function(){ |
| 139 | $("#parent_search_term").attr("value", node.title); | 143 | $("#parent_search_term").attr("value", node.title); |
| 140 | alert(node.id); | ||
| 141 | $("#parent_id").attr("value", node.node_id); | 144 | $("#parent_id").attr("value", node.node_id); |
| 142 | $('#search_results').slideUp(); | 145 | $('#search_results').slideUp(); |
| 143 | $('#search_results').empty(); | 146 | $('#search_results').empty(); |
| @@ -145,5 +148,20 @@ parent_search = { | |||
| 145 | } | 148 | } |
| 146 | 149 | ||
| 147 | return barf; | 150 | return barf; |
| 151 | }, | ||
| 152 | |||
| 153 | initialize_radio_buttons : function() { | ||
| 154 | $("#kind_top_level").bind("change", function(){ | ||
| 155 | $("#parent_search_field").hide(); | ||
| 156 | }); | ||
| 157 | |||
| 158 | $("#kind_update").bind("change", function(){ | ||
| 159 | $("#parent_search_field").hide(); | ||
| 160 | }); | ||
| 161 | |||
| 162 | $("#kind_generic").bind("change", function(){ | ||
| 163 | $("#parent_search_field").show(); | ||
| 164 | }); | ||
| 165 | |||
| 148 | } | 166 | } |
| 149 | } | 167 | } |
diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb index ff9aa68..7559869 100644 --- a/test/functional/nodes_controller_test.rb +++ b/test/functional/nodes_controller_test.rb | |||
| @@ -19,10 +19,55 @@ class NodesControllerTest < ActionController::TestCase | |||
| 19 | assert_response :success | 19 | assert_response :success |
| 20 | end | 20 | end |
| 21 | 21 | ||
| 22 | def test_create | 22 | test "create generic node with parent_id provided" do |
| 23 | login_as :quentin | 23 | login_as :quentin |
| 24 | post :create, :node => {:slug => 'foobar'}, :parent_id => Node.root.id | 24 | assert_difference "Node.count", +1 do |
| 25 | assert_redirected_to edit_node_path(Node.last) | 25 | post( |
| 26 | :create, | ||
| 27 | :kind => "generic", | ||
| 28 | :parent_id => Node.first.id, | ||
| 29 | :title => "Hello Spaceboy" | ||
| 30 | ) | ||
| 31 | end | ||
| 32 | |||
| 33 | assert_response :redirect | ||
| 34 | assert_equal "hello-spaceboy", Node.last.slug | ||
| 35 | assert_equal Node.last.parent_id, Node.first.id | ||
| 36 | assert_equal 1, Node.last.level | ||
| 37 | end | ||
| 38 | |||
| 39 | test "create update node" do | ||
| 40 | login_as :quentin | ||
| 41 | #difference of three because "updates" and "2009" node get created as well | ||
| 42 | assert_difference "Node.count", +3 do | ||
| 43 | post( | ||
| 44 | :create, | ||
| 45 | :kind => "update", | ||
| 46 | :title => "Hello Spaceboy" | ||
| 47 | ) | ||
| 48 | end | ||
| 49 | |||
| 50 | assert_response :redirect | ||
| 51 | expected = "updates/#{Time.now.year.to_s}/hello-spaceboy" | ||
| 52 | assert_equal expected, Node.last.unique_name | ||
| 53 | assert_equal 3, Node.last.level | ||
| 54 | end | ||
| 55 | |||
| 56 | test "create top level node" do | ||
| 57 | login_as :quentin | ||
| 58 | |||
| 59 | assert_difference "Node.count", +1 do | ||
| 60 | post( | ||
| 61 | :create, | ||
| 62 | :kind => "top_level", | ||
| 63 | :title => "Hello Spaceboy" | ||
| 64 | ) | ||
| 65 | end | ||
| 66 | |||
| 67 | assert_response :redirect | ||
| 68 | expected = "hello-spaceboy" | ||
| 69 | assert_equal expected, Node.last.unique_name | ||
| 70 | assert_equal 1, Node.last.level | ||
| 26 | end | 71 | end |
| 27 | 72 | ||
| 28 | def test_editing_a_node | 73 | def test_editing_a_node |
