Class | ActionController::Routing::Optimisation::PositionalArguments |
In: |
vendor/rails/actionpack/lib/action_controller/routing_optimisation.rb
|
Parent: | Optimiser |
Given a route: map.person ’/people/:id‘
If the user calls person_url(@person), we can simply return a string like "/people/#{@person.to_param}" rather than triggering the expensive logic in url_for
# File vendor/rails/actionpack/lib/action_controller/routing_optimisation.rb, line 69 69: def generation_code 70: elements = [] 71: idx = 0 72: 73: if kind == :url 74: elements << '#{request.protocol}' 75: elements << '#{request.host_with_port}' 76: end 77: 78: elements << '#{request.relative_url_root if request.relative_url_root}' 79: 80: # The last entry in route.segments appears to # *always* be a 81: # 'divider segment' for '/' but we have assertions to ensure that 82: # we don't include the trailing slashes, so skip them. 83: (route.segments.size == 1 ? route.segments : route.segments[0..-2]).each do |segment| 84: if segment.is_a?(DynamicSegment) 85: elements << segment.interpolation_chunk("args[#{idx}].to_param") 86: idx += 1 87: else 88: elements << segment.interpolation_chunk 89: end 90: end 91: %("#{elements * ''}") 92: end
# File vendor/rails/actionpack/lib/action_controller/routing_optimisation.rb, line 58 58: def guard_condition 59: number_of_arguments = route.segment_keys.size 60: # if they're using foo_url(:id=>2) it's one 61: # argument, but we don't want to generate /foos/id2 62: if number_of_arguments == 1 63: "defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)" 64: else 65: "defined?(request) && request && args.size == #{number_of_arguments}" 66: end 67: end