From b48ee28e586fd75e84e33fabfcba70c05d32a606 Mon Sep 17 00:00:00 2001 From: hukl Date: Tue, 8 Sep 2009 20:06:44 +0200 Subject: lots of gui improvements for creating nodes --- app/controllers/nodes_controller.rb | 14 +++++---- app/views/nodes/new.html.erb | 12 +++++--- lib/Update.rb | 17 +++++++++++ public/javascripts/admin_search.js | 22 ++++++++++++-- test/functional/nodes_controller_test.rb | 51 ++++++++++++++++++++++++++++++-- 5 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 lib/Update.rb 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 end def create - @node = Node.new( params[:node] ) - - parent = Node.find(params[:parent_id]) + parent = case params[:kind] + when "top_level" then Node.root + when "update" then Update.find_or_create_parent + when "generic" then Node.find(params[:parent_id]) + end - if parent and @node.save - @node.move_to_child_of parent + if parent + @node = parent.children.create(:slug => params[:title].parameterize.to_s) + @node.draft.update_attributes(:title => params[:title]) redirect_to(edit_node_path(@node)) else - @node.errors.add("Parent node") render :action => :new end 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,15 +5,19 @@

What kind of node do you want to create?

-<% form_for nodes_path do %> +<% form_tag nodes_path do %> - +
Type
- <%= radio_button_tag :kind, "generic" %> - Generic ( can be created anywhere ) + <%= radio_button_tag :kind, "top_level" %> + Top Level
+

+ <%= radio_button_tag :kind, "generic", :selected => true %> + Generic ( can be created anywhere ) +

<%= radio_button_tag :kind, "update" %> Update / Press release ( is automatically created in /updates ) @@ -24,7 +28,7 @@

Title <%= text_field_tag :title %>
Parent <%= 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 @@ +module Update + + def self.find_or_create_parent + current_year = Time.now.year.to_s + + if parent = Node.find_by_unique_name("updates/#{current_year}") + parent + else + unless updates = Node.find_by_unique_name("updates") + updates = Node.root.children.create(:slug => "updates") + end + parent = updates.children.create(:slug => current_year) + parent + end + end + +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 = { parent_search = { initialize_search : function() { + parent_search.initialize_radio_buttons(); + $("#parent_search_term").bind("keyup", function() { if ($(this).attr("value")) { $.ajax({ @@ -118,10 +120,11 @@ parent_search = { } }); }, + show_results : function(results) { $("#search_results").empty(); for (result in results) { - var link = $((""+ results[result].title + "")); + var link = $((""+ results[result].title + "")); $(link).bind("click", parent_search.link_closure(results[result])); @@ -134,10 +137,10 @@ parent_search = { } }, + link_closure : function(node) { var barf = function(){ $("#parent_search_term").attr("value", node.title); - alert(node.id); $("#parent_id").attr("value", node.node_id); $('#search_results').slideUp(); $('#search_results').empty(); @@ -145,5 +148,20 @@ parent_search = { } return barf; + }, + + initialize_radio_buttons : function() { + $("#kind_top_level").bind("change", function(){ + $("#parent_search_field").hide(); + }); + + $("#kind_update").bind("change", function(){ + $("#parent_search_field").hide(); + }); + + $("#kind_generic").bind("change", function(){ + $("#parent_search_field").show(); + }); + } } 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 assert_response :success end - def test_create + test "create generic node with parent_id provided" do login_as :quentin - post :create, :node => {:slug => 'foobar'}, :parent_id => Node.root.id - assert_redirected_to edit_node_path(Node.last) + assert_difference "Node.count", +1 do + post( + :create, + :kind => "generic", + :parent_id => Node.first.id, + :title => "Hello Spaceboy" + ) + end + + assert_response :redirect + assert_equal "hello-spaceboy", Node.last.slug + assert_equal Node.last.parent_id, Node.first.id + assert_equal 1, Node.last.level + end + + test "create update node" do + login_as :quentin + #difference of three because "updates" and "2009" node get created as well + assert_difference "Node.count", +3 do + post( + :create, + :kind => "update", + :title => "Hello Spaceboy" + ) + end + + assert_response :redirect + expected = "updates/#{Time.now.year.to_s}/hello-spaceboy" + assert_equal expected, Node.last.unique_name + assert_equal 3, Node.last.level + end + + test "create top level node" do + login_as :quentin + + assert_difference "Node.count", +1 do + post( + :create, + :kind => "top_level", + :title => "Hello Spaceboy" + ) + end + + assert_response :redirect + expected = "hello-spaceboy" + assert_equal expected, Node.last.unique_name + assert_equal 1, Node.last.level end def test_editing_a_node -- cgit v1.3