Class: ActionController::MimeResponds::Collector
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | actionpack/lib/action_controller/metal/mime_responds.rb |
Overview
A container for responses available from the current controller for requests for different mime-types sent to a particular action.
The public controller methods #respond_to may be called with a block that is used to define responses to different mime-types, e.g. for #respond_to :
respond_to do |format|
format.html
format.xml { render xml: @people }
end
In this usage, the argument passed to the block (#format above) is an instance of the Collector
class. This object serves as a container in which available responses can be stored by calling any of the dynamically generated, mime-type-specific methods such as html
, xml
etc on the Collector
. Each response is represented by a corresponding block if present.
A subsequent call to #negotiate_format(request) will enable the Collector
to determine which specific mime-type it should respond with for the current request, with this response then being accessible by calling #response.
Class Method Summary
- .new(mimes, variant = nil) ⇒ Collector constructor
Instance Attribute Summary
- #any_response? ⇒ Boolean readonly
- #format rw
Instance Method Summary
-
#all(*args, &block)
Alias for #any.
- #any(*args, &block) (also: #all)
- #custom(mime_type, &block)
- #negotiate_format(request)
- #response
Constructor Details
.new(mimes, variant = nil) ⇒ Collector
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 246
def initialize(mimes, variant = nil) @responses = {} @variant = variant mimes.each { |mime| @responses[Mime[mime]] = nil } end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AbstractController::Collector
Instance Attribute Details
#any_response? ⇒ Boolean
(readonly)
[ GitHub ]
#format (rw)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 244
attr_accessor :format
Instance Method Details
#all(*args, &block)
Alias for #any.
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 260
alias :all :any
#any(*args, &block) Also known as: #all
[ GitHub ]#custom(mime_type, &block)
[ GitHub ]#negotiate_format(request)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 288
def negotiate_format(request) @format = request.negotiate_mime(@responses.keys) end
#response
[ GitHub ]# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 275
def response response = @responses.fetch(format, @responses[Mime::ALL]) if response.is_a?(VariantCollector) # `format.html.phone` - variant inline syntax response.variant elsif response.nil? || response.arity == 0 # `format.html` - just a format, call its block response else # `format.html{ |variant| variant.phone }` - variant block syntax variant_collector = VariantCollector.new(@variant) response.call(variant_collector) # call format block with variants collector variant_collector.variant end end