Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (97 sloc) 3.224 kB
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
1 require 'set'
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
2
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
3 module ActionController
b3dad5d @cldwalker add some docs for ActionController::Renderers
cldwalker authored
4 # See <tt>Renderers.add</tt>
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
5 def self.add_renderer(key, &block)
36c13cc @dhh Rename RenderOptions to Renderers
dhh authored
6 Renderers.add(key, &block)
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
7 end
8
36c13cc @dhh Rename RenderOptions to Renderers
dhh authored
9 module Renderers
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored
10 extend ActiveSupport::Concern
de20324 @josh Revert "Revert "Whitespace!""
josh authored
11
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
12 included do
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
13 class_attribute :_renderers
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
14 self._renderers = Set.new.freeze
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
15 end
de20324 @josh Revert "Revert "Whitespace!""
josh authored
16
9d3d774 Update render options to remove performance implications of many rend…
Yehuda Katz + Carl Lerche authored
17 module ClassMethods
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
18 def use_renderers(*args)
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
19 renderers = _renderers + args
20 self._renderers = renderers.freeze
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
21 end
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
22 alias use_renderer use_renderers
9d3d774 Update render options to remove performance implications of many rend…
Yehuda Katz + Carl Lerche authored
23 end
de20324 @josh Revert "Revert "Whitespace!""
josh authored
24
9d3d774 Update render options to remove performance implications of many rend…
Yehuda Katz + Carl Lerche authored
25 def render_to_body(options)
26 _handle_render_options(options) || super
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
27 end
ad1c90d Added the :xml render option
Yehuda Katz + Carl Lerche authored
28
559c429 @spastorino Remove unneeded metaprogramming and method generation in favor of dir…
spastorino authored
29 def _handle_render_options(options)
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
30 _renderers.each do |name|
31 if options.key?(name)
559c429 @spastorino Remove unneeded metaprogramming and method generation in favor of dir…
spastorino authored
32 _process_options(options)
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
33 return send("_render_option_#{name}", options.delete(name), options)
559c429 @spastorino Remove unneeded metaprogramming and method generation in favor of dir…
spastorino authored
34 end
35 end
36 nil
37 end
38
b3dad5d @cldwalker add some docs for ActionController::Renderers
cldwalker authored
39 # Hash of available renderers, mapping a renderer name to its proc.
eea6689 @fxn removes support for render :update
fxn authored
40 # Default keys are :json, :js, :xml.
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
41 RENDERERS = Set.new
b3dad5d @cldwalker add some docs for ActionController::Renderers
cldwalker authored
42
43 # Adds a new renderer to call within controller actions.
44 # A renderer is invoked by passing its name as an option to
45 # <tt>AbstractController::Rendering#render</tt>. To create a renderer
46 # pass it a name and a block. The block takes two arguments, the first
47 # is the value paired with its key and the second is the remaining
48 # hash of options passed to +render+.
49 #
50 # Create a csv renderer:
51 #
52 # ActionController::Renderers.add :csv do |obj, options|
53 # filename = options[:filename] || 'data'
54 # str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
55 # send_data str, type: Mime::CSV,
56 # disposition: "attachment; filename=#{filename}.csv"
b3dad5d @cldwalker add some docs for ActionController::Renderers
cldwalker authored
57 # end
58 #
59 # Note that we used Mime::CSV for the csv mime type as it comes with Rails.
60 # For a custom renderer, you'll need to register a mime type with
61 # <tt>Mime::Type.register</tt>.
62 #
63 # To use the csv renderer in a controller action:
64 #
65 # def show
66 # @csvable = Csvable.find(params[:id])
67 # respond_to do |format|
68 # format.html
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
69 # format.csv { render csv: @csvable, filename: @csvable.name }
b3dad5d @cldwalker add some docs for ActionController::Renderers
cldwalker authored
70 # }
71 # end
72 # To use renderers and their mime types in more concise ways, see
73 # <tt>ActionController::MimeResponds::ClassMethods.respond_to</tt> and
74 # <tt>ActionController::MimeResponds#respond_with</tt>
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
75 def self.add(key, &block)
76 define_method("_render_option_#{key}", &block)
a4c04a4 @wycats Refactor to make renderers a Set
wycats authored
77 RENDERERS << key.to_sym
ad1c90d Added the :xml render option
Yehuda Katz + Carl Lerche authored
78 end
79
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
80 module All
81 extend ActiveSupport::Concern
36c13cc @dhh Rename RenderOptions to Renderers
dhh authored
82 include Renderers
de20324 @josh Revert "Revert "Whitespace!""
josh authored
83
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
84 included do
85 self._renderers = RENDERERS
de20324 @josh Revert "Revert "Whitespace!""
josh authored
86 end
7b3fb1d Got controller/render_js_test.rb to pass on new base
Yehuda Katz + Carl Lerche authored
87 end
88
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
89 add :json do |json, options|
fa2a5ae @neerajdotname If a user wants json output then try best to render json output. In s…
neerajdotname authored
90 json = json.to_json(options) unless json.kind_of?(String)
e73f547 @josevalim Avoid calling content type multiple times
josevalim authored
91
92 if options[:callback].present?
6b15a45 @omjokine Change mimetype to Mime::JS if JSONP is used
omjokine authored
93 self.content_type ||= Mime::JS
e73f547 @josevalim Avoid calling content type multiple times
josevalim authored
94 "#{options[:callback]}(#{json})"
95 else
96 self.content_type ||= Mime::JSON
97 json
6b15a45 @omjokine Change mimetype to Mime::JS if JSONP is used
omjokine authored
98 end
ad1c90d Added the :xml render option
Yehuda Katz + Carl Lerche authored
99 end
6923b39 Added the :rjs render option
Yehuda Katz + Carl Lerche authored
100
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
101 add :js do |js, options|
102 self.content_type ||= Mime::JS
4f04452 @josevalim Slightly reorganize rendering stack.
josevalim authored
103 js.respond_to?(:to_js) ? js.to_js(options) : js
6923b39 Added the :rjs render option
Yehuda Katz + Carl Lerche authored
104 end
69a1ea8 Created an ActionController::Renderers::All that includes all the def…
Yehuda Katz + Carl Lerche authored
105
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
106 add :xml do |xml, options|
107 self.content_type ||= Mime::XML
4f04452 @josevalim Slightly reorganize rendering stack.
josevalim authored
108 xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml
f9d570b Simpler RenderOption API -- removes the need for registering the type…
Carlhuda authored
109 end
2daac47 Added the ability to register methods to handle specific render optio…
Yehuda Katz + Carl Lerche authored
110 end
1fa7e33 @jeremy Ruby 1.9 compat: don't rely on lexical scoping since the included blo…
jeremy authored
111 end
Something went wrong with that request. Please try again.