Skip to content

Commit

Permalink
Move html_safe declaration for user_mailer from layout to helper
Browse files Browse the repository at this point in the history
This allows us to enable output safety checks for all code within
erb files.

`rubocop --auto-gen-config` ignores code within the erb files, so
it is easier to maintain an exclusion on the helper than inside
the layout.
  • Loading branch information
gravitystorm committed Nov 27, 2024
1 parent d2dc1ff commit ce342fe
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .erb_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ linters:
Enabled: false
Naming/FileName:
Enabled: false
Rails/OutputSafety:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
SelfClosingTag:
Expand Down
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Rails/NotNullColumn:
Rails/OutputSafety:
Exclude:
- 'app/helpers/application_helper.rb'
- 'app/helpers/user_mailer_helper.rb'
- 'lib/rich_text.rb'
- 'test/helpers/application_helper_test.rb'

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/user_mailer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def style_message(html)
# Because we can't use stylesheets in HTML emails, we need to inline the
# styles. Rather than copy-paste the same string of CSS into every message,
# we apply it once here, after the message has been composed.
html.gsub("<p>", '<p style="color: black; margin: 0.75em 0; font-family: \'Helvetica Neue\', Arial, Sans-Serif">')
html.gsub("<p>", '<p style="color: black; margin: 0.75em 0; font-family: \'Helvetica Neue\', Arial, Sans-Serif">').html_safe
end

def style_left
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/user_mailer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<table style="background-color: #fff; color: #222; border: solid 1px #ccc; border-collapse: separate">
<tr>
<td style="text-align: <%= style_left %>; padding: 0px 15px 5px 15px">
<%= raw style_message(yield) %>
<%= style_message(yield) %>
</td>
</tr>
</table>
Expand Down

0 comments on commit ce342fe

Please sign in to comment.