2009-10-09 20:11:58 -04:00
|
|
|
module Devise
|
|
|
|
module Controllers
|
2009-10-18 10:54:21 -04:00
|
|
|
# Create url helpers to be used with resource/scope configuration. Acts as
|
|
|
|
# proxies to the generated routes created by devise.
|
|
|
|
# Resource param can be a string or symbol, a class, or an instance object.
|
|
|
|
# Example using a :user resource:
|
|
|
|
#
|
|
|
|
# new_session_path(:user) => new_user_session_path
|
|
|
|
# session_path(:user) => user_session_path
|
|
|
|
# destroy_session_path(:user) => destroy_user_session_path
|
|
|
|
#
|
|
|
|
# new_password_path(:user) => new_user_password_path
|
|
|
|
# password_path(:user) => user_password_path
|
|
|
|
# edit_password_path(:user) => edit_user_password_path
|
|
|
|
#
|
|
|
|
# new_confirmation_path(:user) => new_user_confirmation_path
|
|
|
|
# confirmation_path(:user) => user_confirmation_path
|
2009-10-27 19:26:40 -04:00
|
|
|
#
|
2012-01-03 14:32:51 -05:00
|
|
|
# Those helpers are included by default to ActionController::Base.
|
|
|
|
#
|
|
|
|
# In case you want to add such helpers to another class, you can do
|
|
|
|
# that as long as this new class includes both url_helpers and
|
|
|
|
# mounted_helpers. Example:
|
|
|
|
#
|
|
|
|
# include Rails.application.routes.url_helpers
|
|
|
|
# include Rails.application.routes.mounted_helpers
|
|
|
|
#
|
2009-10-09 20:11:58 -04:00
|
|
|
module UrlHelpers
|
2011-08-29 08:29:38 -04:00
|
|
|
def self.remove_helpers!
|
|
|
|
self.instance_methods.map(&:to_s).grep(/_(url|path)$/).each do |method|
|
|
|
|
remove_method method
|
|
|
|
end
|
|
|
|
end
|
2009-10-09 20:11:58 -04:00
|
|
|
|
2011-10-09 05:50:48 -04:00
|
|
|
def self.generate_helpers!(routes=nil)
|
|
|
|
routes ||= begin
|
|
|
|
mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq
|
|
|
|
Devise::URL_HELPERS.slice(*mappings)
|
|
|
|
end
|
2010-07-13 04:09:55 -04:00
|
|
|
|
2011-08-29 08:29:38 -04:00
|
|
|
routes.each do |module_name, actions|
|
|
|
|
[:path, :url].each do |path_or_url|
|
|
|
|
actions.each do |action|
|
|
|
|
action = action ? "#{action}_" : ""
|
2015-05-12 15:05:31 -04:00
|
|
|
method = :"#{action}#{module_name}_#{path_or_url}"
|
2011-08-29 08:29:38 -04:00
|
|
|
|
2015-05-12 15:05:31 -04:00
|
|
|
define_method method do |resource_or_scope, *args|
|
|
|
|
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
|
|
router_name = Devise.mappings[scope].router_name
|
|
|
|
context = router_name ? send(router_name) : _devise_route_context
|
|
|
|
context.send("#{action}#{scope}_#{module_name}_#{path_or_url}", *args)
|
|
|
|
end
|
2011-08-29 08:29:38 -04:00
|
|
|
end
|
2009-10-09 20:11:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-09 05:50:48 -04:00
|
|
|
|
|
|
|
generate_helpers!(Devise::URL_HELPERS)
|
2012-01-02 16:42:38 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def _devise_route_context
|
2012-02-15 11:13:55 -05:00
|
|
|
@_devise_route_context ||= send(Devise.available_router_name)
|
2012-01-02 16:42:38 -05:00
|
|
|
end
|
2009-10-09 20:11:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|