Skip to content

Commit

Permalink
Add support for multiple preview_path
Browse files Browse the repository at this point in the history
  • Loading branch information
camelmasa committed Sep 16, 2014
1 parent b816f9f commit 4f89fde
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
4 changes: 1 addition & 3 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -335,9 +335,7 @@ module ActionMailer
# Methods must return a <tt>Mail::Message</tt> object which can be generated by calling the mailer
# method without the additional <tt>deliver_now</tt> / <tt>deliver_later</tt>. The location of the
# mailer previews directory can be configured using the <tt>preview_paths</tt> option which has a default
# of <tt>test/mailers/previews</tt>:
#
# config.action_mailer.preview_paths = "#{Rails.root}/lib/mailer_previews"
# of <tt>test/mailers/previews</tt>
#
# An overview of all previews is accessible at <tt>http://localhost:3000/rails/mailers</tt>
# on a running development server instance.
Expand Down
6 changes: 3 additions & 3 deletions actionmailer/lib/action_mailer/preview.rb
Expand Up @@ -5,9 +5,9 @@ module Previews #:nodoc:
extend ActiveSupport::Concern

included do
# Set the location of mailer previews through app configuration:
# Add the location of mailer previews through app configuration:
#
# config.action_mailer.preview_paths = "#{Rails.root}/lib/mailer_previews"
# config.action_mailer.preview_paths << "#{Rails.root}/lib/mailer_previews"
#
mattr_accessor :preview_paths, instance_writer: false

Expand Down Expand Up @@ -96,7 +96,7 @@ def preview_name
protected
def load_previews #:nodoc:
if preview_paths
Dir["#{preview_paths}/**/*_preview.rb"].each{ |file| require_dependency file }
preview_paths.existent.each{ |file| require_dependency file }
end
end

Expand Down
8 changes: 1 addition & 7 deletions actionmailer/lib/action_mailer/railtie.rb
Expand Up @@ -22,7 +22,7 @@ class Railtie < Rails::Railtie # :nodoc:
options.show_previews = Rails.env.development? if options.show_previews.nil?

if options.show_previews
options.preview_paths ||= defined?(Rails.root) ? "#{Rails.root}/test/mailers/previews" : nil
options.preview_paths ||= paths["test/mailers/previews"]
end

# make sure readers methods get compiled
Expand Down Expand Up @@ -54,11 +54,5 @@ class Railtie < Rails::Railtie # :nodoc:
config.compile_methods! if config.respond_to?(:compile_methods!)
end
end

config.after_initialize do
if ActionMailer::Base.preview_paths
ActiveSupport::Dependencies.autoload_paths << ActionMailer::Base.preview_paths
end
end
end
end
18 changes: 9 additions & 9 deletions actionpack/test/controller/resources_test.rb
Expand Up @@ -346,13 +346,13 @@ def test_with_new_action
end

preview_options = {:action => 'preview'}
preview_paths = "/messages/new/preview"
preview_path = "/messages/new/preview"
assert_restful_routes_for :messages do |options|
assert_recognizes(options.merge(preview_options), :path => preview_paths, :method => :post)
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
end

assert_restful_named_routes_for :messages do |options|
assert_named_route preview_paths, :preview_new_message_path, preview_options
assert_named_route preview_path, :preview_new_message_path, preview_options
end
end
end
Expand All @@ -368,13 +368,13 @@ def test_with_new_action_with_name_prefix
end

preview_options = {:action => 'preview', :thread_id => '1'}
preview_paths = "/threads/1/messages/new/preview"
preview_path = "/threads/1/messages/new/preview"
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
assert_recognizes(options.merge(preview_options), :path => preview_paths, :method => :post)
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
end

assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
assert_named_route preview_paths, :preview_new_thread_message_path, preview_options
assert_named_route preview_path, :preview_new_thread_message_path, preview_options
end
end
end
Expand All @@ -390,13 +390,13 @@ def test_with_formatted_new_action_with_name_prefix
end

preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
preview_paths = "/threads/1/messages/new/preview.xml"
preview_path = "/threads/1/messages/new/preview.xml"
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
assert_recognizes(options.merge(preview_options), :path => preview_paths, :method => :post)
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
end

assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
assert_named_route preview_paths, :preview_new_thread_message_path, preview_options
assert_named_route preview_path, :preview_new_thread_message_path, preview_options
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions guides/source/configuring.md
Expand Up @@ -453,10 +453,10 @@ There are a number of settings available on `config.action_mailer`:
config.action_mailer.interceptors = ["MailInterceptor"]
```

* `config.action_mailer.preview_paths` specifies the location of mailer previews.
* `config.action_mailer.preview_paths` adds the location of mailer previews.

```ruby
config.action_mailer.preview_paths = "#{Rails.root}/lib/mailer_previews"
config.action_mailer.preview_paths << "#{Rails.root}/lib/mailer_previews"
```

* `config.action_mailer.show_previews` enable or disable mailer previews. By default this is `true` in development.
Expand Down
2 changes: 2 additions & 0 deletions railties/lib/rails/engine/configuration.rb
Expand Up @@ -67,6 +67,8 @@ def paths
paths.add "vendor", load_path: true
paths.add "vendor/assets", glob: "*"

paths.add "test/mailers/previews", glob: "**/*_preview.rb"

paths
end
end
Expand Down
36 changes: 36 additions & 0 deletions railties/test/application/mailer_previews_test.rb
Expand Up @@ -102,6 +102,42 @@ def foo
assert_match '<li><a href="/rails/mailers/notifier/foo">foo</a></li>', last_response.body
end

test "mailer previews are loaded from a custom preview_path when it's multiple" do
add_to_config "config.action_mailer.preview_path = ['#{app_path}/lib/notifier_previews', '#{app_path}/lib/confirm_previews']"

['notifier', 'confirm'].each do |keyword|
mailer keyword, <<-RUBY
class #{keyword.camelize} < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY

text_template "#{keyword}/foo", <<-RUBY
Hello, World!
RUBY

app_file "lib/#{keyword}_previews/notifier_preview.rb", <<-RUBY
class #{keyword.camelize}Preview < ActionMailer::Preview
def foo
#{keyword.camelize}.foo
end
end
RUBY
end

app('development')

get "/rails/mailers"
assert_match '<h3><a href="/rails/mailers/notifier">Notifier</a></h3>', last_response.body
assert_match '<li><a href="/rails/mailers/notifier/foo">foo</a></li>', last_response.body
assert_match '<h3><a href="/rails/mailers/confirm">Confirm</a></h3>', last_response.body
assert_match '<li><a href="/rails/mailers/confirm/foo">foo</a></li>', last_response.body
end

test "mailer previews are reloaded across requests" do
app('development')

Expand Down

0 comments on commit 4f89fde

Please sign in to comment.