Skip to content

Commit

Permalink
Remove BasicRendering and remove template functionality from AbsC::Re…
Browse files Browse the repository at this point in the history
…ndering
  • Loading branch information
José Valim committed Sep 9, 2013
1 parent ff8fac6 commit a416695
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 47 deletions.
5 changes: 0 additions & 5 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
* Introduce `BasicRendering` which is the most basic rendering implementation. It
allows to `render :text` and `render :nothing` without depending on Action View.

*Łukasz Strzałkowski*

* Separate Action View completely from Action Pack.

*Łukasz Strzałkowski*
Expand Down
43 changes: 12 additions & 31 deletions actionpack/lib/abstract_controller/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def initialize(message = nil)
end
end

class UnsupportedOperationError < Error
DEFAULT_MESSAGE = "Unsupported render operation. BasicRendering supports only :text and :nothing options. For more, you need to include ActionView."

def initialize
super DEFAULT_MESSAGE
end
end

module Rendering
extend ActiveSupport::Concern

Expand All @@ -22,6 +30,8 @@ module Rendering
# sticks the result in self.response_body.
# :api: public
def render(*args, &block)
options = _normalize_render(*args, &block)
self.response_body = render_to_body(options)
end

# Raw rendering of a template to a string.
Expand All @@ -40,11 +50,10 @@ def render_to_string(*args, &block)
render_to_body(options)
end

# Raw rendering of a template.
# Performs the actual template rendering.
# :api: public
def render_to_body(options = {})
_process_options(options)
_render_template(options)
raise UnsupportedOperationError
end

# Return Content-Type of rendered content
Expand Down Expand Up @@ -97,32 +106,4 @@ def _normalize_render(*args, &block)
options
end
end

# 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

# 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

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
end
3 changes: 1 addition & 2 deletions actionpack/lib/action_controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module ActionController
# The <tt>metal</tt> anonymous class was introduced to solve issue with including modules in <tt>ActionController::Base</tt>.
# Modules needes to be included in particluar order. First wee need to have <tt>AbstractController::Rendering</tt> included,
# Modules needes to be included in particluar order. First we need to have <tt>AbstractController::Rendering</tt> included,
# next we should include actuall implementation which would be for example <tt>ActionView::Rendering</tt> and after that
# <tt>ActionController::Rendering</tt>. This order must be preserved and as we want to have middle module included dynamicaly
# <tt>metal</tt> class was introduced. It has <tt>AbstractController::Rendering</tt> included and is parent class of
Expand All @@ -14,7 +14,6 @@ module ActionController
#
metal = Class.new(Metal) do
include AbstractController::Rendering
include AbstractController::BasicRendering
end

# Action Controllers are the core of a web request in \Rails. They are made up of one or more actions that are executed
Expand Down
8 changes: 6 additions & 2 deletions actionpack/lib/action_controller/metal/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ def render_to_string(*)
end
end

def render_to_body(*)
super || " "
def render_to_body(options = {})
super || if options[:text].present?
options[:text]
else
" "
end
end

private
Expand Down
12 changes: 5 additions & 7 deletions actionview/lib/action_view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,18 @@ def view_renderer
@_view_renderer ||= ActionView::Renderer.new(lookup_context)
end

# Render template to response_body
# :api: public
def render(*args, &block)
options = _normalize_render(*args, &block)
self.response_body = render_to_body(options)
end

# Find and renders a template based on the options given.
# :api: private
def _render_template(options) #:nodoc:
lookup_context.rendered_format = nil if options[:formats]
view_renderer.render(view_context, options)
end

def render_to_body(options = {})
_process_options(options)
_render_template(options)
end

def rendered_format
Mime[lookup_context.rendered_format]
end
Expand Down

0 comments on commit a416695

Please sign in to comment.