123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActionDispatch::Routing::RouteSet::NamedRouteCollection::UrlHelper
Defined in: actionpack/lib/action_dispatch/routing/route_set.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(route, options, route_name) ⇒ OptimizedUrlHelper

[ GitHub ]

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

def initialize(route, options, route_name)
  super
  @required_parts = @route.required_parts
  @arg_size       = @required_parts.size
end

Instance Attribute Details

#arg_size (readonly)

[ GitHub ]

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

attr_reader :arg_size

Instance Method Details

#call(t, method_name, args, inner_options, url_strategy)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 214

def call(t, method_name, args, inner_options, url_strategy)
  if args.size == arg_size && !inner_options && optimize_routes_generation?(t)
    options = t.url_options.merge @options
    path = optimized_helper(args)
    path << "/" if options[:trailing_slash] && !path.end_with?("/")
    options[:path] = path

    original_script_name = options.delete(:original_script_name)
    script_name = t._routes.find_script_name(options)

    if original_script_name
      script_name = original_script_name + script_name
    end

    options[:script_name] = script_name

    url_strategy.call options
  else
    super
  end
end

#optimize_routes_generation?(t) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 245

def optimize_routes_generation?(t)
  t.send(:optimize_routes_generation?)
end

#optimized_helper(args) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 237

def optimized_helper(args)
  params = parameterize_args(args) do
    raise_generation_error(args)
  end

  @route.format params
end

#parameterize_args(args) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 249

def parameterize_args(args)
  params = {}
  @arg_size.times { |i|
    key = @required_parts[i]
    value = args[i].to_param
    yield key if value.nil? || value.empty?
    params[key] = value
  }
  params
end

#raise_generation_error(args) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 260

def raise_generation_error(args)
  missing_keys = []
  params = parameterize_args(args) { |missing_key|
    missing_keys << missing_key
  }
  constraints = Hash[@route.requirements.merge(params).sort_by { |k, v| k.to_s }]
  message = +"No route matches #{constraints.inspect}"
  message << ", missing required keys: #{missing_keys.sort.inspect}"

  raise ActionController::UrlGenerationError, message
end