Skip to content
Browse files

Finish custom handling [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4409 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 180edf4 commit 6dea52c54e272b4592d1d157c8626003b03fcac1 @dhh dhh committed Jun 2, 2006
View
18 actionpack/lib/action_controller/mime_responds.rb
@@ -94,7 +94,7 @@ module InstanceMethods
# 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 @@ def custom(mime_type, &block)
@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
View
16 actionpack/test/controller/mime_responds_test.rb
@@ -61,6 +61,17 @@ def custom_type_handling
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 @@ def test_xhr
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

0 comments on commit 6dea52c

Please sign in to comment.
Something went wrong with that request. Please try again.