2007-12-21 08:48:59 +01:00
|
|
|
module ActionController
|
|
|
|
module Routing
|
2008-10-27 07:47:01 +01:00
|
|
|
# Much of the slow performance from routes comes from the
|
2008-05-18 06:22:34 +02:00
|
|
|
# complexity of expiry, <tt>:requirements</tt> matching, defaults providing
|
2008-10-27 07:47:01 +01:00
|
|
|
# and figuring out which url pattern to use. With named routes
|
|
|
|
# we can avoid the expense of finding the right route. So if
|
2007-12-21 08:48:59 +01:00
|
|
|
# they've provided the right number of arguments, and have no
|
2008-05-18 06:22:34 +02:00
|
|
|
# <tt>:requirements</tt>, we can just build up a string and return it.
|
2008-10-27 07:47:01 +01:00
|
|
|
#
|
|
|
|
# To support building optimisations for other common cases, the
|
|
|
|
# generation code is separated into several classes
|
2007-12-21 08:48:59 +01:00
|
|
|
module Optimisation
|
|
|
|
def generate_optimisation_block(route, kind)
|
|
|
|
return "" unless route.optimise?
|
|
|
|
OPTIMISERS.inject("") do |memo, klazz|
|
|
|
|
memo << klazz.new(route, kind).source_code
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Optimiser
|
|
|
|
attr_reader :route, :kind
|
2008-11-24 22:53:39 +01:00
|
|
|
GLOBAL_GUARD_CONDITIONS = [
|
|
|
|
"(!defined?(default_url_options) || default_url_options.blank?)",
|
|
|
|
"(!defined?(controller.default_url_options) || controller.default_url_options.blank?)",
|
|
|
|
"defined?(request)",
|
|
|
|
"request"
|
|
|
|
]
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def initialize(route, kind)
|
|
|
|
@route = route
|
|
|
|
@kind = kind
|
|
|
|
end
|
|
|
|
|
2008-11-24 22:53:39 +01:00
|
|
|
def guard_conditions
|
|
|
|
["false"]
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def generation_code
|
|
|
|
'nil'
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_code
|
|
|
|
if applicable?
|
2008-11-24 22:53:39 +01:00
|
|
|
guard_condition = (GLOBAL_GUARD_CONDITIONS + guard_conditions).join(" && ")
|
2007-12-21 08:48:59 +01:00
|
|
|
"return #{generation_code} if #{guard_condition}\n"
|
|
|
|
else
|
|
|
|
"\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Temporarily disabled <tt>:url</tt> optimisation pending proper solution to
|
2007-12-21 08:48:59 +01:00
|
|
|
# Issues around request.host etc.
|
|
|
|
def applicable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Given a route
|
2007-12-21 08:48:59 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# map.person '/people/:id'
|
|
|
|
#
|
|
|
|
# If the user calls <tt>person_url(@person)</tt>, we can simply
|
2008-10-27 07:47:01 +01:00
|
|
|
# return a string like "/people/#{@person.to_param}"
|
2008-05-18 06:22:34 +02:00
|
|
|
# rather than triggering the expensive logic in +url_for+.
|
2007-12-21 08:48:59 +01:00
|
|
|
class PositionalArguments < Optimiser
|
2008-11-24 22:53:39 +01:00
|
|
|
def guard_conditions
|
2009-02-04 21:26:08 +01:00
|
|
|
number_of_arguments = route.required_segment_keys.size
|
2008-10-27 07:47:01 +01:00
|
|
|
# if they're using foo_url(:id=>2) it's one
|
2007-12-21 08:48:59 +01:00
|
|
|
# argument, but we don't want to generate /foos/id2
|
|
|
|
if number_of_arguments == 1
|
2008-11-24 22:53:39 +01:00
|
|
|
["args.size == 1", "!args.first.is_a?(Hash)"]
|
2007-12-21 08:48:59 +01:00
|
|
|
else
|
2008-11-24 22:53:39 +01:00
|
|
|
["args.size == #{number_of_arguments}"]
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generation_code
|
|
|
|
elements = []
|
|
|
|
idx = 0
|
|
|
|
|
|
|
|
if kind == :url
|
|
|
|
elements << '#{request.protocol}'
|
|
|
|
elements << '#{request.host_with_port}'
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
elements << '#{ActionController::Base.relative_url_root if ActionController::Base.relative_url_root}'
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# The last entry in <tt>route.segments</tt> appears to *always* be a
|
2007-12-21 08:48:59 +01:00
|
|
|
# 'divider segment' for '/' but we have assertions to ensure that
|
|
|
|
# we don't include the trailing slashes, so skip them.
|
|
|
|
(route.segments.size == 1 ? route.segments : route.segments[0..-2]).each do |segment|
|
|
|
|
if segment.is_a?(DynamicSegment)
|
|
|
|
elements << segment.interpolation_chunk("args[#{idx}].to_param")
|
|
|
|
idx += 1
|
|
|
|
else
|
|
|
|
elements << segment.interpolation_chunk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
%("#{elements * ''}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This case is mostly the same as the positional arguments case
|
2008-10-27 07:47:01 +01:00
|
|
|
# above, but it supports additional query parameters as the last
|
2007-12-21 08:48:59 +01:00
|
|
|
# argument
|
|
|
|
class PositionalArgumentsWithAdditionalParams < PositionalArguments
|
2008-11-24 22:53:39 +01:00
|
|
|
def guard_conditions
|
|
|
|
["args.size == #{route.segment_keys.size + 1}"] +
|
|
|
|
UrlRewriter::RESERVED_OPTIONS.collect{ |key| "!args.last.has_key?(:#{key})" }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# This case uses almost the same code as positional arguments,
|
|
|
|
# but add a question mark and args.last.to_query on the end,
|
|
|
|
# unless the last arg is empty
|
2007-12-21 08:48:59 +01:00
|
|
|
def generation_code
|
2008-10-27 07:47:01 +01:00
|
|
|
super.insert(-2, '#{\'?\' + args.last.to_query unless args.last.empty?}')
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# To avoid generating "http://localhost/?host=foo.example.com" we
|
2007-12-21 08:48:59 +01:00
|
|
|
# can't use this optimisation on routes without any segments
|
|
|
|
def applicable?
|
2008-10-27 07:47:01 +01:00
|
|
|
super && route.segment_keys.size > 0
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
OPTIMISERS = [PositionalArguments, PositionalArgumentsWithAdditionalParams]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|