diff options
author | Zuhao Wan <wanzuhao@gmail.com> | 2014-05-18 16:54:20 +0800 |
---|---|---|
committer | Zuhao Wan <wanzuhao@gmail.com> | 2014-05-20 02:00:57 +0800 |
commit | ac36b45672513417d59ab8960b49ff1f8707d352 (patch) | |
tree | b3cbde2137b05bde5084d7ca23143a13a5584f2d | |
parent | dbbcc8388acbcded4ef3a8b17213e3f16a354f32 (diff) | |
download | rails-ac36b45672513417d59ab8960b49ff1f8707d352.tar.gz rails-ac36b45672513417d59ab8960b49ff1f8707d352.tar.bz2 rails-ac36b45672513417d59ab8960b49ff1f8707d352.zip |
Add ActionController::Renderers.remove.
-rw-r--r-- | actionpack/lib/action_controller/metal/renderers.rb | 16 | ||||
-rw-r--r-- | actionpack/test/controller/mime/respond_with_test.rb | 19 |
2 files changed, 35 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index 0443b73953..29ce5abd55 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -6,6 +6,11 @@ module ActionController Renderers.add(key, &block) end + # See <tt>Renderers.remove</tt> + def self.remove_renderer(key) + Renderers.remove(key) + end + class MissingRenderer < LoadError def initialize(format) super "No renderer defined for format: #{format}" @@ -83,6 +88,17 @@ module ActionController RENDERERS << key.to_sym end + # This method is the opposite of add method. + # + # Usage: + # + # ActionController::Renderers.remove(:csv) + def self.remove(key) + RENDERERS.delete(key.to_sym) + method = "_render_option_#{key}" + remove_method(method) if method_defined?(method) + end + module All extend ActiveSupport::Concern include Renderers diff --git a/actionpack/test/controller/mime/respond_with_test.rb b/actionpack/test/controller/mime/respond_with_test.rb index 416b3b81a5..742225fec5 100644 --- a/actionpack/test/controller/mime/respond_with_test.rb +++ b/actionpack/test/controller/mime/respond_with_test.rb @@ -643,6 +643,8 @@ class RespondWithControllerTest < ActionController::TestCase get :index, format: 'csv' assert_equal Mime::CSV, @response.content_type assert_equal "c,s,v", @response.body + ensure + ActionController::Renderers.remove :csv end def test_raises_missing_renderer_if_an_api_behavior_with_no_renderer @@ -652,6 +654,23 @@ class RespondWithControllerTest < ActionController::TestCase end end + def test_removing_renderers + ActionController::Renderers.add :csv do |obj, options| + send_data obj.to_csv, type: Mime::CSV + end + @controller = CsvRespondWithController.new + @request.accept = "text/csv" + get :index, format: 'csv' + assert_equal Mime::CSV, @response.content_type + + ActionController::Renderers.remove :csv + assert_raise ActionController::MissingRenderer do + get :index, format: 'csv' + end + ensure + ActionController::Renderers.remove :csv + end + def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called @controller = EmptyRespondWithController.new @request.accept = "*/*" |