mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Finish custom handling [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4409 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
180edf4146
commit
6dea52c54e
2 changed files with 28 additions and 6 deletions
|
@ -94,7 +94,7 @@ module ActionController #:nodoc:
|
|||
# and accept Rails' defaults, life will be much easier.
|
||||
def respond_to(*types, &block)
|
||||
raise ArgumentError, "respond_to takes either types or a block, never bot" unless types.any? ^ block
|
||||
block ||= lambda { |responder| types.each { |type| responder.known(type) } }
|
||||
block ||= lambda { |responder| types.each { |type| responder.send(type) } }
|
||||
responder = Responder.new(block.binding)
|
||||
block.call(responder)
|
||||
responder.respond
|
||||
|
@ -131,13 +131,19 @@ module ActionController #:nodoc:
|
|||
@responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding)
|
||||
end
|
||||
end
|
||||
|
||||
def known(mime_type_extension, &block)
|
||||
custom(Mime.const_get(mime_type_extension.to_s.upcase), &block)
|
||||
end
|
||||
|
||||
def any(*args, &block)
|
||||
args.each { |type| known(type, &block) }
|
||||
args.each { |type| send(type, &block) }
|
||||
end
|
||||
|
||||
def method_missing(symbol, &block)
|
||||
mime_constant = symbol.to_s.upcase
|
||||
|
||||
if Mime::SET.include?(Mime.const_get(mime_constant))
|
||||
custom(Mime.const_get(mime_constant), &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond
|
||||
|
|
|
@ -61,6 +61,17 @@ class RespondToController < ActionController::Base
|
|||
type.all { render :text => "Nothing" }
|
||||
end
|
||||
end
|
||||
|
||||
def custom_constant_handling
|
||||
Mime::Type.register("text/x-mobile", :mobile)
|
||||
|
||||
respond_to do |type|
|
||||
type.html { render :text => "HTML" }
|
||||
type.mobile { render :text => "Mobile" }
|
||||
end
|
||||
|
||||
Mime.send :remove_const, :MOBILE
|
||||
end
|
||||
|
||||
def handle_any
|
||||
respond_to do |type|
|
||||
|
@ -255,6 +266,11 @@ class MimeControllerTest < Test::Unit::TestCase
|
|||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
end
|
||||
|
||||
def test_custom_constant
|
||||
get :custom_constant_handling, :format => "mobile"
|
||||
assert_equal "Mobile", @response.body
|
||||
end
|
||||
|
||||
def test_forced_format
|
||||
get :html_xml_or_rss
|
||||
assert_equal "HTML", @response.body
|
||||
|
|
Loading…
Reference in a new issue