Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise's registration_path, how it's generated?

Default Devise user signup form looks like this:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

When I run rake routes I don't see any registration prefix, there is user_registration, new_user_registration etc. but not just registration, so how does it work? Where can I find it's source code?

like image 901
Marcin Doliwa Avatar asked Oct 20 '22 21:10

Marcin Doliwa


1 Answers

The registration_path route is generated in Devise::Controllers::UrlHelpers.

As you can see in the code below it just calls your regular routes.

def self.generate_helpers!(routes=nil)
  routes ||= begin
    mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq
    Devise::URL_HELPERS.slice(*mappings)
  end

  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)
            _devise_route_context.send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
          end
        URL_HELPERS
      end
    end
  end
end

generate_helpers!(Devise::URL_HELPERS)

With Devise::URL_HELPERS being:

{:session=>[nil, :new, :destroy], :omniauth_callback=>[], :password=>[nil, :new, :edit], :registration=>[nil, :new, :edit, :cancel], :confirmation=>[nil, :new], :unlock=>[nil, :new]}
like image 75
joost Avatar answered Oct 30 '22 16:10

joost