From f7ce8c245fde3f6202103ae614b181c0ba44ed93 Mon Sep 17 00:00:00 2001 From: hukl Date: Wed, 9 Sep 2009 15:19:33 +0200 Subject: added editing of slugs and moving of nodes. to edit a slug or move a node you edit that node, make your changes and save. the slug and parent node changes get staged and are applied once that revision is published --- app/controllers/nodes_controller.rb | 1 + app/models/node.rb | 9 +++ app/views/nodes/edit.html.erb | 22 ++++++- ...20090909113832_add_staged_slug_and_parent_id.rb | 11 ++++ public/javascripts/admin_interface.js | 1 + public/javascripts/admin_search.js | 51 ++++++++++++++++ public/stylesheets/admin.css | 9 +++ test/functional/nodes_controller_test.rb | 70 ++++++++++++++++++++++ 8 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20090909113832_add_staged_slug_and_parent_id.rb diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index a23354c..0d4a3fe 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -57,6 +57,7 @@ class NodesController < ApplicationController end def update + @node.update_attributes(params[:node]) @draft = @node.find_or_create_draft current_user @draft.tag_list = params[:tag_list] if @draft.update_attributes( params[:page] ) diff --git a/app/models/node.rb b/app/models/node.rb index a870a3a..057248e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -87,6 +87,15 @@ class Node < ActiveRecord::Base self.head = self.draft self.head.save! self.draft = nil + + if staged_slug && (staged_slug != slug) + self.slug = staged_slug + end + + if staged_parent_id && (staged_parent_id != parent_id) + self.parent_id = staged_parent_id + end + self.save! self.unlock! else diff --git a/app/views/nodes/edit.html.erb b/app/views/nodes/edit.html.erb index e41321d..c23381f 100644 --- a/app/views/nodes/edit.html.erb +++ b/app/views/nodes/edit.html.erb @@ -18,7 +18,27 @@ Slug - <%= f.text_field :slug %> + + <%= + f.text_field( + :staged_slug, :value => @node.staged_slug || @node.slug + ) + %> + + + + parent + + <%= text_field_tag :move_to_search_term, @node.parent.title rescue "" %> +
+ +
+ <%= f.hidden_field( + :staged_parent_id, + :value => @node.staged_parent_id || @node.parent_id + ) + %> + <% fields_for @draft do |d| %> diff --git a/db/migrate/20090909113832_add_staged_slug_and_parent_id.rb b/db/migrate/20090909113832_add_staged_slug_and_parent_id.rb new file mode 100644 index 0000000..ab883ca --- /dev/null +++ b/db/migrate/20090909113832_add_staged_slug_and_parent_id.rb @@ -0,0 +1,11 @@ +class AddStagedSlugAndParentId < ActiveRecord::Migration + def self.up + add_column :nodes, :staged_slug, :string + add_column :nodes, :staged_parent_id, :integer + end + + def self.down + remove_column :nodes, :staged_slug + remove_column :nodes, :staged_parent_id + end +end diff --git a/public/javascripts/admin_interface.js b/public/javascripts/admin_interface.js index 95256ee..cf067f0 100644 --- a/public/javascripts/admin_interface.js +++ b/public/javascripts/admin_interface.js @@ -4,6 +4,7 @@ $(document).ready(function () { meta_data.initialize(); menu_item_sorter.initialize(); parent_search.initialize_search(); + move_to_search.initialize_search(); $(".with_editor").tinymce({ script_url : '/javascripts/tiny_mce/tiny_mce.js', diff --git a/public/javascripts/admin_search.js b/public/javascripts/admin_search.js index c08bdf3..6d3ca93 100644 --- a/public/javascripts/admin_search.js +++ b/public/javascripts/admin_search.js @@ -163,3 +163,54 @@ parent_search = { } } + +move_to_search = { + initialize_search : function() { + $("#move_to_search_term").bind("keyup", function() { + if ($(this).attr("value")) { + $.ajax({ + type: "GET", + url: "/admin/menu_search", + data: "search_term=" + $(this).attr("value"), + dataType: "json", + success : function(results) { + move_to_search.show_results(results); + } + }); + } + else { + $('#search_results').slideUp(); + $('#search_results').empty(); + } + }); + }, + + show_results : function(results) { + $("#search_results").empty(); + for (result in results) { + var link = $((""+ results[result].title + "")); + $(link).bind("click", move_to_search.link_closure(results[result])); + + + // Sometimes I don't get jquery; wrap() didn't work *sigh* + // Guess I'll need a book someday or another framework + var wrapper = $("
"); + $(wrapper).append(link) + + $("#search_results").append(wrapper); + + } + }, + + link_closure : function(node) { + var barf = function(){ + $("#move_to_search_term").attr("value", node.title); + $("#node_staged_parent_id").attr("value", node.node_id); + $('#search_results').slideUp(); + $('#search_results').empty(); + return false; + } + + return barf; + } +} \ No newline at end of file diff --git a/public/stylesheets/admin.css b/public/stylesheets/admin.css index 9fa503b..575ddff 100644 --- a/public/stylesheets/admin.css +++ b/public/stylesheets/admin.css @@ -416,3 +416,12 @@ div#image_browser { div#image_browser ul li { list-style-type: none; } + +input#move_to_search_term, input#node_staged_slug { + width: 690px; +} + +#tag_list { + width: 680px; +} + diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb index 7559869..08e50fd 100644 --- a/test/functional/nodes_controller_test.rb +++ b/test/functional/nodes_controller_test.rb @@ -127,4 +127,74 @@ class NodesControllerTest < ActionController::TestCase assert_equal "There", test_node.draft.body assert_equal "Foobar", test_node.draft.template_name end + + + test "publish draft with staged_slug unqueal slug" do + login_as :quentin + + test_node = Node.create! :slug => "test_node", :staged_slug => "peter_pan" + test_node.move_to_child_of Node.root + + put :publish, :id => test_node.id + + test_node.reload + assert_equal "peter_pan", test_node.slug + assert_equal "peter_pan", test_node.unique_name + end + + test "publish draft with staged_slug with more levels of nodes" do + login_as :quentin + + test_node = Node.create! :slug => "test_node", :staged_slug => "peter_pan" + test_node.move_to_child_of Node.root + test_node2 = Node.create! :slug => "test_node2" + test_node2.move_to_child_of test_node + + put :publish, :id => test_node.id + + test_node.reload; test_node2.reload + assert_equal "peter_pan/test_node2", test_node2.unique_name + assert_equal "peter_pan", test_node.unique_name + end + + test "publish draft with staged_parent_id" do + login_as :quentin + + parent = Node.create! :slug => "parent" + parent.move_to_child_of Node.root + test_node = Node.create! :slug => "test_node", :staged_parent_id => parent.id + test_node.move_to_child_of Node.root + test_node2 = Node.create! :slug => "test_node2" + test_node2.move_to_child_of test_node + + put :publish, :id => test_node.id + + test_node.reload; test_node2.reload + assert_equal "parent/test_node", test_node.unique_name + assert_equal "parent/test_node/test_node2", test_node2.unique_name + end + + test "publish draft with staged_parent_id and staged_slug" do + login_as :quentin + + parent = Node.create! :slug => "parent" + parent.move_to_child_of Node.root + + test_node = Node.create!( + :slug => "test_node", + :staged_parent_id => parent.id, + :staged_slug => "peter_pan" + ) + test_node.move_to_child_of Node.root + + test_node2 = Node.create! :slug => "test_node2" + test_node2.move_to_child_of test_node + + put :publish, :id => test_node.id + + test_node.reload; test_node2.reload + assert_equal "parent/peter_pan", test_node.unique_name + assert_equal "parent/peter_pan/test_node2", test_node2.unique_name + end + end -- cgit v1.3