Move Files When Renaming Web

Renaming a web should move the
corresponding subdirectory of webs/ .
Otherwise, links to uploaded files break.
This commit is contained in:
Jacques Distler 2009-12-26 23:43:18 -06:00
parent a705709f9a
commit 601331b508
4 changed files with 31 additions and 6 deletions

View file

@ -27,14 +27,28 @@ class Wiki
if not (web = Web.find_by_address(old_address))
raise Instiki::ValidationError.new("Web with address '#{old_address}' does not exist")
end
old_files_path = web.files_path
web.update_attributes(:address => new_address, :name => name, :markup => markup, :color => color,
:additional_style => additional_style, :safe_mode => safe_mode, :password => password, :published => published,
:brackets_only => brackets_only, :count_pages => count_pages, :allow_uploads => allow_uploads, :max_upload_size => max_upload_size)
@webs = nil
raise Instiki::ValidationError.new("There is already a web with address '#{new_address}'") unless web.errors.on(:address).nil?
web
move_files(old_files_path, web.files_path)
end
def move_files(old_path, new_path)
return if new_path == old_path
default_path = Rails.root.join("webs", "files")
FileUtils.rmdir(new_path)
if [old_path, new_path].include? default_path
File.rename(old_path, new_path)
FileUtils.rmdir(old_path.parent) unless old_path == default_path
else
File.rename(old_path.parent, new_path.parent)
end
end
def read_page(web_address, page_name)
ApplicationController.logger.debug "Reading page '#{page_name}' from web '#{web_address}'"

View file

@ -71,7 +71,8 @@
Stylesheet tweaks &gt;&gt;</a>
<em>
- add or change styles used by this web; styles defined here take precedence over
instiki.css. Hint: View HTML source of a page you want to style to find ID names on individual
instiki.css.<br/>
Hint: View HTML source of a page you want to style to find ID names on individual
tags.</em>
<br/>
<textarea id="additionalStyle" class="disableAutoComplete" cols="50" rows="20"
@ -96,7 +97,7 @@
<div class="help">
You can turn on a read-only version of this web that's accessible even when the regular web
is password protected.
The published version is accessible through URLs like /wiki/published/HomePage.
The published version is accessible through URLs like /<%= @web.address %>/published/HomePage.
</div>
<div class="inputBox">
<input type="checkbox" id="published" name="published" class="disableAutoComplete" <%= 'checked="checked"' if @web.published? %> />

View file

@ -25,6 +25,7 @@ class AdminControllerTest < ActionController::TestCase
@elephant = pages(:elephant)
@web = webs(:test_wiki)
@home = @page = pages(:home_page)
FileUtils.rm_rf("#{RAILS_ROOT}/webs/renamed_wiki1")
end
def test_create_system_form_displayed
@ -116,7 +117,8 @@ class AdminControllerTest < ActionController::TestCase
def test_edit_web_form_submitted
@wiki.system.update_attribute(:password, 'pswd')
@web.save
process('edit_web', 'system_password' => 'pswd',
'web' => 'wiki1', 'address' => 'renamed_wiki1', 'name' => 'Renamed Wiki1',
'markup' => 'markdown', 'color' => 'blue', 'additional_style' => 'whatever',
@ -137,11 +139,14 @@ class AdminControllerTest < ActionController::TestCase
assert @web.count_pages?
assert @web.allow_uploads?
assert_equal 300, @web.max_upload_size
assert File.directory? Rails.root.join("webs", "renamed_wiki1", "files")
assert !File.exist?(Rails.root.join("webs", "wiki1"))
end
def test_edit_web_web_password_mismatch
@wiki.system.update_attribute(:password, 'pswd')
@web.save
process('edit_web', 'system_password' => 'pswd',
'web' => 'wiki1', 'address' => 'renamed_wiki1', 'name' => 'Renamed Wiki1',
'markup' => 'markdown', 'color' => 'blue', 'additional_style' => 'whatever',
@ -151,10 +156,13 @@ class AdminControllerTest < ActionController::TestCase
assert_response :success
assert @response.has_template_object?('error')
assert File.directory? Rails.root.join("webs", "wiki1", "files")
assert !File.exist?(Rails.root.join("webs", "renamed_wiki1"))
end
def test_edit_web_opposite_values
@wiki.system.update_attribute(:password, 'pswd')
@web.save
process('edit_web', 'system_password' => 'pswd',
'web' => 'wiki1', 'address' => 'renamed_wiki1', 'name' => 'Renamed Wiki1',
@ -170,6 +178,8 @@ class AdminControllerTest < ActionController::TestCase
assert !@web.brackets_only?
assert !@web.count_pages?
assert !@web.allow_uploads?
assert File.directory? Rails.root.join("webs", "renamed_wiki1", "files")
assert !File.exist?(Rails.root.join("webs", "wiki1"))
end
def test_edit_web_wrong_password

View file

@ -24,7 +24,7 @@ class FileControllerTest < ActionController::TestCase
@wiki = Wiki.new
WikiFile.delete_all
require 'fileutils'
FileUtils.rm_rf("#{RAILS_ROOT}/webs/wiki1/files")
FileUtils.rm_rf("#{RAILS_ROOT}/webs/wiki1")
end
def test_file_upload_form