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.



111
112
113
114
115
116
# File 'lib/action_controller/mime_responds.rb', line 111

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



138
139
140
# File 'lib/action_controller/mime_responds.rb', line 138

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

#custom(mime_type, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/action_controller/mime_responds.rb', line 118

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/action_controller/mime_responds.rb', line 142

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