Module: ActionController::Renderers
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::ActiveSupport::Concern
|
|
| Defined in: | actionpack/lib/action_controller/metal/renderers.rb |
Constant Summary
-
RENDERERS =
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 37ActiveSupport::Deprecation::DeprecatedObjectProxy.new( @all, "ActionController::Renderers::RENDERERS is deprecated. Use ActionController.add_renderer, or ActionController.remove_renderer " \ "to add or remove renderers. If you need to check which renderers exists for an application, you can instead use " \ "ActionController::Renderers.all", ActionController.deprecator, )
Class Attribute Summary
-
.all
readonly
Returns a Set of renderers name.
Class Method Summary
-
.add(key, &block)
Adds a new renderer to call within controller actions.
-
.remove(key)
This method is the opposite of add method.
- ._render_with_renderer_method_name(key) Internal use only
::ActiveSupport::Concern - Extended
| class_methods | Define class methods from given block. |
| included | Evaluate given block in context of base class, so that you can write class macros here. |
| prepended | Evaluate given block in context of base class, so that you can write class macros here. |
| append_features, prepend_features | |
Instance Method Summary
-
#render_to_body(options)
Called by
renderin::AbstractController::Renderingwhich sets the return value as theresponse_body. - #_all_renderers Internal use only
- #_render_to_body_with_renderer(options) Internal use only
DSL Calls
included
[ GitHub ]57 58 59 60 61 62
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 57
included do class_attribute :_renderers, default: Set.new.freeze class_attribute :escape_json_responses, instance_writer: false, instance_accessor: false, default: true singleton_class.prepend DeprecatedEscapeJsonResponses end
Class Attribute Details
.all (readonly)
Returns a Set of renderers name. By default, Action Controller adds the
json, js, xml, markdown and svg renderers.
You can use all to see what renderers exists for an application.
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 33
attr_reader :all # :doc:
Class Method Details
._render_with_renderer_method_name(key)
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 123
def self._render_with_renderer_method_name(key) # :nodoc: "_render_with_renderer_#{key}" end
.add(key, &block)
Adds a new renderer to call within controller actions. A renderer is invoked
by passing its name as an option to AbstractController::Rendering#render. To
create a renderer pass it a name and a block. The block takes two arguments,
the first is the value paired with its key and the second is the remaining
hash of options passed to render.
Create a csv renderer:
ActionController::Renderers.add :csv do |obj, |
filename = [:filename] || 'data'
str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
send_data str, type: Mime[:csv],
disposition: "attachment; filename=#{filename}.csv"
end
Note that we used Mime for the csv mime type as it comes with ::Rails.
For a custom renderer, you'll need to register a mime type with
Mime::Type.register.
To use the csv renderer in a controller action:
def show
@csvable = Csvable.find(params[:id])
respond_to do |format|
format.html
format.csv { render csv: @csvable, filename: @csvable.name }
end
end
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 103
def self.add(key, &block) define_method(_render_with_renderer_method_name(key), &block) (@all += [key.to_sym]).freeze RENDERERS.target = @all end
.remove(key)
This method is the opposite of add method.
To remove a csv renderer:
ActionController::Renderers.remove(:csv)
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 115
def self.remove(key) (@all -= [key.to_sym]).freeze method_name = _render_with_renderer_method_name(key) remove_possible_method(method_name) RENDERERS.target = @all end
Instance Method Details
#_all_renderers
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 188
def _all_renderers # :nodoc: _renderers end
#_render_to_body_with_renderer(options)
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 177
def _render_to_body_with_renderer() # :nodoc: _all_renderers.each do |name| if .key?(name) () method_name = Renderers._render_with_renderer_method_name(name) return send(method_name, .delete(name), ) end end nil end
#render_to_body(options)
Called by render in ::AbstractController::Rendering which sets the return
value as the response_body.
If no renderer is found, super returns control to
ActionView::Rendering.render_to_body, if present.
# File 'actionpack/lib/action_controller/metal/renderers.rb', line 173
def render_to_body() _render_to_body_with_renderer() || super end