Class: ActionDispatch::Routing::RouteSet::NamedRouteCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
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.

Defined Under Namespace

Classes: UrlHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNamedRouteCollection

Returns a new instance of NamedRouteCollection.



95
96
97
98
99
100
101
# File 'lib/action_dispatch/routing/route_set.rb', line 95

def initialize
  @routes  = {}
  @path_helpers = Set.new
  @url_helpers = Set.new
  @url_helpers_module  = Module.new
  @path_helpers_module = Module.new
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



93
94
95
# File 'lib/action_dispatch/routing/route_set.rb', line 93

def routes
  @routes
end

#url_helpers_moduleObject (readonly)

Returns the value of attribute url_helpers_module.



93
94
95
# File 'lib/action_dispatch/routing/route_set.rb', line 93

def url_helpers_module
  @url_helpers_module
end

Instance Method Details

#add(name, route) ⇒ Object Also known as: []=



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/action_dispatch/routing/route_set.rb', line 134

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

#clear!Object Also known as: clear



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/action_dispatch/routing/route_set.rb', line 120

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

#eachObject



163
164
165
166
# File 'lib/action_dispatch/routing/route_set.rb', line 163

def each
  routes.each { |name, route| yield name, route }
  self
end

#get(name) ⇒ Object Also known as: []



151
152
153
# File 'lib/action_dispatch/routing/route_set.rb', line 151

def get(name)
  routes[name.to_sym]
end

#helper_namesObject



116
117
118
# File 'lib/action_dispatch/routing/route_set.rb', line 116

def helper_names
  @path_helpers.map(&:to_s) + @url_helpers.map(&:to_s)
end

#helpersObject



108
109
110
111
112
113
114
# File 'lib/action_dispatch/routing/route_set.rb', line 108

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/action_dispatch/routing/route_set.rb', line 155

def key?(name)
  routes.key? name.to_sym
end

#lengthObject



172
173
174
# File 'lib/action_dispatch/routing/route_set.rb', line 172

def length
  routes.length
end

#namesObject



168
169
170
# File 'lib/action_dispatch/routing/route_set.rb', line 168

def names
  routes.keys
end

#path_helpers_module(warn = false) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/action_dispatch/routing/route_set.rb', line 176

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

#route_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/action_dispatch/routing/route_set.rb', line 103

def route_defined?(name)
  key = name.to_sym
  @path_helpers.include?(key) || @url_helpers.include?(key)
end