Skip to content

Commit

Permalink
Fix regression issue with preview_path=
Browse files Browse the repository at this point in the history
The `preview_path` feature of `ActionMailer` was added in Rails 4.1.
Versions before that release are not able to handle that configuration.
The regression issue occurred in part to the spec suite overlooking
changes to the environment and how that affects the shelling out.

Additionally, the previous changes noted that the configuration for a
particular module always responds to a setting; even when it should not.
However, while this logic was noted for `show_previews`, a few lines
further down, that same logic was overlooked for `preview_path`.

It appears the only available methods for detecting `ActionMailer`, and
it's features, is tasting the top-level `config` in addition to checking
the Rails version string.
  • Loading branch information
cupakromer committed Jun 5, 2015
1 parent 88b3823 commit df2b12b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/rspec-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@ class Railtie < ::Rails::Railtie
private

def setup_preview_path(app)
# If the action mailer railtie isn't loaded the config will not respond
return unless supports_action_mailer_previews?(app.config)
options = app.config.action_mailer
config_default_preview_path(options) if config_preview_path?(options)
end

def config_preview_path?(options)
# This string version check avoids loading the ActionMailer class, as
# would happen using `defined?`. This is necessary because the
# ActionMailer class only loads it's settings once, at load time. If we
# load the class now any settings declared in a config block in an
# initializer will be ignored.
#
# We cannot use `respond_to?(:show_previews)` here as it will always
# return `true`.
if ::Rails::VERSION::STRING < '4.2'
Expand All @@ -59,8 +52,18 @@ def config_default_preview_path(options)
end

def supports_action_mailer_previews?(config)
config.respond_to?(:action_mailer) &&
config.action_mailer.respond_to?(:preview_path)
# These checks avoid loading `ActionMailer`. Using `defined?` has the
# side-effect of the class getting loaded if it is available. This is
# problematic because loading `ActionMailer::Base` will cause it to
# read the config settings; this is the only time the config is read.
# If the config is loaded now, any settings declared in a config block
# in an initializer will be ignored.
#
# If the action mailer railtie has not been loaded then `config` will
# not respond to the method. However, we cannot use
# `config.action_mailer.respond_to?(:preview_path)` here as it will
# always return `true`.
config.respond_to?(:action_mailer) && ::Rails::VERSION::STRING > '4.1'
end
end
end
Expand Down

0 comments on commit df2b12b

Please sign in to comment.