Class: ActionController::MimeResponds::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/mime_responds.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_BLOCKS =
{
  :html    => 'Proc.new { render }',
  :js      => 'Proc.new { render :action => "#{action_name}.rjs" }',
  :xml     => 'Proc.new { render :action => "#{action_name}.rxml" }'
}

Instance Method Summary collapse

Constructor Details

#initialize(block_binding) ⇒ Responder

Returns a new instance of Responder.



117
118
119
120
121
122
# File 'lib/action_controller/mime_responds.rb', line 117

def initialize(block_binding)
  @block_binding = block_binding
  @mime_type_priority = eval("request.accepts", block_binding)
  @order     = []
  @responses = {}
end

Instance Method Details

#any(*args, &block) ⇒ Object



144
145
146
# File 'lib/action_controller/mime_responds.rb', line 144

def any(*args, &block)
  args.each { |type| send(type, &block) }
end

#custom(mime_type, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/action_controller/mime_responds.rb', line 124

def custom(mime_type, &block)
  mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s)
  
  @order << mime_type
  
  if block_given?
    @responses[mime_type] = block
  else
    @responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding)
  end
end

#respondObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/action_controller/mime_responds.rb', line 148

def respond
  for priority in @mime_type_priority
    if priority == Mime::ALL
      @responses[@order.first].call
      return
    else
      if priority === @order
        @responses[priority].call
        return # mime type match found, be happy and return
      end
    end
  end
  
  if @order.include?(Mime::ALL)
    @responses[Mime::ALL].call
  else
    eval 'render(:nothing => true, :status => "406 Not Acceptable")', @block_binding
  end
end