Changeset 791

Show
Ignore:
Timestamp:
10/28/07 22:27:00 (1 year ago)
Author:
iv..@gweezlebur.com
Message:

phew. at least got back to 8 failing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/ivey_unhosing/lib/merb/abstract_controller.rb

    r769 r791  
    199199    end 
    200200     
    201      
     201 
     202    def content_type 
     203      params[:format] 
     204    end 
    202205  end   
    203206   
  • branches/ivey_unhosing/lib/merb/mixins/render.rb

    r786 r791  
    2727    # 
    2828    #   Merb::Template::Haml 
    29     # 
    30     # Erubis template ext:  .erb  
    31     # Markaby template ext: .mab 
    32     # Builder template ext: .builder 
    33     # Haml template ext: .haml 
    3429    # 
    3530    # In addition, you can identify the type of output with an  
     
    378373        extensions = Template::EXTENSIONS.keys 
    379374        glob = "#{path}.#{@_template_format}.{#{opts[:ext] || extensions.join(",")}}" 
    380         glob_old = old_style_template_glob(path, extensions, opts) 
    381         Dir[glob].first || Dir[glob_old].first || (@_merb_unmatched = glob << "<br/>Or on the old scheme<br/>" << glob_old; nil) 
     375        Dir[glob].first || (@_merb_unmatched = glob; nil) 
    382376      end 
    383377       
     
    385379      # request of the developer. 
    386380      def choose_template_format(types, opts) 
    387         opts[:format] ||= (content_type_set? ? content_type : params[:format] ) 
     381        opts[:format] ||= content_type 
    388382        @_template_format = [(opts.keys & types.keys)].flatten.first # Check for render :js => etc 
    389383        @_template_format ||= opts[:format] || :html                                            
     
    406400      end 
    407401       
    408       # this method is to support the old style of extensions.   
    409       # the newer template.format.engine should take it's place 
    410       def old_style_template_glob(path, extensions, opts) #:nodoc: 
    411         ext = [] 
    412         case opts[:format] 
    413         when :js 
    414           ext << %w(jerb) 
    415         when :xml 
    416           ext << %w(xerb rxml builder) 
    417         when :html 
    418           ext << %w(herb rhtml erb) 
    419         end 
    420         out = "#{path}.{#{(opts[:ext] || ext << extensions).join(",")}}" 
    421       end 
    422402  end   
    423403end 
  • branches/ivey_unhosing/lib/merb/mixins/responder.rb

    r784 r791  
    7979  module ResponderMixin 
    8080     
    81     def respond_to(&block) 
    82       responder = Rest::Responder.new(request.accept, params) 
    83       block.call(responder) 
    84       responder.respond(headers) 
    85       @_status = responder.status if responder.status 
    86       responder.body 
    87     end 
    88      
    89     def determine_content_type 
     81    # def respond_to(&block) 
     82    #   responder = Rest::Responder.new(request.accept, params) 
     83    #   block.call(responder) 
     84    #   responder.respond(headers) 
     85    #   @_status = responder.status if responder.status 
     86    #   responder.body 
     87    # end 
     88     
     89    def perform_content_negotiation 
    9090      raise Merb::ControllerExceptions::NotAcceptable if provided_formats.empty? 
    9191      if fmt = params[:format] 
     
    114114     
    115115    def content_type 
    116       @_content_type ||= determine_content_type 
     116      unless content_type_set? 
     117        @_content_type = perform_content_negotiation 
     118        raise Merb::ControllerExceptions::NotAcceptable.new("Unknown content_type for response: #{@_content_type}") unless 
     119          Merb.available_mime_types.has_key?(@_content_type) 
     120        headers['Content-Type'] = Merb.available_mime_types[@_content_type].first 
     121      end 
     122      @_content_type 
    117123    end 
    118124     
     
    129135      class Responder 
    130136       
    131         attr_reader :body, :type, :status 
    132        
    133         def initialize(accept_header, params={}) 
    134           MERB_LOGGER.info accept_header 
    135           @accepts = Responder.parse(accept_header) 
    136           @params = params 
    137           @stack  = {} 
    138         end 
    139        
    140         def method_missing(symbol, &block) 
    141           raise "respond_to expects a block" unless block_given? 
    142           # the first method we encounter here will be used for the catch all mime-type */* 
    143           @default_content_type ||= symbol 
    144           @stack[symbol] = block 
    145         end 
    146          
    147         def respond(headers) 
    148           unless @stack.keys.all?{|k| TYPES.has_key?(k) } 
    149             raise "unrecognized mime type in respond_to block" 
    150           end 
    151           mime_type = negotiate_content 
    152           if mime_type 
    153             headers['Content-Type'] = mime_type.super_range 
    154             @params[:format] = mime_type.to_sym 
    155             @body   = @stack[mime_type.to_sym].call 
    156           else 
    157             raise ControllerExceptions::NotAcceptable 
    158           end 
    159         end 
    160  
    161137        protected 
    162138           
     
    177153          end 
    178154           
    179         private 
    180          
    181           def negotiate_content 
    182             if @params[:format] 
    183               negotiate_by_format 
    184             elsif @accepts.first.to_sym == :all || (@stack.keys & @accepts.map(&:to_sym)).size > 0 
    185               negotiate_by_accept_header 
    186             end 
    187           end 
    188            
    189           def negotiate_by_format 
    190             format = @params[:format].to_sym 
    191             if @stack[format] 
    192               if @accepts.map {|a| a.to_sym }.include?(format) 
    193                 @accepts.detect{|a| a.to_sym == format } 
    194               else 
    195                 AcceptType.new(TYPES[format].first,0) 
    196               end 
    197             end 
    198           end 
    199            
    200           def negotiate_by_accept_header 
    201             @accepts.each do |accept| 
    202               if accept.to_sym == :all 
    203                 if @default_content_type 
    204                   return AcceptType.new(TYPES[@default_content_type].first,0) 
    205                 else 
    206                   raise "no formats specified in respond_to block" 
    207                 end 
    208               end 
    209               return accept if @stack[accept.to_sym] 
    210             end 
    211           end 
    212          
    213155      end 
    214156 
  • branches/ivey_unhosing/spec/fixtures/controllers/render_spec_controllers.rb

    r784 r791  
    1111 
    1212# Fake class so we can render subdirectories of views 
    13 class Examples < Merb::Controller; end 
     13class Examples < Merb::Controller;  
     14end 
    1415 
    1516module Nested 
     
    4748 
    4849class ExtensionTemplateController < Merb::Controller 
     50  provides :js, :xml 
    4951  def erubis_templates 
    50     respond_to do |format| 
    51       format.html { render } 
    52       format.js   { render } 
    53       format.xml  { render } 
    54     end 
     52    render 
    5553  end 
    5654 
    5755  def haml_templates 
    58     respond_to do |format| 
    59       format.html { render } 
    60       format.js   { render } 
    61       format.xml  { render } 
    62     end 
     56    render 
    6357  end 
    6458 
    6559  def markaby_templates 
    66     respond_to do |format| 
    67       format.html { render } 
    68       format.js   { render } 
    69       format.xml  { render } 
    70     end 
     60    render 
    7161  end 
    7262 
    7363  def old_style_erubis 
    74     respond_to do |format| 
    75       format.html { render } 
    76       format.js   { render } 
    77       format.xml  { render } 
    78     end 
     64    render 
    7965  end 
    8066 
    8167  def old_style_erubis2 
    82     respond_to do |format| 
    83       format.html { render } 
    84       format.js   { render } 
    85       format.xml  { render } 
    86     end 
     68    render 
    8769  end 
    8870 
    8971  def old_style_erubis3 
    90     respond_to do |format| 
    91       format.html { render } 
    92       format.xml  { render } 
    93     end 
     72    render 
    9473  end 
    9574 
    9675  def old_style_haml 
    97     respond_to do |format| 
    98       format.html { render } 
    99       format.js   { render } 
    100       format.xml  { render } 
    101     end 
     76    render 
    10277  end 
    10378 
    10479  def old_style_markaby 
    105     respond_to do |format| 
    106       format.html { render } 
    107       format.js   { render } 
    108       format.xml  { render } 
    109     end 
     80    render 
    11081  end 
    11182 
    11283  def old_style_builder 
    113     respond_to do |format| 
    114       format.html { render } 
    115       format.js   { render } 
    116       format.xml  { render } 
    117     end 
     84    render 
    11885  end 
    11986 
  • branches/ivey_unhosing/spec/merb/render_spec.rb

    r784 r791  
    5050    lambda { 
    5151      c.render_no_layout(:template => nil) 
    52     }.should raise_error(Exception, "called find_template without an :action or :layout"
     52    }.should raise_error(Merb::ControllerExceptions::TemplateNotFound
    5353  end 
    5454 
     
    146146      c = new_controller 
    147147      content = c.render(:template => "erubis") 
    148       c.template.should == "erubis.herb" 
     148      c.template.should == "erubis.html.erb" 
    149149    end 
    150150 
     
    185185      c.template.should == "erubis_templates.xml.erb" 
    186186    end 
    187  
    188     it "should provide backwards compatibility and render old_style_erubis.herb" do 
    189       c = new_spec_controller(:format => 'html') 
    190       c.dispatch(:old_style_erubis) 
    191       c.template.should == "old_style_erubis.herb" 
    192     end 
    193  
    194     it "should provide backwards compatibility and render old_syle_erubis.jerb" do 
    195       c = new_spec_controller(:format => 'js') 
    196       c.dispatch(:old_style_erubis) 
    197       c.template.should == "old_style_erubis.jerb" 
    198     end 
    199  
    200     it "should provide backwards compatibility and render old_style_erubis2.rhtml" do 
    201       c = new_spec_controller(:format => 'html') 
    202       c.dispatch(:old_style_erubis2) 
    203       c.template.should == "old_style_erubis2.rhtml" 
    204     end 
    205  
    206     it "should provide backwards compatibility and render old_style_erubis3.rhtml" do 
    207       c = new_spec_controller(:format => 'html') 
    208       c.dispatch(:old_style_erubis3) 
    209       c.template.should == "old_style_erubis3.erb" 
    210     end 
    211187     
    212188    it "should render js in nested partials when the format is javascript" do 
     
    222198    end 
    223199 
    224     # it "should render js with .rhtml" do 
    225     #   c = new_spec_controller(:format => 'js') 
    226     #   c.dispatch(:old_style_erubis2) 
    227     #   c.template.should == "old_style_erubis2.rhtml" 
    228     # end 
    229  
    230200  end 
    231201 
     
    256226      c.dispatch(:markaby_templates) 
    257227      c.template.should == "markaby_templates.xml.mab" 
    258     end 
    259  
    260     it "should render all actions with .mab" do 
    261       %w( html js xml ).each do |format| 
    262         c = new_spec_controller(:format => format) 
    263         c.dispatch(:old_style_markaby) 
    264         c.template.should == "old_style_markaby.mab" 
    265       end 
    266228    end 
    267229  end 
     
    297259      end 
    298260 
    299       it "should render all actions with .haml" do 
    300         %w( html js xml ).each do |format| 
    301           c = new_spec_controller(:format => format) 
    302           c.dispatch(:old_style_haml) 
    303           c.template.should == "old_style_haml.haml" 
    304         end 
    305       end 
    306261    end 
    307262 
     
    323278      end 
    324279 
    325       it "should render all actions with .builder" do 
    326         %w( html js xml ).each do |format| 
    327           c = new_spec_controller(:format => format) 
    328           c.dispatch(:old_style_builder) 
    329           c.template.should == "old_style_builder.builder" 
    330         end 
    331       end 
    332  
    333       it "should provide backwards compatibility and render old_style_erubis.xerb" do 
    334         c = new_spec_controller(:format => "xml") 
    335         c.dispatch(:old_style_erubis) 
    336         c.template.should == "old_style_erubis.xerb" 
    337       end 
    338  
    339       it "should provide backwards compatibility and render old_style_erubis2.rxml" do 
    340         c = new_spec_controller(:format => "xml") 
    341         c.dispatch(:old_style_erubis2) 
    342         c.template.should == "old_style_erubis2.rxml" 
    343       end 
    344  
    345       it "should provide backwards compatibility and render old_style_erubis3.rxml" do 
    346         c = new_spec_controller(:format => "xml") 
    347         c.dispatch(:old_style_erubis3) 
    348         c.template.should == "old_style_erubis3.builder" 
    349       end 
    350280    end 
    351281  end 
  • branches/ivey_unhosing/spec/merb/responder_spec.rb

    r784 r791  
    224224 
    225225class ResponderSpecController < Merb::Controller 
    226    
    227226  def index 
    228     respond_to do |format| 
    229       format.html { "html" } 
    230       format.xml  { "xml" } 
    231       format.yaml { "yaml" } 
    232     end 
     227    only_provides :html, :xml, :yaml 
     228    content_type.to_s 
    233229  end 
    234230   
    235231  def create 
    236     respond_to do |format| 
    237       format.xml { render :nothing => 201 } 
    238     end 
    239   end 
    240    
     232    only_provides :xml 
     233    render :nothing => 201 
     234  end 
    241235end 
    242236 
    243237class CrazyResponderSpecController < Merb::Controller 
    244    
    245238  def index 
    246     respond_to do |format| 
    247       format.donkey { "donkey" } 
    248     end 
    249   end 
    250    
    251 end 
    252  
    253 class RespondToVsNoRespondToController < Merb::Controller 
    254    
    255   def with_respond_to 
    256     respond_to do |type| 
    257       type.html { "a response" } 
    258     end 
    259   end 
    260    
    261   def without_respond_to 
    262     "a response" 
    263   end 
     239    only_provides :donkey 
     240    content_type 
     241    "donkey" 
     242  end   
    264243end 
    265244 
     
    325304  end 
    326305   
    327   it "should raise runtime error when respond_to type is not in TYPES" do 
     306  it "should raise 406 when negotiated type is not in TYPES" do 
    328307    r = Merb::Test::FakeRequest.new 
    329308    c = CrazyResponderSpecController.build(r, r.body) 
    330     lambda{c.dispatch(:index)}.should raise_error(RuntimeError) 
    331   end 
    332    
    333   it "should return the same whether respond_to is used or not and Accept is */*" do 
    334     r1 = Merb::Test::FakeRequest.new(:http_accept => '*/*') 
    335     c1 = RespondToVsNoRespondToController.build(r1, r1.body) 
    336     c1.dispatch(:without_respond_to) 
    337     r2 = Merb::Test::FakeRequest.new(:http_accept => '*/*') 
    338     c2 = RespondToVsNoRespondToController.build(r2, r2.body) 
    339     c2.dispatch(:with_respond_to) 
    340     c1.headers['Content-Type'].should == c2.headers['Content-Type'] 
    341   end 
    342    
     309    lambda{c.dispatch(:index)}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
     310  end 
     311     
    343312  it "should call the block for the supported response type yaml" do 
    344313    c = new_responder_spec_controller(:http_accept => 'text/yaml') 
     
    387356    ResponderSpecController.build(@request, @request.body) 
    388357  end 
    389  
    390358end 
    391359 
     
    404372  end 
    405373 
    406   it "should have a determine_content_type" do 
    407     @responder.should respond_to(:determine_content_type
     374  it "should have a perform_content_negotiation" do 
     375    @responder.should respond_to(:perform_content_negotiation
    408376  end 
    409377   
     
    412380    @responder.should_receive(:provided_formats). 
    413381      at_least(:once).and_return([:html]) 
    414     @responder.determine_content_type 
     382    @responder.perform_content_negotiation 
    415383  end 
    416384   
     
    418386    @responder.should_receive(:provided_formats). 
    419387      at_least(:once).and_return([:html]) 
    420     @responder.determine_content_type 
     388    @responder.perform_content_negotiation 
    421389  end 
    422390   
     
    424392    @responder.should_receive(:params).at_least(:once). 
    425393      and_return({:format => "html"}) 
    426     @responder.determine_content_type 
     394    @responder.perform_content_negotiation 
    427395  end 
    428396 
     
    431399    @responder.should_receive(:request).once.and_return(@request) 
    432400    @request.should_receive(:accept).once.and_return("text/html") 
    433     @responder.determine_content_type 
     401    @responder.perform_content_negotiation 
    434402  end 
    435403 
    436404  it "should not use request.accept when params[:format] is not nil" do 
    437405    @responder.should_receive(:request).exactly(0).times 
    438     @responder.determine_content_type 
     406    @responder.perform_content_negotiation 
    439407  end 
    440408   
    441409  it "should return :html when params[:format] = :html" do 
    442     @responder.determine_content_type.should == :html     
     410    @responder.perform_content_negotiation.should == :html     
    443411  end 
    444412   
     
    446414    @responder.should_receive(:provided_formats).at_least(:once). 
    447415      and_return([:xml]) 
    448     lambda {@responder.determine_content_type}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
     416    lambda {@responder.perform_content_negotiation}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
    449417  end 
    450418   
     
    453421    @responder.stub!(:params).and_return({}) 
    454422    @request.should_receive(:accept).once.and_return("*/*") 
    455     @responder.determine_content_type.should == :xml 
     423    @responder.perform_content_negotiation.should == :xml 
    456424  end 
    457425 
     
    460428    @responder.stub!(:params).and_return({}) 
    461429    @request.should_receive(:accept).once.and_return("text/xml, text/html") 
    462     @responder.determine_content_type.should == :xml 
     430    @responder.perform_content_negotiation.should == :xml 
    463431  end 
    464432   
     
    467435    @responder.stub!(:params).and_return({}) 
    468436    @request.should_receive(:accept).once.and_return("text/xml, text/json") 
    469     lambda {@responder.determine_content_type}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
     437    lambda {@responder.perform_content_negotiation}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
    470438  end 
    471439   
     
    474442    @responder.should_receive(:params).exactly(0).times 
    475443    @responder.should_receive(:request).exactly(0).times 
    476     lambda {@responder.determine_content_type}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
     444    lambda {@responder.perform_content_negotiation}.should raise_error(Merb::ControllerExceptions::NotAcceptable) 
    477445  end 
    478446end 
  • branches/ivey_unhosing/spec/spec_helper.rb

    r627 r791  
    2424  request.params.update(additional_params) 
    2525  request.cookies = {} 
     26  request.accept ||= '*/*' 
    2627   
    2728  yield request if block_given?