aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch
diff options
context:
space:
mode:
author@schneems and @sgrif <sean@thoughtbot.com>2014-06-19 17:26:29 -0500
committerschneems <richard.schneeman@gmail.com>2014-07-30 12:01:45 -0500
commit2bbcca004cc232cef868cd0e301f274ce5638df0 (patch)
treec0b460531e5460ab32fe9fb354f52c697405c648 /actionpack/lib/action_dispatch
parent4efb36e7b44ae3facb948aa3c5f2790a3fd3b61a (diff)
downloadrails-2bbcca004cc232cef868cd0e301f274ce5638df0.tar.gz
rails-2bbcca004cc232cef868cd0e301f274ce5638df0.tar.bz2
rails-2bbcca004cc232cef868cd0e301f274ce5638df0.zip
Deprecate `*_path` methods in mailers
Email does not support relative links since there is no implicit host. Therefore all links inside of emails must be fully qualified URLs. All path helpers are now deprecated. When removed, the error will give early indication to developers to use `*_url` methods instead. Currently if a developer uses a `*_path` helper, their tests and `mail_view` will not catch the mistake. The only way to see the error is by sending emails in production. Preventing sending out emails with non-working path's is the desired end goal of this PR. Currently path helpers are mixed-in to controllers (the ActionMailer::Base acts as a controller). All `*_url` and `*_path` helpers are made available through the same module. This PR separates this behavior into two modules so we can extend the `*_path` methods to add a Deprecation to them. Once deprecated we can use this same area to raise a NoMethodError and add an informative message directing the developer to use `*_url` instead. The module with warnings is only mixed in when a controller returns false from the newly added `supports_relative_path?`. Paired @sgrif & @schneems
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb120
1 files changed, 72 insertions, 48 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index d869b62398..0906c67719 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -86,12 +86,13 @@ module ActionDispatch
# named routes.
class NamedRouteCollection #:nodoc:
include Enumerable
- attr_reader :routes, :helpers, :module
+ attr_reader :routes, :helpers, :url_helpers_module
def initialize
@routes = {}
@helpers = Set.new
- @module = Module.new
+ @url_helpers_module = Module.new
+ @path_helpers_module = Module.new
end
def route_defined?(name)
@@ -104,7 +105,11 @@ module ActionDispatch
def clear!
@helpers.each do |helper|
- @module.send :undef_method, helper
+ if helper =~ /_path$/
+ @path_helpers_module.send :undef_method, helper
+ else
+ @url_helpers_module.send :undef_method, helper
+ end
end
@routes.clear
@@ -114,10 +119,12 @@ module ActionDispatch
def add(name, route)
key = name.to_sym
if routes.key? key
- undef_named_route_methods @module, name
+ @path_helpers_module.send :undef_method, :"#{name}_path"
+ @url_helpers_module.send :undef_method, :"#{name}_url"
end
routes[key] = route
- define_named_route_methods(@module, name, route)
+ define_url_helper @path_helpers_module, route, :"#{name}_path", route.defaults, name, PATH
+ define_url_helper @url_helpers_module, route, :"#{name}_url", route.defaults, name, FULL
end
def get(name)
@@ -141,6 +148,26 @@ module ActionDispatch
routes.length
end
+ def path_helpers_module(warn = false)
+ if warn
+ mod = @path_helpers_module
+ Module.new do
+ include mod
+
+ mod.instance_methods(false).each do |meth|
+ define_method("#{meth}_with_warning") do |*args, &block|
+ ActiveSupport::Deprecation.warn("The method `#{meth}` cannot be used here as a full URL is required. Use `#{meth.to_s.sub(/_path$/, '_url')}` instead")
+ send("#{meth}_without_warning", *args, &block)
+ end
+
+ alias_method_chain meth, :warning
+ end
+ end
+ else
+ @path_helpers_module
+ end
+ end
+
class UrlHelper # :nodoc:
def self.create(route, options, route_name, url_strategy)
if optimize_helper?(route)
@@ -263,7 +290,7 @@ module ActionDispatch
#
def define_url_helper(mod, route, name, opts, route_key, url_strategy)
helper = UrlHelper.create(route, opts, route_key, url_strategy)
-
+ mod.remove_possible_method name
mod.module_eval do
define_method(name) do |*args|
options = nil
@@ -274,16 +301,6 @@ module ActionDispatch
helpers << name
end
-
- def define_named_route_methods(mod, name, route)
- define_url_helper mod, route, :"#{name}_path", route.defaults, name, PATH
- define_url_helper mod, route, :"#{name}_url", route.defaults, name, FULL
- end
-
- def undef_named_route_methods(mod, name)
- mod.send :undef_method, :"#{name}_path"
- mod.send :undef_method, :"#{name}_url"
- end
end
# :stopdoc:
@@ -396,44 +413,51 @@ module ActionDispatch
RUBY
end
- def url_helpers
- @url_helpers ||= begin
- routes = self
-
- Module.new do
- extend ActiveSupport::Concern
- include UrlFor
-
- # Define url_for in the singleton level so one can do:
- # Rails.application.routes.url_helpers.url_for(args)
- @_routes = routes
- class << self
- delegate :url_for, :optimize_routes_generation?, :to => '@_routes'
- attr_reader :_routes
- def url_options; {}; end
- end
+ def url_helpers(include_path_helpers = true)
+ routes = self
- route_methods = routes.named_routes.module
+ Module.new do
+ extend ActiveSupport::Concern
+ include UrlFor
+
+ # Define url_for in the singleton level so one can do:
+ # Rails.application.routes.url_helpers.url_for(args)
+ @_routes = routes
+ class << self
+ delegate :url_for, :optimize_routes_generation?, to: '@_routes'
+ attr_reader :_routes
+ def url_options; {}; end
+ end
- # Make named_routes available in the module singleton
- # as well, so one can do:
- # Rails.application.routes.url_helpers.posts_path
- extend route_methods
+ route_methods = routes.named_routes.url_helpers_module
- # Any class that includes this module will get all
- # named routes...
- include route_methods
+ # Make named_routes available in the module singleton
+ # as well, so one can do:
+ # Rails.application.routes.url_helpers.posts_path
+ extend route_methods
- # plus a singleton class method called _routes ...
- included do
- singleton_class.send(:redefine_method, :_routes) { routes }
- end
+ # Any class that includes this module will get all
+ # named routes...
+ include route_methods
- # And an instance method _routes. Note that
- # UrlFor (included in this module) add extra
- # conveniences for working with @_routes.
- define_method(:_routes) { @_routes || routes }
+ if include_path_helpers
+ path_helpers = routes.named_routes.path_helpers_module
+ else
+ path_helpers = routes.named_routes.path_helpers_module(true)
end
+
+ include path_helpers
+ extend path_helpers
+
+ # plus a singleton class method called _routes ...
+ included do
+ singleton_class.send(:redefine_method, :_routes) { routes }
+ end
+
+ # And an instance method _routes. Note that
+ # UrlFor (included in this module) add extra
+ # conveniences for working with @_routes.
+ define_method(:_routes) { @_routes || routes }
end
end