Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer reading header from rich style over plain one #114

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, next ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to run CI on this PR.
If you don't like this change, I can separate this from this PR and will discuss in another PR.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks !


jobs:
test:
Expand Down
6 changes: 3 additions & 3 deletions app/models/letter_opener_web/letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def initialize(params)
end

def headers
html = read_file(:plain) if style_exists?('plain')
html ||= read_file(:rich)
html = read_file(:rich) if style_exists?('rich')
html ||= read_file(:plain)

# NOTE: This is ugly, we should look into using nokogiri and making that a
# dependency of this gem
match_data = html.match(%r{<body>\s*<div[^>]+id="container">\s*<div[^>]+id="message_headers">\s*(<dl>.+</dl>)}m)
return remove_attachments_link(match_data[1]).html_safe if match_data[1].present?
return remove_attachments_link(match_data[1]).html_safe if match_data && match_data[1].present?

'UNABLE TO PARSE HEADERS'
end
Expand Down
54 changes: 44 additions & 10 deletions spec/models/letter_opener_web/letter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ def rich_text(mail_id)
<<~MAIL
Rich text for #{mail_id}
<!DOCTYPE html>
<a href='a-link.html'>
<img src='an-image.jpg'>
Link text
</a>
<a href='fooo.html'>Bar</a>
<a href="example.html" class="blank"></a>
<address><a href="inside-address.html">inside address</a></address>
<body>
<div id="container">
<div id="message_headers">
<dl>
<dt>From:</dt>
<dd>noreply@example.com</dd>
</dl>
</div>

<a href='a-link.html'>
<img src='an-image.jpg'>
Link text
</a>
<a href='fooo.html'>Bar</a>
<a href="example.html" class="blank"></a>
<address><a href="inside-address.html">inside address</a></address>
</div>
</body>
MAIL
end

Expand All @@ -36,6 +47,28 @@ def rich_text(mail_id)
FileUtils.rm_rf(location)
end

describe 'rich text headers' do
let(:id) { '1111_1111' }
subject { described_class.new(id: id).headers }

before do
FileUtils.rm_rf("#{location}/#{id}/plain.html")
end

it { is_expected.to match(%r{<dl>\s*<dt>From:</dt>\s*<dd>noreply@example\.com</dd>}m) }
end

describe 'plain text headers' do
let(:id) { '1111_1111' }
subject { described_class.new(id: id).headers }

before do
FileUtils.rm_rf("#{location}/#{id}/rich.html")
end

it { is_expected.to eq('UNABLE TO PARSE HEADERS') }
end

describe 'rich text version' do
let(:id) { '1111_1111' }
subject { described_class.new(id: id).rich_text }
Expand All @@ -45,9 +78,10 @@ def rich_text(mail_id)
it 'changes links to show up on a new window' do
link_html = [
"<a href='a-link.html' target='_blank'>",
"<img src='an-image.jpg'/>",
"Link text\n</a>"
].join("\n ")
" <img src='an-image.jpg'/>",
' Link text',
'</a>'
].join("\n ")

expect(subject).to include(link_html)
end
Expand Down