Changeset 792
- Timestamp:
- 10/30/07 14:10:30 (1 year ago)
- Files:
-
- branches/ivey_unhosing/lib/merb/mail_controller.rb (modified) (3 diffs)
- branches/ivey_unhosing/lib/merb/mixins/responder.rb (modified) (1 diff)
- branches/ivey_unhosing/lib/merb/test/fake_request.rb (modified) (1 diff)
- branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/eighth.erb (modified) (1 diff)
- branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/ninth.erb (modified) (1 diff)
- branches/ivey_unhosing/spec/fixtures/mailers/views/test_mail_controller/second.erb (modified) (1 diff)
- branches/ivey_unhosing/spec/merb/mail_controller_spec.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ivey_unhosing/lib/merb/mail_controller.rb
r521 r792 102 102 # 103 103 # 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, and105 # 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. 106 106 # 107 107 # The only mime-types currently supported are "html" and "text", which … … 156 156 # render_mail :html => :foo, :text => "BAR" 157 157 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 159 160 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 183 189 end 184 # string to render185 else186 @mail.send((mime_type == :html ? "rawhtml=" : "text="), renderer)187 190 end 188 191 end 192 @mail 189 193 end 190 194 … … 246 250 end 247 251 248 private249 def mimed_layout(mime_type, layout = nil)250 if layout && layout != :application251 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 = name255 elsif name = find_template(:layout => self.class.name.snake_case)256 layout_choice = name257 elsif name = find_template(:layout => "application.#{mime_type}")258 layout_choice = name259 else260 layout_choice = find_template(:layout => "application")261 end262 unless layout_choice263 raise "No mail layout found"264 end265 layout_choice = layout_choice.split("/").last.split(".")[0...-1].join(".")266 end267 268 252 end 269 253 end branches/ivey_unhosing/lib/merb/mixins/responder.rb
r791 r792 69 69 Merb.add_mime_type(:all,nil,%w[*/*]) 70 70 Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) 71 Merb.add_mime_type(:t xt,:to_text,%w[text/plain])71 Merb.add_mime_type(:text,:to_text,%w[text/plain]) 72 72 Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) 73 73 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 63 63 'HTTP_CONNECTION' => 'keep-alive', 64 64 'REQUEST_METHOD' => 'GET' 65 } 65 } unless defined?(DEFAULT_ENV) 66 66 end 67 67 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 SECONDbranches/ivey_unhosing/spec/merb/mail_controller_spec.rb
r684 r792 50 50 end 51 51 52 def tenth 53 render_mail 54 end 55 52 56 end 53 57 … … 83 87 it "should render files in its directory without a mimetype extension by default" do 84 88 deliver :second 85 @delivery.text.should == " BASIC\nSECOND\nLAYOUT"89 @delivery.text.should == "TEXT\nSECOND\nENDTEXT" 86 90 end 87 91 … … 118 122 it "should hold onto instance variables" do 119 123 deliver :eighth 120 @delivery. text.should == "BASIC\nTEST\nLAYOUT"124 @delivery.html.should == "BASIC\nTEST\nLAYOUT" 121 125 end 122 126 … … 125 129 @res = StringIO.new 126 130 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 128 143 end 129 144
