Skip to content

Commit

Permalink
Wiki#update_page takes a name parameter for renames
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Aug 2, 2010
1 parent 6609b2d commit 760d9c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion lib/gollum/frontend/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class App < Sinatra::Base
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
Expand Down
19 changes: 10 additions & 9 deletions lib/gollum/wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def file(name, version = 'master')
# 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)
Expand Down Expand Up @@ -134,6 +134,7 @@ def write_page(name, format, data, commit = {})
# 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:
Expand All @@ -142,19 +143,19 @@ def write_page(name, format, data, commit = {})
# :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)

index = nil

if page.format == format
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

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
Expand Down
37 changes: 34 additions & 3 deletions test/test_wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
@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
Expand All @@ -105,13 +105,44 @@
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",
Expand All @@ -123,7 +154,7 @@

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
Expand Down

0 comments on commit 760d9c1

Please sign in to comment.