Created
September 23, 2008 20:24
-
-
Save jackdempsey/12385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/merb-core/controller/mixins/controller.rb b/lib/merb-core/controller/mixins/controller.rb | |
index 8c8078c..2bb0f74 100644 | |
--- a/lib/merb-core/controller/mixins/controller.rb | |
+++ b/lib/merb-core/controller/mixins/controller.rb | |
@@ -127,9 +127,15 @@ module Merb | |
def redirect(url, opts = {}) | |
default_redirect_options = { :message => nil, :permanent => false } | |
opts = default_redirect_options.merge(opts) | |
- if opts[:message] | |
- notice = Merb::Request.escape([Marshal.dump(opts[:message])].pack("m")) | |
- url = url =~ /\?/ ? "#{url}&_message=#{notice}" : "#{url}?_message=#{notice}" | |
+ if message_set?(opts) | |
+ [:notice, :error].each do |message_key| | |
+ if opts[message_key] | |
+ opts[:message] ||= {} | |
+ opts[:message][message_key] = opts[message_key] | |
+ end | |
+ end | |
+ message = Merb::Request.escape([Marshal.dump(opts[:message])].pack("m")) | |
+ url = url =~ /\?/ ? "#{url}&_message=#{message}" : "#{url}?_message=#{message}" | |
end | |
self.status = opts[:permanent] ? 301 : 302 | |
Merb.logger.info("Redirecting to: #{url} (#{self.status})") | |
@@ -315,5 +321,9 @@ module Merb | |
raise(Merb::ControllerExceptions::NotImplemented, "Current Rack adapter does not support streaming") | |
end | |
end | |
+ | |
+ def message_set?(opts) | |
+ opts[:message] || opts[:notice] || opts[:error] | |
+ end | |
end | |
end | |
diff --git a/spec/public/request/redirect_spec.rb b/spec/public/request/redirect_spec.rb | |
index 65b65eb..75dbe35 100644 | |
--- a/spec/public/request/redirect_spec.rb | |
+++ b/spec/public/request/redirect_spec.rb | |
@@ -22,4 +22,20 @@ describe Merb::Request, " redirects" do | |
expected_url = Merb::Request.escape([Marshal.dump(:notice => "what?")].pack("m")) | |
result.headers["Location"].should == "/?_message=#{expected_url}" | |
end | |
+ | |
+ it "allows for short style using :notice" do | |
+ @request = Merb::Request.new({}) | |
+ result = @request.redirect("/", :notice => "what?" ) | |
+ result.status.should == 302 | |
+ expected_url = Merb::Request.escape([Marshal.dump(:notice => "what?")].pack("m")) | |
+ result.headers["Location"].should == "/?_message=#{expected_url}" | |
+ end | |
+ | |
+ it "allows for short style using :error" do | |
+ @request = Merb::Request.new({}) | |
+ result = @request.redirect("/", :error => "what?" ) | |
+ result.status.should == 302 | |
+ expected_url = Merb::Request.escape([Marshal.dump(:error => "what?")].pack("m")) | |
+ result.headers["Location"].should == "/?_message=#{expected_url}" | |
+ end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment