Changeset 792

Show
Ignore:
Timestamp:
10/30/07 14:10:30 (1 year ago)
Author:
iv..@gweezlebur.com
Message:

ivey_unhosing: hassox's patch

Files:

Legend:

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

    r521 r792  
    102102    # 
    103103    # The files themselves should be named action_name.mime_type.extension. For 
    104     # example, a herb template that should be the HTML part of the email, and 
    105     # rendered from the "foo" action would be named foo.html.herb. 
     104    # example, an erb template that should be the HTML part of the email, and 
     105    # rendered from the "foo" action would be named foo.html.erb. 
    106106    # 
    107107    # The only mime-types currently supported are "html" and "text", which 
     
    156156    #   render_mail :html => :foo, :text => "BAR" 
    157157    def render_mail(options = @method) 
    158       # If the options are not a hash, normalize to an action hash 
     158      @_missing_templates = false # used to make sure that at least one template was found 
     159      # # If the options are not a hash, normalize to an action hash 
    159160      options = {:action => {:html => options, :text => options}} if !options.is_a?(Hash) 
    160       # Normalize :html => :sym and :text => :sym to an action hash 
    161       options[:action] ||= {} 
    162         options[:action][:html] = options.delete(:html) if options[:html].is_a?(Symbol) 
    163         options[:action][:text] = options.delete(:text) if options[:text].is_a?(Symbol) 
    164       options.delete(:action) if options[:action].empty? 
    165  
    166       # set type to action or template, if the hash includes :action or :template 
    167       type = options.keys.find {|x| x.to_s =~ /(action|template)/} 
    168       # normalize :action/:template => ... to :action/template => {:html => ..., :text => ...} 
    169       options[type] = {:html => options[type], :text => options[type]} if type && !options[type].is_a?(Hash) 
    170       for mime_type in [:html, :text] 
    171         # if action or template, get the template to render. If not, get the string to render. If none 
    172         # are available, use the default params[:action] 
    173         renderer = (type && options[type][mime_type] ? options[type][mime_type] : options[mime_type]).to_s 
    174         # action or template 
    175         if type && options[type][mime_type] 
    176           # build the template to render 
    177           mime_renderer = renderer + ".#{mime_type}" if !(renderer =~ /.#{mime_type}$/) 
    178           if find_template(type => mime_renderer) 
    179             @mail.send((mime_type == :html ? "rawhtml=" : "text="),  
    180               render(type => mime_renderer, :layout => mimed_layout(mime_type, options[:layout])))  
    181           elsif find_template(type => renderer) && mime_type == :text 
    182             @mail.text = render(type => renderer, :layout => options[:layout]) 
     161       
     162      # Take care of the options 
     163      use_options = {} 
     164      opts = options.dup 
     165      actions = opts.delete(:action) if opts[:action].is_a?(Hash) 
     166      templates = opts.delete(:template) if opts[:template].is_a?(Hash) 
     167       
     168      # Prepare the options hash for each format 
     169      # We need to delete anything relating to the other format here 
     170      # before we try to render the template. 
     171      [:html, :text].each do |fmt| 
     172        opts_hash = use_options[fmt] = {} 
     173        opts_hash[fmt] = opts.delete(fmt) 
     174        opts_hash[fmt] ||= actions[fmt] if actions && actions[fmt] 
     175        opts_hash[:tempalte] = templates[fmt] if templates && templates[fmt] 
     176      end 
     177       
     178      # Send the result to the mailer 
     179      { :html => "rawhtml=", :text => "text="}.each do |fmt,meth| 
     180        begin 
     181          value = render use_options[fmt].merge!(opts).merge!(:clean_context => true, :format => fmt) 
     182          @mail.send(meth,value) unless value.nil? || value.empty? 
     183        rescue => e 
     184          # An error should be logged if no template is found instead of an error raised 
     185          if @_missing_templates 
     186            MERB_LOGGER.error(e) 
     187          else 
     188            @_missing_templates = true 
    183189          end 
    184         # string to render 
    185         else 
    186           @mail.send((mime_type == :html ? "rawhtml=" : "text="), renderer) 
    187190        end 
    188191      end 
     192      @mail 
    189193    end 
    190194     
     
    246250    end 
    247251     
    248     private 
    249     def mimed_layout(mime_type, layout = nil) 
    250       if layout && layout != :application 
    251         layout_choice = find_template(:layout => layout + ".#{mime_type}") || 
    252           find_template(:layout => layout) 
    253       elsif name = find_template(:layout => self.class.name.snake_case + ".#{mime_type}") 
    254         layout_choice = name 
    255       elsif name = find_template(:layout => self.class.name.snake_case) 
    256         layout_choice = name 
    257       elsif name = find_template(:layout => "application.#{mime_type}") 
    258         layout_choice = name 
    259       else 
    260         layout_choice = find_template(:layout => "application") 
    261       end 
    262       unless layout_choice 
    263         raise "No mail layout found" 
    264       end 
    265       layout_choice = layout_choice.split("/").last.split(".")[0...-1].join(".") 
    266     end 
    267      
    268252  end 
    269253end 
  • branches/ivey_unhosing/lib/merb/mixins/responder.rb

    r791 r792  
    6969    Merb.add_mime_type(:all,nil,%w[*/*]) 
    7070    Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) 
    71     Merb.add_mime_type(:txt,:to_text,%w[text/plain]) 
     71    Merb.add_mime_type(:text,:to_text,%w[text/plain]) 
    7272    Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) 
    7373    Merb.add_mime_type(:xml,:to_xml,%w[application/xml text/xml application/x-xml], :Encoding => "UTF-8") 
  • branches/ivey_unhosing/lib/merb/test/fake_request.rb

    r603 r792  
    6363        'HTTP_CONNECTION' => 'keep-alive', 
    6464        'REQUEST_METHOD' => 'GET'       
    65       } 
     65      } unless defined?(DEFAULT_ENV) 
    6666    end 
    6767  end 
  • branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/eighth.erb

    r267 r792  
    1 <%= @testing %> 
  • branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/ninth.erb

    r267 r792  
    1 <%= params[:x] %> 
  • branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/second.erb

    r267 r792  
    1 SECOND 
  • branches/ivey_unhosing/spec/merb/mail_controller_spec.rb

    r684 r792  
    5050  end 
    5151   
     52  def tenth 
     53    render_mail 
     54  end 
     55   
    5256end 
    5357 
     
    8387  it "should render files in its directory without a mimetype extension by default" do 
    8488    deliver :second 
    85     @delivery.text.should == "BASIC\nSECOND\nLAYOUT" 
     89    @delivery.text.should == "TEXT\nSECOND\nENDTEXT" 
    8690  end 
    8791   
     
    118122  it "should hold onto instance variables" do 
    119123    deliver :eighth 
    120     @delivery.text.should == "BASIC\nTEST\nLAYOUT"     
     124    @delivery.html.should == "BASIC\nTEST\nLAYOUT"     
    121125  end 
    122126   
     
    125129    @res = StringIO.new     
    126130    call_action :one 
    127     @delivery.text.should == "BASIC\nONE_CONTROLLER\nLAYOUT"     
     131    @delivery.html.should == "BASIC\nONE_CONTROLLER\nLAYOUT"     
     132  end 
     133   
     134  it "should not raise an error if there are no templates" do 
     135    lambda do 
     136      deliver :tenth     
     137    end.should_not raise_error 
     138  end 
     139   
     140  it "should log an error if there are no templates available" do 
     141    MERB_LOGGER.should_receive(:error).once 
     142    deliver :tenth 
    128143  end 
    129144