Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (105 sloc) 3.74 kb
49608f7 @strzalek Move protected_instance_variables & view_assigns to AbstractController
strzalek authored
1 require 'active_support/concern'
2 require 'active_support/core_ext/class/attribute'
47860b6 @fphilipe Require action_view to fix missing constant
fphilipe authored
3 require 'action_view'
d99dd0a @strzalek Require action_view explicitly in AC::Base
strzalek authored
4 require 'action_view/view_paths'
267e5c8 @tenderlove calculate the ivars to remove in advance as a set and cache them in a
tenderlove authored
5 require 'set'
49608f7 @strzalek Move protected_instance_variables & view_assigns to AbstractController
strzalek authored
6
b1f078b @wycats First, very early, AbstractController code. More to come
wycats authored
7 module AbstractController
4b4e517 @josh Relocate AbstractController exceptions into their proper parent modules
josh authored
8 class DoubleRenderError < Error
9 DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"."
10
11 def initialize(message = nil)
12 super(message || DEFAULT_MESSAGE)
13 end
14 end
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
15
16 module Rendering
d6363aa @strzalek Code formatting & typo fixes
strzalek authored
17 extend ActiveSupport::Concern
d8888b9 @strzalek Retain ActionPack dependency on ActionView
strzalek authored
18 include ActionView::ViewPaths
49608f7 @strzalek Move protected_instance_variables & view_assigns to AbstractController
strzalek authored
19
9b9ec0d @robin850 Fix a few typos [ci skip]
robin850 authored
20 # Normalizes arguments, options and then delegates render_to_body and
bea61d6 @claudiob Better docs for AbstractController
claudiob authored
21 # sticks the result in <tt>self.response_body</tt>.
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
22 # :api: public
23 def render(*args, &block)
a416695 @josevalim Remove BasicRendering and remove template functionality from AbsC::Re…
josevalim authored
24 options = _normalize_render(*args, &block)
25 self.response_body = render_to_body(options)
103e18c @sikachu Introduce `render :body` for render raw content
sikachu authored
26 _process_format(rendered_format, options) if rendered_format
67336ce @josevalim Remove remaining coupling with AV in MimeResponds
josevalim authored
27 self.response_body
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
28 end
29
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
30 # Raw rendering of a template to a string.
31 #
32 # It is similar to render, except that it does not
bea61d6 @claudiob Better docs for AbstractController
claudiob authored
33 # set the +response_body+ and it should be guaranteed
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
34 # to always return a string.
35 #
bea61d6 @claudiob Better docs for AbstractController
claudiob authored
36 # If a component extends the semantics of +response_body+
37 # (as ActionController extends it to be anything that
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
38 # responds to the method each), this method needs to be
39 # overridden in order to still return a string.
40 # :api: plugin
41 def render_to_string(*args, &block)
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
42 options = _normalize_render(*args, &block)
43 render_to_body(options)
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
44 end
45
a416695 @josevalim Remove BasicRendering and remove template functionality from AbsC::Re…
josevalim authored
46 # Performs the actual template rendering.
1656f58 @strzalek Improve AV::Rendering docs
strzalek authored
47 # :api: public
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
48 def render_to_body(options = {})
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
49 end
50
b1b9a0a @laurocaetano Typos. return -> returns. [ci skip]
laurocaetano authored
51 # Returns Content-Type of rendered content
7d81004 @strzalek Add #rendered_format method to controllers
strzalek authored
52 # :api: public
53 def rendered_format
d35cf4b @strzalek Make Mime::TEXT default format in AbstractController
strzalek authored
54 Mime::TEXT
7d81004 @strzalek Add #rendered_format method to controllers
strzalek authored
55 end
56
c8b566d @tenderlove use a set and reject to avoid array allocations
tenderlove authored
57 DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %w(
544d0fa @strzalek Return to using protected_instance_variables in AV
strzalek authored
58 @_action_name @_response_body @_formats @_prefixes @_config
59 @_view_context_class @_view_renderer @_lookup_context
697acc4 @tenderlove these variables are also private
tenderlove authored
60 @_routes @_db_runtime
32e94a4 @tenderlove instance_variables returns symbols, so we should use symbols in our list
tenderlove authored
61 ).map(&:to_sym)
544d0fa @strzalek Return to using protected_instance_variables in AV
strzalek authored
62
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
63 # This method should return a hash with assigns.
64 # You can overwrite this configuration per controller.
65 # :api: public
66 def view_assigns
c8b566d @tenderlove use a set and reject to avoid array allocations
tenderlove authored
67 protected_vars = _protected_ivars
68 variables = instance_variables
69
70 variables.reject! { |s| protected_vars.include? s }
779cd6e @tenderlove each_with_object on the view_assigns hash
tenderlove authored
71 variables.each_with_object({}) { |name, hash|
9a4adb4 @tenderlove use slice to avoid range allocation
tenderlove authored
72 hash[name.slice(1, name.length)] = instance_variable_get(name)
73 }
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
74 end
75
bea61d6 @claudiob Better docs for AbstractController
claudiob authored
76 # Normalize args by converting <tt>render "foo"</tt> to
77 # <tt>render :action => "foo"</tt> and <tt>render "foo/bar"</tt> to
78 # <tt>render :file => "foo/bar"</tt>.
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
79 # :api: plugin
80 def _normalize_args(action=nil, options={})
a46fa8d @spastorino Make AC standalone rendering work
spastorino authored
81 if action.is_a? Hash
82 action
83 else
84 options
85 end
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
86 end
87
88 # Normalize options.
89 # :api: plugin
90 def _normalize_options(options)
91 options
92 end
93
94 # Process extra options.
95 # :api: plugin
96 def _process_options(options)
97 options
98 end
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
99
67336ce @josevalim Remove remaining coupling with AV in MimeResponds
josevalim authored
100 # Process the rendered format.
101 # :api: private
103e18c @sikachu Introduce `render :body` for render raw content
sikachu authored
102 def _process_format(format, options = {})
67336ce @josevalim Remove remaining coupling with AV in MimeResponds
josevalim authored
103 end
104
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
105 # Normalize args and options.
106 # :api: private
107 def _normalize_render(*args, &block)
108 options = _normalize_args(*args, &block)
2d3a6a0 @strzalek Action Pack Variants
strzalek authored
109 #TODO: remove defined? when we restore AP <=> AV dependency
f6d9b68 @joankaradimov Check if the `request` variable isn't nil when calling render_to_string
joankaradimov authored
110 if defined?(request) && request && request.variant.present?
111 options[:variant] = request.variant
112 end
aea02eb @strzalek Move skeleton methods from AV to AbsC
strzalek authored
113 _normalize_options(options)
114 options
115 end
c8b566d @tenderlove use a set and reject to avoid array allocations
tenderlove authored
116
117 def _protected_ivars # :nodoc:
267e5c8 @tenderlove calculate the ivars to remove in advance as a set and cache them in a
tenderlove authored
118 DEFAULT_PROTECTED_INSTANCE_VARIABLES
c8b566d @tenderlove use a set and reject to avoid array allocations
tenderlove authored
119 end
8e3413d @strzalek Create AbstractController::Rendering interface
strzalek authored
120 end
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
121 end
Something went wrong with that request. Please try again.