diff options
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/CHANGELOG.md | 23 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer.rb | 2 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 27 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/preview.rb | 67 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/railtie.rb | 8 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/version.rb | 2 | ||||
-rw-r--r-- | actionmailer/test/abstract_unit.rb | 3 | ||||
-rw-r--r-- | actionmailer/test/base_test.rb | 21 | ||||
-rw-r--r-- | actionmailer/test/i18n_with_controller_test.rb | 3 |
9 files changed, 152 insertions, 4 deletions
diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index dc8c6bdf74..fc9aefd416 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,3 +1,26 @@ +* Add mailer previews feature based on 37 Signals mail_view gem + + *Andrew White* + +* Calling `mail()` without arguments serves as getter for the current mail + message and keeps previously set headers. + + Example: + + class MailerWithCallback < ActionMailer::Base + after_action :a_callback + + def welcome + mail subject: "subject", to: ["joe@example.com"] + end + + def a_callback + mail # => returns the current mail message + end + end + + *Yves Senn* + * Instrument the generation of Action Mailer messages. The time it takes to generate a message is written to the log. diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 5b6960c8fc..557eec4728 100644 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -41,6 +41,8 @@ module ActionMailer autoload :Base autoload :DeliveryMethods autoload :MailHelper + autoload :Preview + autoload :Previews, 'action_mailer/preview' autoload :TestCase autoload :TestHelper end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 501fee55aa..3c21db991c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -308,6 +308,28 @@ module ActionMailer # Note that unless you have a specific reason to do so, you should prefer using before_action # rather than after_action in your ActionMailer classes so that headers are parsed properly. # + # = Previewing emails + # + # You can preview your email templates visually by adding a mailer preview file to the + # <tt>ActionMailer::Base.preview_path</tt>. Since most emails do something interesting + # with database data, you'll need to write some scenarios to load messages with fake data: + # + # class NotifierPreview < ActionMailer::Preview + # def welcome + # Notifier.welcome(User.first) + # end + # end + # + # Methods must return a Mail::Message object which can be generated by calling the mailer + # method without the additional <tt>deliver</tt>. The location of the mailer previews + # directory can be configured using the <tt>preview_path</tt> option which has a default + # of <tt>test/mailers/previews</tt>: + # + # config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews" + # + # An overview of all previews is accessible at <tt>http://localhost:3000/rails/mailers</tt> + # on a running development server instance. + # # = Configuration options # # These options are specified on the class level, like @@ -362,6 +384,7 @@ module ActionMailer # <tt>delivery_method :test</tt>. Most useful for unit and functional testing. class Base < AbstractController::Base include DeliveryMethods + include Previews abstract! @@ -373,6 +396,8 @@ module ActionMailer include AbstractController::AssetPaths include AbstractController::Callbacks + include ActionView::Layouts + PROTECTED_IVARS = AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [:@_action_has_layout] def _protected_ivars # :nodoc: @@ -689,6 +714,8 @@ module ActionMailer # end # def mail(headers = {}, &block) + return @_message if @_mail_was_called && headers.blank? && !block + @_mail_was_called = true m = @_message diff --git a/actionmailer/lib/action_mailer/preview.rb b/actionmailer/lib/action_mailer/preview.rb new file mode 100644 index 0000000000..43d9ec4bb5 --- /dev/null +++ b/actionmailer/lib/action_mailer/preview.rb @@ -0,0 +1,67 @@ +require 'active_support/descendants_tracker' + +module ActionMailer + module Previews #:nodoc: + extend ActiveSupport::Concern + + included do + # Set the location of mailer previews through app configuration: + # + # config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews" + # + class_attribute :preview_path, instance_writer: false + end + end + + class Preview + extend ActiveSupport::DescendantsTracker + + class << self + # Returns all mailer preview classes + def all + load_previews if descendants.empty? + descendants + end + + # Returns the mail object for the given email name + def call(email) + preview = self.new + preview.public_send(email) + end + + # Returns all of the available email previews + def emails + public_instance_methods(false).map(&:to_s).sort + end + + # Returns true if the email exists + def email_exists?(email) + emails.include?(email) + end + + # Returns true if the preview exists + def exists?(preview) + all.any?{ |p| p.preview_name == preview } + end + + # Find a mailer preview by its underscored class name + def find(preview) + all.find{ |p| p.preview_name == preview } + end + + # Returns the underscored name of the mailer preview without the suffix + def preview_name + name.sub(/Preview$/, '').underscore + end + + protected + def load_previews #:nodoc: + Dir["#{preview_path}/**/*_preview.rb"].each{ |file| require_dependency file } + end + + def preview_path #:nodoc: + Base.preview_path + end + end + end +end diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb index 7677ff3a7c..af8009ba97 100644 --- a/actionmailer/lib/action_mailer/railtie.rb +++ b/actionmailer/lib/action_mailer/railtie.rb @@ -40,5 +40,13 @@ module ActionMailer config.compile_methods! if config.respond_to?(:compile_methods!) end end + + initializer "action_mailer.configure_mailer_previews", before: :set_autoload_paths do |app| + if Rails.env.development? + options = app.config.action_mailer + options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/mailers/previews" : nil + app.config.autoload_paths << options.preview_path + end + end end end diff --git a/actionmailer/lib/action_mailer/version.rb b/actionmailer/lib/action_mailer/version.rb index 9d00091972..46eb763c26 100644 --- a/actionmailer/lib/action_mailer/version.rb +++ b/actionmailer/lib/action_mailer/version.rb @@ -1,7 +1,7 @@ module ActionMailer # Returns the version of the currently loaded ActionMailer as a Gem::Version def self.version - Gem::Version.new "4.1.0.beta" + Gem::Version.new "4.1.0.beta1" end module VERSION #:nodoc: diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index aa18c512c7..cf790c7487 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -20,6 +20,9 @@ ActionMailer::Base.send(:include, ActionView::Layouts) # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Bogus template processors ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect } ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect } diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index b74728ae34..c1759d9b92 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -671,6 +671,27 @@ class BaseTest < ActiveSupport::TestCase assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from end + test "mail() without arguments serves as getter for the current mail message" do + class MailerWithCallback < ActionMailer::Base + after_action :a_callback + + def welcome + headers('X-Special-Header' => 'special indeed!') + mail subject: "subject", body: "hello world", to: ["joe@example.com"] + end + + def a_callback + mail.to << "jane@example.com" + end + end + + mail = MailerWithCallback.welcome + assert_equal "subject", mail.subject + assert_equal ["joe@example.com", "jane@example.com"], mail.to + assert_equal "hello world", mail.body.encoded.strip + assert_equal "special indeed!", mail["X-Special-Header"].to_s + end + protected # Execute the block setting the given values and restoring old values after diff --git a/actionmailer/test/i18n_with_controller_test.rb b/actionmailer/test/i18n_with_controller_test.rb index ab5eaaa9d5..14a1b11b6d 100644 --- a/actionmailer/test/i18n_with_controller_test.rb +++ b/actionmailer/test/i18n_with_controller_test.rb @@ -15,9 +15,6 @@ class I18nTestMailer < ActionMailer::Base end end -# Emulate AV railtie -ActionController::Base.superclass.send(:include, ActionView::Layouts) - class TestController < ActionController::Base def send_mail I18nTestMailer.mail_with_i18n_subject("test@localhost").deliver |