Module: Devise::Controllers::UrlHelpers

Defined in:
lib/devise/better_routes.rb

Class Method Summary collapse

Class Method Details

.override_current_resource_helpers!(routes = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/devise/better_routes.rb', line 9

def self.override_current_resource_helpers!(routes = {})
  routes.each do |module_name, actions|
    [:path, :url].each do |path_or_url|
      actions.each do |action|
        action = action ? "#{action}_" : ""
        method = "#{action}#{module_name}_#{path_or_url}"

        class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
          def #{method}(resource_or_scope, *args)
            scope = Devise::Mapping.find_scope!(resource_or_scope)
            current_resource_name = _devise_path_name(scope, "current_\#{scope}")
            _devise_route_context.send("#{action}\#{current_resource_name}_#{module_name}_#{path_or_url}", *args)
          end
        URL_HELPERS
      end
    end
  end
end

.override_registration_helpers!(registration_routes = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/devise/better_routes.rb', line 28

def self.override_registration_helpers!(registration_routes = nil)
  helper_mappings = {
    new_registration: 'new_#{scope}',
    edit_registration: 'edit_#{current_resource_name}',
    cancel_registration: 'cancel_#{current_resource_name}'
  }
  [:path, :url].each do |path_or_url|
    # TODO: use real helper name
    class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
      def registration_#{path_or_url}(resource_or_scope, *args)
        scope = Devise::Mapping.find_scope!(resource_or_scope)
        current_resource_name = _devise_path_name(scope, "current_\#{scope}")
        if respond_to?(:controller_name) && controller_name == _devise_registration_controller(scope, current_resource_name)
          _devise_route_context.send("\#{current_resource_name}_#{path_or_url}", *args)
        else
          _devise_route_context.send("\#{scope.to_s.pluralize}_#{path_or_url}", *args)
        end
      end
    URL_HELPERS
    helper_mappings.each do |old_name, new_name|
      method = "#{old_name}_#{path_or_url}"
      class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
        def #{method}(resource_or_scope, *args)
          scope = Devise::Mapping.find_scope!(resource_or_scope)
          current_resource_name = _devise_path_name(scope, "current_\#{scope}")
          _devise_route_context.send("#{new_name}_#{path_or_url}", *args)
        end
      URL_HELPERS
    end
  end
end

.run_generate_hooks!Object



60
61
62
63
# File 'lib/devise/better_routes.rb', line 60

def self.run_generate_hooks!
  self.override_current_resource_helpers!(Devise::URL_HELPERS.slice(:session, :password))
  self.override_registration_helpers!(Devise::URL_HELPERS[:registration])
end