Skip to content

Commit

Permalink
Exploration
Browse files Browse the repository at this point in the history
Looking at ways to improve the Gem and fix support for Rails > 7.0.8

Overriding the Rails::MailerController is a recipie for pain and is the
source of the issue we see in Rails > 7.0.8.

Right now I think all this does is allows a custom layout (the GOV.UK
one) - we can do that without the heavy handiness by suppling a layout
in the Mailer class - this commit contains that change.

To support the layout simply being in the views, we have to become a
'Engine' so we can add views by replicating the Rails directories (there
are other ways but this is nice and simple).

Becuase engines are just Railties, everything should work as it did,
again that change is here - needs better naming.

https://api.rubyonrails.org/classes/Rails/Engine.html

With all that done, the previews just work™ the mailer suppling the
layout overide for Mail::Notify::Mailer subclasses only.

I've only checked the 'view' version so far - but I can't see it being a
problem for the 'template' version.

With these changes all of the rails stuff in the specs is redundent, I
think - so everything becomes much simpler.

Right now I am testing this with a fully fledged Rails 7.1.2 install -
it would be nice to come up with a way to run 'tests' in all 3 major
versions of Rails - may Docker could help?

I would like to get this working and go for 1.2.1 so testing outside of
the repo is probably enough for now though!

Until next Friday! :wave:
  • Loading branch information
mec committed Dec 1, 2023
1 parent 529b22f commit cef6319
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/mail/notify/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Mail
module Notify
class Mailer < ActionMailer::Base
layout "govuk_notify_layout"

def view_mail(template_id, headers)
raise ArgumentError, "You must specify a template ID" if template_id.blank?

Expand Down
8 changes: 1 addition & 7 deletions lib/mail/notify/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

module Mail
module Notify
class Railtie < Rails::Railtie
class Railtie < Rails::Engine
initializer "mail-notify.add_delivery_method", before: "action_mailer.set_configs" do
ActionMailer::Base.add_delivery_method(:notify, Mail::Notify::DeliveryMethod)
end

initializer "mail-notify.action_controller" do
ActiveSupport.on_load(:action_controller, run_once: true) do
Rails::MailersController.prepend(Mail::Notify::MailersController)
end
end
end
end
end
2 changes: 1 addition & 1 deletion mail-notify.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "simplecov", "~> 0.21"
spec.add_development_dependency "simplecov-lcov"
spec.add_development_dependency "pry", "~> 0.14.1"
spec.add_development_dependency "rails", "7.0.8"
spec.add_development_dependency "rails", "~> 7.1"
spec.add_development_dependency "rake", "~> 13.0.6"
spec.add_development_dependency "rspec-rails", "~> 5.1"
spec.add_development_dependency "standard", "~> 1"
Expand Down

0 comments on commit cef6319

Please sign in to comment.