Class: ActionController::MimeResponds::Collector

Inherits:
Object
  • Object
show all
Includes:
AbstractController::Collector
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 ActionController::MimeResponds::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.

Defined Under Namespace

Classes: VariantCollector

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractController::Collector

generate_method_for_mime

Constructor Details

#initialize(mimes, variant = nil) ⇒ Collector

Returns a new instance of Collector.



255
256
257
258
259
260
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 255

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

#formatObject

Returns the value of attribute format.



253
254
255
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 253

def format
  @format
end

Instance Method Details

#any(*args, &block) ⇒ Object Also known as: all



262
263
264
265
266
267
268
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 262

def any(*args, &block)
  if args.any?
    args.each { |type| send(type, &block) }
  else
    custom(Mime::ALL, &block)
  end
end

#any_response?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 280

def any_response?
  !@responses.fetch(format, false) && @responses[Mime::ALL]
end

#custom(mime_type, &block) ⇒ Object



271
272
273
274
275
276
277
278
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 271

def custom(mime_type, &block)
  mime_type = Mime::Type.lookup(mime_type.to_s) unless mime_type.is_a?(Mime::Type)
  @responses[mime_type] ||= if block_given?
    block
  else
    VariantCollector.new(@variant)
  end
end

#negotiate_format(request) ⇒ Object



297
298
299
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 297

def negotiate_format(request)
  @format = request.negotiate_mime(@responses.keys)
end

#responseObject



284
285
286
287
288
289
290
291
292
293
294
295
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 284

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