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.undef_method path_name @url_helpers_module.undef_method url_name end routes[key] = route helper = UrlHelper.create(route, route.defaults, name) define_url_helper @path_helpers_module, path_name, helper, PATH define_url_helper @url_helpers_module, url_name, helper, UNKNOWN @path_helpers << path_name @url_helpers << url_name end
def add_url_helper(name, defaults, &block)
Given a +name+, defines name_path and name_url helpers.
def add_url_helper(name, defaults, &block) helper = CustomUrlHelper.new(name, defaults, &block) path_name = :"#{name}_path" url_name = :"#{name}_url" @path_helpers_module.module_eval do redefine_method(path_name) do |*args| helper.call(self, args, true) end end @url_helpers_module.module_eval do redefine_method(url_name) do |*args| helper.call(self, args, false) end end @path_helpers << path_name @url_helpers << url_name self end
def clear!
def clear! @path_helpers.each do |helper| @path_helpers_module.remove_method helper end @url_helpers.each do |helper| @url_helpers_module.remove_method helper end @routes.clear @path_helpers.clear @url_helpers.clear end
def define_url_helper(mod, name, helper, 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, name, helper, url_strategy) mod.define_method(name) do |*args| last = args.last options = \ case last when Hash args.pop when ActionController::Parameters args.pop.to_h end helper.call(self, name, args, options, url_strategy) end end
def each(&block)
def each(&block) routes.each(&block) 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 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) return unless name routes.key? name.to_sym end
def length
def length routes.length end
def names
def names routes.keys end
def route_defined?(name)
def route_defined?(name) key = name.to_sym @path_helpers.include?(key) || @url_helpers.include?(key) end