aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal/rendering.rb
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2013-08-26 03:07:30 -0700
committerPiotr Sarnacki <drogus@gmail.com>2013-08-26 03:07:30 -0700
commitc19958015fac7b50f8dedbdf17991c00d6b8bbd4 (patch)
tree42431a33fd65c954b1242b8ff6116163c9969924 /actionpack/lib/action_controller/metal/rendering.rb
parent27dc4fa28ee098d70a11829ad5fa4af0c54e880b (diff)
parent0d43df7ebf7587f032e3b16c153bf35746878c46 (diff)
downloadrails-c19958015fac7b50f8dedbdf17991c00d6b8bbd4.tar.gz
rails-c19958015fac7b50f8dedbdf17991c00d6b8bbd4.tar.bz2
rails-c19958015fac7b50f8dedbdf17991c00d6b8bbd4.zip
Merge pull request #11396 from strzalek/extract_renderers
Remove dependency on Action View from Action Pack This set of changes removes the need of using Action View with Action Pack. Now you may use controllers without Action View, by rendering `:text` response or alternatively you can plug in your own rendering logic. This is especially handy when you're just dealing with APIs and don't need to include entire Action View just to render simple JSON responses.
Diffstat (limited to 'actionpack/lib/action_controller/metal/rendering.rb')
-rw-r--r--actionpack/lib/action_controller/metal/rendering.rb42
1 files changed, 36 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb
index bea6b88f91..21224b9c3b 100644
--- a/actionpack/lib/action_controller/metal/rendering.rb
+++ b/actionpack/lib/action_controller/metal/rendering.rb
@@ -1,8 +1,38 @@
module ActionController
- module Rendering
+ # Basic rendering implements the most minimal rendering layer.
+ # It only supports rendering :text and :nothing. Passing any other option will
+ # result in `UnsupportedOperationError` exception. For more functionality like
+ # different formats, layouts etc. you should use `ActionView` gem.
+ module BasicRendering
extend ActiveSupport::Concern
- include AbstractController::Rendering
+ # Render text or nothing (empty string) to response_body
+ # :api: public
+ def render(*args, &block)
+ super(*args, &block)
+ opts = args.first
+ if opts.has_key?(:text) && opts[:text].present?
+ self.response_body = opts[:text]
+ elsif opts.has_key?(:nothing) && opts[:nothing]
+ self.response_body = " "
+ else
+ raise UnsupportedOperationError
+ end
+ end
+
+ def rendered_format
+ Mime::TEXT
+ end
+
+ class UnsupportedOperationError < StandardError
+ def initialize
+ super "Unsupported render operation. BasicRendering supports only :text and :nothing options. For more, you need to include ActionView."
+ end
+ end
+ end
+
+ module Rendering
+ extend ActiveSupport::Concern
# Before processing, set the request formats in current controller formats.
def process_action(*) #:nodoc:
@@ -12,17 +42,17 @@ module ActionController
# Check for double render errors and set the content_type after rendering.
def render(*args) #:nodoc:
- raise ::AbstractController::DoubleRenderError if response_body
+ raise ::AbstractController::DoubleRenderError if self.response_body
super
- self.content_type ||= Mime[lookup_context.rendered_format].to_s
- response_body
+ self.content_type ||= rendered_format.to_s
+ self.response_body
end
# Overwrite render_to_string because body can now be set to a rack body.
def render_to_string(*)
if self.response_body = super
string = ""
- response_body.each { |r| string << r }
+ self.response_body.each { |r| string << r }
string
end
ensure