diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 6765c5e7..a6a7480f 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -7,6 +7,8 @@ class ApplicationController < ActionController::Base # implements Instiki's legacy URLs require 'url_rewriting_hack' + after_filter :remember_location + # For injecting a different wiki model implementation. Intended for use in tests def self.wiki=(the_wiki) # a global variable is used here because Rails reloads controller and model classes in the @@ -15,13 +17,37 @@ class ApplicationController < ActionController::Base $instiki_wiki_service = the_wiki logger.debug("Wiki service: #{the_wiki.to_s}") end - + def self.wiki $instiki_wiki_service end + + protected def wiki $instiki_wiki_service end + + @@REMEMBER_NOT = [] + def remember_location + if @response.headers['Status'] == '200 OK' + @session[:return_to] = url_for unless @@REMEMBER_NOT.include? action_name + @session[:already_tried_index_as_fallback] = false + end + end + + def return_to_last_remembered + # Forget the redirect location + redirect_target, @session[:return_to] = @session[:return_to], nil + # then try to redirect to it + if redirect_target.nil? + raise 'Cannot redirect to index' if @session[:already_tried_index_as_fallback] + @session[:already_tried_index_as_fallback] = true + redirect_to_url '/' + else + redirect_to_url(redirect_target) + end + end + end diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index b7018133..b641797c 100755 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -213,21 +213,26 @@ class WikiController < ApplicationController def save redirect_to :action => 'index' if @page_name.nil? - - if @web.pages[@page_name] - page = wiki.revise_page( - @web_name, @page_name, @params['content'], Time.now, - Author.new(@params['author'], remote_ip) - ) - page.unlock - else - page = wiki.write_page( - @web_name, @page_name, @params['content'], Time.now, - Author.new(@params['author'], remote_ip) - ) - end cookies['author'] = @params['author'] - redirect_show(@page_name) + + begin + if @web.pages[@page_name] + page = wiki.revise_page( + @web_name, @page_name, @params['content'], Time.now, + Author.new(@params['author'], remote_ip) + ) + page.unlock + else + page = wiki.write_page( + @web_name, @page_name, @params['content'], Time.now, + Author.new(@params['author'], remote_ip) + ) + end + redirect_show(@page_name) + rescue Instiki::ValidationError => e + flash[:error] = e + return_to_last_remembered + end end def show diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 11267754..2a11daeb 100755 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -543,13 +543,17 @@ class WikiControllerTest < Test::Unit::TestCase def test_save_new_revision_identical_to_last revisions_before = @home.revisions.size - assert_raise(Instiki::ValidationError) { - process 'save', 'web' => 'wiki1', 'id' => 'HomePage', + r = process 'save', {'web' => 'wiki1', 'id' => 'HomePage', 'content' => @home.revisions.last.content.dup, - 'author' => 'SomeOtherAuthor' - } + 'author' => 'SomeOtherAuthor'}, {:return_to => '/wiki1/show/HomePage'} + + assert_redirect_url '/wiki1/show/HomePage' + assert_flash_has :error + assert r.flash[:error].kind_of?(Instiki::ValidationError) + revisions_after = @home.revisions.size assert_equal revisions_before, revisions_after + end