Class: ActionDispatch::Routing::RouteSet::NamedRouteCollection
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | actionpack/lib/action_dispatch/routing/route_set.rb |
Overview
A NamedRouteCollection instance is a collection of named routes, and also maintains an anonymous module that can be used to install helpers for the named routes.
Class Method Summary
- .new ⇒ NamedRouteCollection constructor
Instance Attribute Summary
- #path_helpers_module readonly
- #url_helpers_module readonly
- #routes readonly private
::Enumerable
- Included
#many? | Returns |
Instance Method Summary
-
#[](name)
Alias for #get.
-
#[]=(name, route)
Alias for #add.
- #add(name, route) (also: #[]=)
-
#add_url_helper(name, defaults, &block)
Given a
name
, defines name_path and name_url helpers. -
#clear
Alias for #clear!.
- #clear! (also: #clear)
- #each(&block)
- #get(name) (also: #[])
- #helper_names
- #key?(name) ⇒ Boolean
- #length
- #names
- #route_defined?(name) ⇒ Boolean
-
#define_url_helper(mod, name, helper, url_strategy)
private
Create a URL helper allowing ordered parameters to be associated with corresponding dynamic segments, so you can do:
::Enumerable
- Included
#compact_blank | Returns a new |
#exclude? | The negative of the |
#excluding | Returns a copy of the enumerable excluding the specified elements. |
#in_order_of | Returns a new |
#including | Returns a new array that includes the passed elements. |
#index_by | Convert an enumerable to a hash, using the block result as the key and the element as the value. |
#index_with | Convert an enumerable to a hash, using the element as the key and the block result as the value. |
#maximum | Calculates the maximum from the extracted elements. |
#minimum | Calculates the minimum from the extracted elements. |
#pick | Extract the given key from the first element in the enumerable. |
#pluck | Extract the given key from each element in the enumerable. |
#sole | Returns the sole item in the enumerable. |
#without | Alias for Enumerable#excluding. |
#as_json |
::ActiveSupport::EnumerableCoreExt::Constants
- Included
Constructor Details
.new ⇒ NamedRouteCollection
Instance Attribute Details
#path_helpers_module (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 86
attr_reader :routes, :url_helpers_module, :path_helpers_module
#routes (readonly, private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 86
attr_reader :routes, :url_helpers_module, :path_helpers_module
#url_helpers_module (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 86
attr_reader :routes, :url_helpers_module, :path_helpers_module
Instance Method Details
#[](name)
Alias for #get.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 149
alias [] get
#[]=(name, route)
Alias for #add.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 148
alias []= add
#add(name, route) Also known as: #[]=
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 120
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
#add_url_helper(name, defaults, &block)
Given a name
, defines name_path and name_url helpers. Used by ‘direct’, ‘resolve’, and ‘polymorphic’ route helpers.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 167
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
#clear
Alias for #clear!.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 150
alias clear clear!
#clear! Also known as: #clear
[ GitHub ]#define_url_helper(mod, name, helper, url_strategy) (private)
Create a URL helper allowing ordered parameters to be associated with corresponding dynamic segments, so you can do:
foo_url(, baz, bang)
Instead of:
foo_url(bar: , baz: baz, bang: bang)
Also allow options hash, so you can do:
foo_url(, baz, bang, sort_by: 'baz')
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 335
def define_url_helper(mod, name, helper, url_strategy) mod.define_method(name) do |*args| last = args.last = \ case last when Hash args.pop when ActionController::Parameters args.pop.to_h end helper.call(self, name, args, , url_strategy) end end
#each(&block)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 152
def each(&block) routes.each(&block) self end
#get(name) Also known as: #[]
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 139
def get(name) routes[name.to_sym] end
#helper_names
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 102
def helper_names @path_helpers.map(&:to_s) + @url_helpers.map(&:to_s) end
#key?(name) ⇒ Boolean
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 143
def key?(name) return unless name routes.key? name.to_sym end
#length
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 161
def length routes.length end
#names
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 157
def names routes.keys end
#route_defined?(name) ⇒ Boolean
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 97
def route_defined?(name) key = name.to_sym @path_helpers.include?(key) || @url_helpers.include?(key) end