Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor attachments

  • Loading branch information...
commit 69f2d02e7487819a58e651bcdd38619d727d04eb 1 parent 690c22c
@nashby nashby authored
View
16 lib/letter_opener/message.html.erb
@@ -79,16 +79,14 @@
<dd><%= h mail.bcc.join(", ") %></dd>
<% end %>
- <dt>Attachments:</dt>
- <dd>
- <% if @attachments.size > 0 %>
- <% @attachments.each do |filename, path| %>
- <a href="<%= path %>"><%= filename %></a>&nbsp;
- <% end %>
- <% else %>
- None
+ <% if @attachments.any? %>
+ <dt>Attachments:</dt>
+ <dd>
+ <% @attachments.each do |filename, path| %>
+ <a href="<%= path %>"><%= filename %></a>&nbsp;
<% end %>
- </dd>
+ </dd>
+ <% end %>
</dl>
<% if mail.multipart? %>
View
17 lib/letter_opener/message.rb
@@ -21,14 +21,16 @@ def initialize(location, mail, part = nil)
def render
FileUtils.mkdir_p(@location)
- if mail.attachments.size > 0
- attachments_dir = File.join(@location,'attachments')
+ if mail.attachments.any?
+ attachments_dir = File.join(@location, 'attachments')
FileUtils.mkdir_p(attachments_dir)
mail.attachments.each do |attachment|
path = File.join(attachments_dir, attachment.filename)
+
unless File.exists?(path) # true if other parts have already been rendered
File.open(path, 'wb') { |f| f.write(attachment.body.raw_source) }
end
+
@attachments << [attachment.filename, "attachments/#{URI.escape(attachment.filename)}"]
end
end
@@ -51,14 +53,15 @@ def content_type
end
def body
- if !@body_string
- @body_string = (@part && @part.body || @mail.body).to_s
+ @body ||= begin
+ body = (@part && @part.body || @mail.body).to_s
+
mail.attachments.each do |attachment|
- @body_string.gsub!(attachment.url, "attachments/#{attachment.filename}")
+ body.gsub!(attachment.url, "attachments/#{attachment.filename}")
end
- @body = @body_string
+
+ body
end
- @body
end
def from
View
4 spec/letter_opener/delivery_method_spec.rb
@@ -178,7 +178,7 @@
attachments[File.basename(__FILE__)] = File.read(__FILE__)
end
attachment_path = Dir["#{location}/*/attachments/#{File.basename(__FILE__)}"].first
- File.exists?(attachment_path).should == true
+ File.exists?(attachment_path).should be_true
text = File.read(Dir["#{location}/*/plain.html"].first)
text.should include(File.basename(__FILE__))
end
@@ -196,7 +196,7 @@
end
end
attachment_path = Dir["#{location}/*/attachments/#{File.basename(__FILE__)}"].first
- File.exists?(attachment_path).should == true
+ File.exists?(attachment_path).should be_true
text = File.read(Dir["#{location}/*/rich.html"].first)
mail.parts[0].body.should include(mail.attachments[0].url)
text.should_not include(mail.attachments[0].url)

2 comments on commit 69f2d02

@davidcornu

My bad :p

@nashby
Collaborator

Hey, you are awesome! Thanks again for this feature.

Please sign in to comment.
Something went wrong with that request. Please try again.