Skip to content

Commit

Permalink
Strip Attachment elements from sidebar
Browse files Browse the repository at this point in the history
The count of elements is (or at least can be) different depending on the
email headers - like reply-to, etc… So rather than relying on the count
and stripping the last two, find the `<dt>` containing the
"Attachments:" label text, and then remove it and the element following
it.
  • Loading branch information
stevenharman committed May 13, 2024
1 parent 1583a7b commit 1c5cd10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Style/Documentation:
- 'lib/letter_opener_web.rb'
- 'lib/letter_opener_web/delivery_method.rb'
- 'lib/letter_opener_web/engine.rb'

Metrics/ClassLength:
Exclude:
- 'app/models/letter_opener_web/letter.rb'
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [Unreleased](https://github.com/fgrehm/letter_opener_web/compare/v2.0.0...master)

- Reliably strip Attachment links from the sidebar. [#132](https://github.com/fgrehm/letter_opener_web/pull/132)

## [v2.0.0](https://github.com/fgrehm/letter_opener_web/compare/v1.4.1...v2.0.0)

- Require Rails >= 5.2, run tests against Rails 6.1 [#113](https://github.com/fgrehm/letter_opener_web/pull/113)
Expand Down
9 changes: 6 additions & 3 deletions app/models/letter_opener_web/letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ def valid?

def remove_attachments_link(headers)
xml = REXML::Document.new(headers)
if xml.root.elements.size == 10
xml.delete_element('//dd[last()]')
xml.delete_element('//dt[last()]')
label_element = xml.root.elements.find { |e| e.get_text&.value&.match?(/attachments:/i) }

if label_element
xml.root.delete_element(label_element.next_element) # the list of attachments
xml.root.delete_element(label_element)
end

xml.to_s
end

Expand Down

0 comments on commit 1c5cd10

Please sign in to comment.