From 760d9c16404573503e9414256c30d99ec86248f9 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 2 Aug 2010 15:39:04 -0700 Subject: [PATCH] Wiki#update_page takes a name parameter for renames --- README.md | 2 +- lib/gollum/frontend/app.rb | 2 +- lib/gollum/wiki.rb | 17 +++++++++-------- test/test_wiki.rb | 37 ++++++++++++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 532d9de3..30806a55 100644 --- a/README.md +++ b/README.md @@ -406,7 +406,7 @@ Update an existing page. If the format is different than the page's current format, the file name will be changed to reflect the new format. page = wiki.page('Page Name') - wiki.update_page(page, page.format, 'Page contents', commit) + wiki.update_page(page, page.name, page.format, 'Page contents', commit) To delete a page and commit the change: diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index 65c3bbce..3724fd0e 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -56,7 +56,7 @@ module Precious page = wiki.page(name) format = params[:format].intern - wiki.update_page(page, format, params[:content], commit_message) + wiki.update_page(page, page.name, format, params[:content], commit_message) redirect "/#{name}" end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 431411f8..09df6bd1 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -95,7 +95,7 @@ module Gollum # Returns the in-memory Gollum::Page. def preview_page(name, data, format) page = @page_class.new(self) - ext = @page_class.format_to_ext(format.to_sym) + ext = @page_class.format_to_ext(format.to_sym) path = @page_class.cname(name) + '.' + ext blob = OpenStruct.new(:name => path, :data => data) page.populate(blob, path) @@ -134,6 +134,7 @@ module Gollum # changed to reflect the new format. # # page - The Gollum::Page to update. + # name - The String extension-less name of the page. # format - The Symbol format of the page. # data - The new String contents of the page. # commit - The commit Hash details: @@ -142,19 +143,19 @@ module Gollum # :email - The String email address. # # Returns the String SHA1 of the newly written version. - def update_page(page, format, data, commit = {}) - pcommit = @repo.commit('master') - map = tree_map(pcommit.tree) + def update_page(page, name, format, data, commit = {}) + pcommit = @repo.commit('master') + map = tree_map(pcommit.tree) + name ||= page.name + format ||= page.format + index = nil - index = nil - - if page.format == format + if page.name == name && page.format == format index = tree_map_to_index(map) index.add(page.path, normalize(data)) else map = delete_from_tree_map(map, page.path) dir = ::File.dirname(page.path) - name = page.name map = add_to_tree_map(map, dir, name, format, data) index = tree_map_to_index(map) end diff --git a/test/test_wiki.rb b/test/test_wiki.rb index a373e6f0..bdd64416 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -87,7 +87,7 @@ context "Wiki page writing" do @wiki.write_page("Gollum", :markdown, "# Gollum", commit) page = @wiki.page("Gollum") - @wiki.update_page(page, :markdown, "# Gollum2", commit) + @wiki.update_page(page, page.name, :markdown, "# Gollum2", commit) assert_equal 2, @wiki.repo.commits.size assert_equal "# Gollum2", @wiki.page("Gollum").raw_data @@ -105,13 +105,44 @@ context "Wiki page writing" do assert_equal :markdown, @wiki.page("Gollum").format page = @wiki.page("Gollum") - @wiki.update_page(page, :textile, "h1. Gollum", commit) + @wiki.update_page(page, page.name, :textile, "h1. Gollum", commit) assert_equal 2, @wiki.repo.commits.size assert_equal :textile, @wiki.page("Gollum").format assert_equal "h1. Gollum", @wiki.page("Gollum").raw_data end + test "update page with name change" do + commit = { :message => "Gollum page", + :name => "Tom Preston-Werner", + :email => "tom@github.com" } + @wiki.write_page("Gollum", :markdown, "# Gollum", commit) + + assert_equal :markdown, @wiki.page("Gollum").format + + page = @wiki.page("Gollum") + @wiki.update_page(page, 'Smeagol', :markdown, "h1. Gollum", commit) + + assert_equal 2, @wiki.repo.commits.size + assert_equal "h1. Gollum", @wiki.page("Smeagol").raw_data + end + + test "update page with name and format change" do + commit = { :message => "Gollum page", + :name => "Tom Preston-Werner", + :email => "tom@github.com" } + @wiki.write_page("Gollum", :markdown, "# Gollum", commit) + + assert_equal :markdown, @wiki.page("Gollum").format + + page = @wiki.page("Gollum") + @wiki.update_page(page, 'Smeagol', :textile, "h1. Gollum", commit) + + assert_equal 2, @wiki.repo.commits.size + assert_equal :textile, @wiki.page("Smeagol").format + assert_equal "h1. Gollum", @wiki.page("Smeagol").raw_data + end + test "update nested page with format change" do commit = { :message => "Gollum page", :name => "Tom Preston-Werner", @@ -123,7 +154,7 @@ context "Wiki page writing" do page = @wiki.page("Gollum") assert_equal :markdown, @wiki.page("Gollum").format - @wiki.update_page(page, :textile, "h1. Gollum", commit) + @wiki.update_page(page, page.name, :textile, "h1. Gollum", commit) page = @wiki.page("Gollum") assert_equal "lotr/Gollum.textile", page.path