class ActionDispatch::Routing::RouteSet::NamedRouteCollection
:nodoc:
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) routes[name.to_sym] = route define_named_route_methods(name, route) end
def clear!
def clear! @routes = {} @helpers = [] @module ||= Module.new do instance_methods.each { |selector| remove_method(selector) } end end
def define_hash_access(route, name, kind, options)
def define_hash_access(route, name, kind, options) selector = hash_access_name(name, kind) # We use module_eval to avoid leaks @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 def #{selector}(options = nil) # def hash_for_users_url(options = nil) options ? #{options.inspect}.merge(options) : #{options.inspect} # options ? {:only_path=>false}.merge(options) : {:only_path=>false} end # end protected :#{selector} # protected :hash_for_users_url END_EVAL helpers << selector end
def define_named_route_methods(name, route)
def define_named_route_methods(name, route) {:url => {:only_path => false}, :path => {:only_path => true}}.each do |kind, opts| hash = route.defaults.merge(:use_route => name).merge(opts) define_hash_access route, name, kind, hash define_url_helper route, name, kind, hash end end
def define_url_helper(route, name, kind, options)
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(route, name, kind, options) selector = url_helper_name(name, kind) hash_access_method = hash_access_name(name, kind) @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 def #{selector}(*args) options = #{hash_access_method}(args.extract_options!) if args.any? options[:_positional_args] = args options[:_positional_keys] = #{route.segment_keys.inspect} end url_for(options) end END_EVAL helpers << selector 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 hash_access_name(name, kind = :url)
def hash_access_name(name, kind = :url) :"hash_for_#{name}_#{kind}" end
def helper_names
def helper_names self.module.instance_methods.map(&:to_s) end
def initialize
def initialize clear! end
def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false)
def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) reset! if regenerate Array(destinations).each do |dest| dest.__send__(:include, @module) end end
def length
def length routes.length end
def names
def names routes.keys end
def reset!
def reset! old_routes = routes.dup clear! old_routes.each do |name, route| add(name, route) end end
def url_helper_name(name, kind = :url)
def url_helper_name(name, kind = :url) :"#{name}_#{kind}" end