class ActionDispatch::Routing::RouteSet::NamedRouteCollection
named routes.
maintains an anonymous module that can be used to install helpers for the
A NamedRouteCollection instance is a collection of named routes, and also
def add(name, route)
def add(name, route) key = name.to_sym path_name = :"#{name}_path" url_name = :"#{name}_url" if routes.key? key @path_helpers_module.send :undef_method, path_name @url_helpers_module.send :undef_method, url_name end routes[key] = route define_url_helper @path_helpers_module, route, path_name, route.defaults, name, LEGACY define_url_helper @url_helpers_module, route, url_name, route.defaults, name, UNKNOWN @path_helpers << path_name @url_helpers << url_name end
def clear!
def clear! @path_helpers.each do |helper| @path_helpers_module.send :undef_method, helper end @url_helpers.each do |helper| @url_helpers_module.send :undef_method, helper end @routes.clear @path_helpers.clear @url_helpers.clear end
def define_url_helper(mod, route, name, opts, route_key, url_strategy)
foo_url(bar, baz, bang, sort_by: 'baz')
Also allow options hash, so you can do:
foo_url(bar: bar, baz: baz, bang: bang)
Instead of:
foo_url(bar, baz, bang)
with corresponding dynamic segments, so you can do:
Create a url helper allowing ordered parameters to be associated
def define_url_helper(mod, route, name, opts, route_key, url_strategy) helper = UrlHelper.create(route, opts, route_key, url_strategy) mod.module_eval do define_method(name) do |*args| options = nil options = args.pop if args.last.is_a? Hash helper.call self, args, options end end end
def each
def each routes.each { |name, route| yield name, route } self end
def get(name)
def get(name) routes[name.to_sym] end
def helper_names
def helper_names @path_helpers.map(&:to_s) + @url_helpers.map(&:to_s) end
def helpers
def helpers ActiveSupport::Deprecation.warn(<<-MSG.squish) `named_routes.helpers` is deprecated, please use `route_defined?(route_name)` to see if a named route was defined. MSG @path_helpers + @url_helpers end
def initialize
def initialize @routes = {} @path_helpers = Set.new @url_helpers = Set.new @url_helpers_module = Module.new @path_helpers_module = Module.new end
def key?(name)
def key?(name) routes.key? name.to_sym end
def length
def length routes.length end
def names
def names routes.keys end
def path_helpers_module(warn = false)
def path_helpers_module(warn = false) if warn mod = @path_helpers_module helpers = @path_helpers Module.new do include mod helpers.each do |meth| define_method(meth) 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") super(*args, &block) end end end else @path_helpers_module end end
def route_defined?(name)
def route_defined?(name) key = name.to_sym @path_helpers.include?(key) || @url_helpers.include?(key) end