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

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/routing/route_set.rb

Direct Known Subclasses

OptimizedUrlHelper

Defined Under Namespace

Classes: OptimizedUrlHelper

Constant Summary collapse

DEPRECATED_STRING_OPTIONS =
%w[controller action]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, options, route_name, url_strategy) ⇒ UrlHelper

Returns a new instance of UrlHelper.



263
264
265
266
267
268
269
# File 'lib/action_dispatch/routing/route_set.rb', line 263

def initialize(route, options, route_name, url_strategy)
  @options      = options
  @segment_keys = route.segment_keys.uniq
  @route        = route
  @url_strategy = url_strategy
  @route_name   = route_name
end

Instance Attribute Details

#route_nameObject (readonly)

Returns the value of attribute route_name.



206
207
208
# File 'lib/action_dispatch/routing/route_set.rb', line 206

def route_name
  @route_name
end

#url_strategyObject (readonly)

Returns the value of attribute url_strategy.



206
207
208
# File 'lib/action_dispatch/routing/route_set.rb', line 206

def url_strategy
  @url_strategy
end

Class Method Details

.create(route, options, route_name, url_strategy) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/action_dispatch/routing/route_set.rb', line 194

def self.create(route, options, route_name, url_strategy)
  if optimize_helper?(route)
    OptimizedUrlHelper.new(route, options, route_name, url_strategy)
  else
    new route, options, route_name, url_strategy
  end
end

.optimize_helper?(route) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/action_dispatch/routing/route_set.rb', line 202

def self.optimize_helper?(route)
  !route.glob? && route.path.requirements.empty?
end

Instance Method Details

#call(t, args, inner_options) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/action_dispatch/routing/route_set.rb', line 271

def call(t, args, inner_options)
  controller_options = t.url_options
  options = controller_options.merge @options
  hash = handle_positional_args(controller_options,
                                deprecate_string_options(inner_options) || {},
                                args,
                                options,
                                @segment_keys)

  t._routes.url_for(hash, route_name, url_strategy)
end

#deprecate_string_options(options) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/action_dispatch/routing/route_set.rb', line 310

def deprecate_string_options(options)
  options ||= {}
  deprecated_string_options = options.keys & DEPRECATED_STRING_OPTIONS
  if deprecated_string_options.any?
    msg = "Calling URL helpers with string keys #{deprecated_string_options.join(", ")} is deprecated. Use symbols instead."
    ActiveSupport::Deprecation.warn(msg)
    deprecated_string_options.each do |option|
      value = options.delete(option)
      options[option.to_sym] = value
    end
  end
  options
end

#handle_positional_args(controller_options, inner_options, args, result, path_params) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/action_dispatch/routing/route_set.rb', line 283

def handle_positional_args(controller_options, inner_options, args, result, path_params)
  if args.size > 0
    # take format into account
    if path_params.include?(:format)
      path_params_size = path_params.size - 1
    else
      path_params_size = path_params.size
    end

    if args.size < path_params_size
      path_params -= controller_options.keys
      path_params -= result.keys
    end
    path_params.each { |param|
      value = inner_options.fetch(param) { args.shift }

      unless param == :format && value.nil?
        result[param] = value
      end
    }
  end

  result.merge!(inner_options)
end