| Class | ActionController::Routing::Optimisation::PositionalArguments |
| In: |
vendor/rails/actionpack/lib/action_controller/routing/optimisations.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/optimisations.rb, line 70
70: def generation_code
71: elements = []
72: idx = 0
73:
74: if kind == :url
75: elements << '#{request.protocol}'
76: elements << '#{request.host_with_port}'
77: end
78:
79: elements << '#{request.relative_url_root if request.relative_url_root}'
80:
81: # The last entry in <tt>route.segments</tt> appears to *always* be a
82: # 'divider segment' for '/' but we have assertions to ensure that
83: # we don't include the trailing slashes, so skip them.
84: (route.segments.size == 1 ? route.segments : route.segments[0..-2]).each do |segment|
85: if segment.is_a?(DynamicSegment)
86: elements << segment.interpolation_chunk("args[#{idx}].to_param")
87: idx += 1
88: else
89: elements << segment.interpolation_chunk
90: end
91: end
92: %("#{elements * ''}")
93: end
# File vendor/rails/actionpack/lib/action_controller/routing/optimisations.rb, line 59
59: def guard_condition
60: number_of_arguments = route.segment_keys.size
61: # if they're using foo_url(:id=>2) it's one
62: # argument, but we don't want to generate /foos/id2
63: if number_of_arguments == 1
64: "(!defined?(default_url_options) || default_url_options.blank?) && defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)"
65: else
66: "(!defined?(default_url_options) || default_url_options.blank?) && defined?(request) && request && args.size == #{number_of_arguments}"
67: end
68: end