Skip to content

Commit

Permalink
Let an app load ActionMailer/ActionView (#56)
Browse files Browse the repository at this point in the history
* Let an app load ActionMailer/ActionView

Requiring ActionMailer while the gem is loading might break
initialization logic of a Rails app using Rails 6.0 or later.

ActionMailer 6.0 introduces the new `delivery_job` setting
(https://github.com/rails/rails/blob/6-0-stable/actionmailer/lib/action_mailer/railtie.rb#L49-L51)
whose value is being constantized and applied as soon as ActionMailer is
loaded. Normally, the delivery job would be configured during the app
initialization, so if ActionMailer is loaded **before** the initializer
is being executed, the configuration won’t be applied and a default
`delivery_job` class would be used.

The problem here is that Maildown is required before Rails initializers
run, and it forces loading of ActionMailer by requiring
`ActionMailer::Base` for monkey-patching. This causes ActionMailer to
never apply `delivery_job` config.

The solution seems simple enough: delay monkey-patching until
ActionMailer is loaded.

Because it's easy and seems safer, we'll also delay monkey-patching of
ActionView until it's loaded as well.

* Update CHANGELOG

* Bump up verssions in Gemfiles

Fix the CI build.
  • Loading branch information
sidonath authored Mar 17, 2021
1 parent 53616d0 commit bde223a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.3.0

- Delay loading of ActionMailer to fix support for `delivery_job` config (https://github.com/codetriage/maildown/pull/56)

## 3.2.0

- Switch to "github style markdown" (https://github.com/codetriage/maildown/pull/54)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_5.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
maildown (3.1.0)
maildown (3.2.0)
actionmailer (>= 4.0.0)
kramdown-parser-gfm

Expand Down Expand Up @@ -58,7 +58,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_51.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
maildown (3.1.0)
maildown (3.2.0)
actionmailer (>= 4.0.0)
kramdown-parser-gfm

Expand Down Expand Up @@ -58,7 +58,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_52.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
maildown (3.1.0)
maildown (3.2.0)
actionmailer (>= 4.0.0)
kramdown-parser-gfm

Expand Down Expand Up @@ -62,7 +62,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_6.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
maildown (3.1.0)
maildown (3.2.0)
actionmailer (>= 4.0.0)
kramdown-parser-gfm

Expand Down Expand Up @@ -75,7 +75,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_head.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ GIT
PATH
remote: ..
specs:
maildown (3.1.0)
maildown (3.2.0)
actionmailer (>= 4.0.0)
kramdown-parser-gfm

Expand All @@ -101,7 +101,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
Expand Down
8 changes: 6 additions & 2 deletions lib/maildown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def self.rails_6?
end

require 'maildown/markdown_engine'
require 'maildown/ext/action_mailer'
require 'maildown/ext/action_view'
ActiveSupport.on_load(:action_mailer) do
require 'maildown/ext/action_mailer'
end
ActiveSupport.on_load(:action_view) do
require 'maildown/ext/action_view'
end
require 'maildown/handlers/markdown'
3 changes: 0 additions & 3 deletions lib/maildown/ext/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require 'action_mailer'
require 'action_mailer/base'

# Monkeypatch to allow mailer to auto generate text/html
#
# If you generate a mailer action, by default it will only
Expand Down
2 changes: 0 additions & 2 deletions lib/maildown/ext/action_view.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'action_view'

# This monkeypatch allows the use of `.md.erb` file extensions
# in addition to `.md+erb` and `.md`
module ActionView
Expand Down

0 comments on commit bde223a

Please sign in to comment.