-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48542 from ghiculescu/am-default-form-builder
Introduce `ActionMailer::FormBuilder`
- Loading branch information
Showing
7 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActionMailer | ||
# = Action Mailer Form Builder | ||
# | ||
# Override the default form builder for all views rendered by this | ||
# mailer and any of its descendants. Accepts a subclass of | ||
# ActionView::Helpers::FormBuilder. | ||
# | ||
# While emails typically will not include forms, this can be used | ||
# by views that are shared between controllers and mailers. | ||
# | ||
# For more information, see +ActionController::FormBuilder+. | ||
module FormBuilder | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :_default_form_builder, instance_accessor: false | ||
end | ||
|
||
module ClassMethods | ||
# Set the form builder to be used as the default for all forms | ||
# in the views rendered by this mailer and its subclasses. | ||
# | ||
# ==== Parameters | ||
# * <tt>builder</tt> - Default form builder, an instance of ActionView::Helpers::FormBuilder | ||
def default_form_builder(builder) | ||
self._default_form_builder = builder | ||
end | ||
end | ||
|
||
# Default form builder for the mailer | ||
def default_form_builder | ||
self.class._default_form_builder | ||
end | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
actionmailer/test/fixtures/form_builder_mailer/welcome.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= form_with(url: "/") do |f| %> | ||
<%= f.message %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "abstract_unit" | ||
require "mailers/form_builder_mailer" | ||
|
||
class MailerFormBuilderTest < ActiveSupport::TestCase | ||
def test_default_form_builder_assigned | ||
email = FormBuilderMailer.welcome | ||
assert_includes(email.body.encoded, "hi from SpecializedFormBuilder") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class FormBuilderMailer < ActionMailer::Base | ||
class SpecializedFormBuilder < ActionView::Helpers::FormBuilder | ||
def message | ||
"hi from SpecializedFormBuilder" | ||
end | ||
end | ||
|
||
default_form_builder SpecializedFormBuilder | ||
|
||
def welcome | ||
mail(to: "email@example.com") | ||
end | ||
end |