| 1 |
module ActionController |
|---|
| 2 |
module Routing |
|---|
| 3 |
class RouteSet |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class Mapper |
|---|
| 10 |
def initialize(set) |
|---|
| 11 |
@set = set |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
def connect(path, options = {}) |
|---|
| 17 |
@set.add_route(path, options) |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
def root(options = {}) |
|---|
| 22 |
if options.is_a?(Symbol) |
|---|
| 23 |
if source_route = @set.named_routes.routes[options] |
|---|
| 24 |
options = source_route.defaults.merge({ :conditions => source_route.conditions }) |
|---|
| 25 |
end |
|---|
| 26 |
end |
|---|
| 27 |
named_route("root", '', options) |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
def named_route(name, path, options = {}) |
|---|
| 31 |
@set.add_named_route(name, path, options) |
|---|
| 32 |
end |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
def namespace(name, options = {}, &block) |
|---|
| 46 |
if options[:namespace] |
|---|
| 47 |
with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block) |
|---|
| 48 |
else |
|---|
| 49 |
with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block) |
|---|
| 50 |
end |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
def method_missing(route_name, *args, &proc) |
|---|
| 54 |
super unless args.length >= 1 && proc.nil? |
|---|
| 55 |
@set.add_named_route(route_name, *args) |
|---|
| 56 |
end |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
class NamedRouteCollection |
|---|
| 63 |
include Enumerable |
|---|
| 64 |
include ActionController::Routing::Optimisation |
|---|
| 65 |
attr_reader :routes, :helpers |
|---|
| 66 |
|
|---|
| 67 |
def initialize |
|---|
| 68 |
clear! |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
def clear! |
|---|
| 72 |
@routes = {} |
|---|
| 73 |
@helpers = [] |
|---|
| 74 |
|
|---|
| 75 |
@module ||= Module.new |
|---|
| 76 |
@module.instance_methods.each do |selector| |
|---|
| 77 |
@module.class_eval { remove_method selector } |
|---|
| 78 |
end |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
def add(name, route) |
|---|
| 82 |
routes[name.to_sym] = route |
|---|
| 83 |
define_named_route_methods(name, route) |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
def get(name) |
|---|
| 87 |
routes[name.to_sym] |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
alias []= add |
|---|
| 91 |
alias [] get |
|---|
| 92 |
alias clear clear! |
|---|
| 93 |
|
|---|
| 94 |
def each |
|---|
| 95 |
routes.each { |name, route| yield name, route } |
|---|
| 96 |
self |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
def names |
|---|
| 100 |
routes.keys |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
def length |
|---|
| 104 |
routes.length |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
def reset! |
|---|
| 108 |
old_routes = routes.dup |
|---|
| 109 |
clear! |
|---|
| 110 |
old_routes.each do |name, route| |
|---|
| 111 |
add(name, route) |
|---|
| 112 |
end |
|---|
| 113 |
end |
|---|
| 114 |
|
|---|
| 115 |
def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) |
|---|
| 116 |
reset! if regenerate |
|---|
| 117 |
Array(destinations).each do |dest| |
|---|
| 118 |
dest.send! :include, @module |
|---|
| 119 |
end |
|---|
| 120 |
end |
|---|
| 121 |
|
|---|
| 122 |
private |
|---|
| 123 |
def url_helper_name(name, kind = :url) |
|---|
| 124 |
:"#{name}_#{kind}" |
|---|
| 125 |
end |
|---|
| 126 |
|
|---|
| 127 |
def hash_access_name(name, kind = :url) |
|---|
| 128 |
:"hash_for_#{name}_#{kind}" |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
def define_named_route_methods(name, route) |
|---|
| 132 |
{:url => {:only_path => false}, :path => {:only_path => true}}.each do |kind, opts| |
|---|
| 133 |
hash = route.defaults.merge(:use_route => name).merge(opts) |
|---|
| 134 |
define_hash_access route, name, kind, hash |
|---|
| 135 |
define_url_helper route, name, kind, hash |
|---|
| 136 |
end |
|---|
| 137 |
end |
|---|
| 138 |
|
|---|
| 139 |
def define_hash_access(route, name, kind, options) |
|---|
| 140 |
selector = hash_access_name(name, kind) |
|---|
| 141 |
@module.module_eval <<-end_eval |
|---|
| 142 |
def |
|---|
| 143 |
options ? |
|---|
| 144 |
end |
|---|
| 145 |
protected : |
|---|
| 146 |
end_eval |
|---|
| 147 |
helpers << selector |
|---|
| 148 |
end |
|---|
| 149 |
|
|---|
| 150 |
def define_url_helper(route, name, kind, options) |
|---|
| 151 |
selector = url_helper_name(name, kind) |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
hash_access_method = hash_access_name(name, kind) |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
@module.module_eval <<-end_eval |
|---|
| 170 |
def |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
opts = if args.empty? || Hash === args.first |
|---|
| 174 |
args.first || {} |
|---|
| 175 |
else |
|---|
| 176 |
options = args.extract_options! |
|---|
| 177 |
args = args.zip( |
|---|
| 178 |
h[k] = v |
|---|
| 179 |
h |
|---|
| 180 |
end |
|---|
| 181 |
options.merge(args) |
|---|
| 182 |
end |
|---|
| 183 |
|
|---|
| 184 |
url_for( |
|---|
| 185 |
end |
|---|
| 186 |
protected : |
|---|
| 187 |
end_eval |
|---|
| 188 |
helpers << selector |
|---|
| 189 |
end |
|---|
| 190 |
end |
|---|
| 191 |
|
|---|
| 192 |
attr_accessor :routes, :named_routes |
|---|
| 193 |
|
|---|
| 194 |
def initialize |
|---|
| 195 |
self.routes = [] |
|---|
| 196 |
self.named_routes = NamedRouteCollection.new |
|---|
| 197 |
end |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
def builder |
|---|
| 202 |
@builder ||= RouteBuilder.new |
|---|
| 203 |
end |
|---|
| 204 |
|
|---|
| 205 |
def draw |
|---|
| 206 |
clear! |
|---|
| 207 |
yield Mapper.new(self) |
|---|
| 208 |
install_helpers |
|---|
| 209 |
end |
|---|
| 210 |
|
|---|
| 211 |
def clear! |
|---|
| 212 |
routes.clear |
|---|
| 213 |
named_routes.clear |
|---|
| 214 |
@combined_regexp = nil |
|---|
| 215 |
@routes_by_controller = nil |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
@compiled_recognize_optimized = nil |
|---|
| 219 |
end |
|---|
| 220 |
|
|---|
| 221 |
def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) |
|---|
| 222 |
Array(destinations).each { |d| d.module_eval { include Helpers } } |
|---|
| 223 |
named_routes.install(destinations, regenerate_code) |
|---|
| 224 |
end |
|---|
| 225 |
|
|---|
| 226 |
def empty? |
|---|
| 227 |
routes.empty? |
|---|
| 228 |
end |
|---|
| 229 |
|
|---|
| 230 |
def load! |
|---|
| 231 |
Routing.use_controllers! nil |
|---|
| 232 |
clear! |
|---|
| 233 |
load_routes! |
|---|
| 234 |
install_helpers |
|---|
| 235 |
end |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
alias reload! load! |
|---|
| 239 |
|
|---|
| 240 |
def reload |
|---|
| 241 |
if @routes_last_modified && defined?(RAILS_ROOT) |
|---|
| 242 |
mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime |
|---|
| 243 |
|
|---|
| 244 |
return if mtime == @routes_last_modified |
|---|
| 245 |
|
|---|
| 246 |
@routes_last_modified = mtime |
|---|
| 247 |
end |
|---|
| 248 |
load! |
|---|
| 249 |
end |
|---|
| 250 |
|
|---|
| 251 |
def load_routes! |
|---|
| 252 |
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes |
|---|
| 253 |
load File.join("#{RAILS_ROOT}/config/routes.rb") |
|---|
| 254 |
@routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime |
|---|
| 255 |
else |
|---|
| 256 |
add_route ":controller/:action/:id" |
|---|
| 257 |
end |
|---|
| 258 |
end |
|---|
| 259 |
|
|---|
| 260 |
def add_route(path, options = {}) |
|---|
| 261 |
route = builder.build(path, options) |
|---|
| 262 |
routes << route |
|---|
| 263 |
route |
|---|
| 264 |
end |
|---|
| 265 |
|
|---|
| 266 |
def add_named_route(name, path, options = {}) |
|---|
| 267 |
|
|---|
| 268 |
name = options[:name_prefix] + name.to_s if options[:name_prefix] |
|---|
| 269 |
named_routes[name.to_sym] = add_route(path, options) |
|---|
| 270 |
end |
|---|
| 271 |
|
|---|
| 272 |
def options_as_params(options) |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
options_as_params = options.clone |
|---|
| 285 |
options_as_params[:action] ||= 'index' if options[:controller] |
|---|
| 286 |
options_as_params[:action] = options_as_params[:action].to_s if options_as_params[:action] |
|---|
| 287 |
options_as_params |
|---|
| 288 |
end |
|---|
| 289 |
|
|---|
| 290 |
def build_expiry(options, recall) |
|---|
| 291 |
recall.inject({}) do |expiry, (key, recalled_value)| |
|---|
| 292 |
expiry[key] = (options.key?(key) && options[key].to_param != recalled_value.to_param) |
|---|
| 293 |
expiry |
|---|
| 294 |
end |
|---|
| 295 |
end |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
def extra_keys(options, recall={}) |
|---|
| 300 |
generate_extras(options, recall).last |
|---|
| 301 |
end |
|---|
| 302 |
|
|---|
| 303 |
def generate_extras(options, recall={}) |
|---|
| 304 |
generate(options, recall, :generate_extras) |
|---|
| 305 |
end |
|---|
| 306 |
|
|---|
| 307 |
def generate(options, recall = {}, method=:generate) |
|---|
| 308 |
named_route_name = options.delete(:use_route) |
|---|
| 309 |
generate_all = options.delete(:generate_all) |
|---|
| 310 |
if named_route_name |
|---|
| 311 |
named_route = named_routes[named_route_name] |
|---|
| 312 |
options = named_route.parameter_shell.merge(options) |
|---|
| 313 |
end |
|---|
| 314 |
|
|---|
| 315 |
options = options_as_params(options) |
|---|
| 316 |
expire_on = build_expiry(options, recall) |
|---|
| 317 |
|
|---|
| 318 |
if options[:controller] |
|---|
| 319 |
options[:controller] = options[:controller].to_s |
|---|
| 320 |
end |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
if !named_route && expire_on[:controller] && options[:controller] && options[:controller][0] != ?/ |
|---|
| 326 |
old_parts = recall[:controller].split('/') |
|---|
| 327 |
new_parts = options[:controller].split('/') |
|---|
| 328 |
parts = old_parts[0..-(new_parts.length + 1)] + new_parts |
|---|
| 329 |
options[:controller] = parts.join('/') |
|---|
| 330 |
end |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
options[:controller] = options[:controller][1..-1] if options[:controller] && options[:controller][0] == ?/ |
|---|
| 334 |
merged = recall.merge(options) |
|---|
| 335 |
|
|---|
| 336 |
if named_route |
|---|
| 337 |
path = named_route.generate(options, merged, expire_on) |
|---|
| 338 |
if path.nil? |
|---|
| 339 |
raise_named_route_error(options, named_route, named_route_name) |
|---|
| 340 |
else |
|---|
| 341 |
return path |
|---|
| 342 |
end |
|---|
| 343 |
else |
|---|
| 344 |
merged[:action] ||= 'index' |
|---|
| 345 |
options[:action] ||= 'index' |
|---|
| 346 |
|
|---|
| 347 |
controller = merged[:controller] |
|---|
| 348 |
action = merged[:action] |
|---|
| 349 |
|
|---|
| 350 |
raise RoutingError, "Need controller and action!" unless controller && action |
|---|
| 351 |
|
|---|
| 352 |
if generate_all |
|---|
| 353 |
|
|---|
| 354 |
return routes.collect do |route| |
|---|
| 355 |
route.send!(method, options, merged, expire_on) |
|---|
| 356 |
end.compact |
|---|
| 357 |
end |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
routes = routes_by_controller[controller][action][options.keys.sort_by { |x| x.object_id }] |
|---|
| 361 |
|
|---|
| 362 |
routes.each do |route| |
|---|
| 363 |
results = route.send!(method, options, merged, expire_on) |
|---|
| 364 |
return results if results && (!results.is_a?(Array) || results.first) |
|---|
| 365 |
end |
|---|
| 366 |
end |
|---|
| 367 |
|
|---|
| 368 |
raise RoutingError, "No route matches #{options.inspect}" |
|---|
| 369 |
end |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
def raise_named_route_error(options, named_route, named_route_name) |
|---|
| 373 |
diff = named_route.requirements.diff(options) |
|---|
| 374 |
unless diff.empty? |
|---|
| 375 |
raise RoutingError, "#{named_route_name}_url failed to generate from #{options.inspect}, expected: #{named_route.requirements.inspect}, diff: #{named_route.requirements.diff(options).inspect}" |
|---|
| 376 |
else |
|---|
| 377 |
required_segments = named_route.segments.select {|seg| (!seg.optional?) && (!seg.is_a?(DividerSegment)) } |
|---|
| 378 |
required_keys_or_values = required_segments.map { |seg| seg.key rescue seg.value } |
|---|
| 379 |
raise RoutingError, "#{named_route_name}_url failed to generate from #{options.inspect} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: #{required_keys_or_values.inspect} - are they all satisfied?" |
|---|
| 380 |
end |
|---|
| 381 |
end |
|---|
| 382 |
|
|---|
| 383 |
def recognize(request) |
|---|
| 384 |
params = recognize_path(request.path, extract_request_environment(request)) |
|---|
| 385 |
request.path_parameters = params.with_indifferent_access |
|---|
| 386 |
"#{params[:controller].camelize}Controller".constantize |
|---|
| 387 |
end |
|---|
| 388 |
|
|---|
| 389 |
def recognize_path(path, environment={}) |
|---|
| 390 |
raise "Not optimized! Check that routing/recognition_optimisation overrides RouteSet#recognize_path." |
|---|
| 391 |
end |
|---|
| 392 |
|
|---|
| 393 |
def routes_by_controller |
|---|
| 394 |
@routes_by_controller ||= Hash.new do |controller_hash, controller| |
|---|
| 395 |
controller_hash[controller] = Hash.new do |action_hash, action| |
|---|
| 396 |
action_hash[action] = Hash.new do |key_hash, keys| |
|---|
| 397 |
key_hash[keys] = routes_for_controller_and_action_and_keys(controller, action, keys) |
|---|
| 398 |
end |
|---|
| 399 |
end |
|---|
| 400 |
end |
|---|
| 401 |
end |
|---|
| 402 |
|
|---|
| 403 |
def routes_for(options, merged, expire_on) |
|---|
| 404 |
raise "Need controller and action!" unless controller && action |
|---|
| 405 |
controller = merged[:controller] |
|---|
| 406 |
merged = options if expire_on[:controller] |
|---|
| 407 |
action = merged[:action] || 'index' |
|---|
| 408 |
|
|---|
| 409 |
routes_by_controller[controller][action][merged.keys] |
|---|
| 410 |
end |
|---|
| 411 |
|
|---|
| 412 |
def routes_for_controller_and_action(controller, action) |
|---|
| 413 |
selected = routes.select do |route| |
|---|
| 414 |
route.matches_controller_and_action? controller, action |
|---|
| 415 |
end |
|---|
| 416 |
(selected.length == routes.length) ? routes : selected |
|---|
| 417 |
end |
|---|
| 418 |
|
|---|
| 419 |
def routes_for_controller_and_action_and_keys(controller, action, keys) |
|---|
| 420 |
selected = routes.select do |route| |
|---|
| 421 |
route.matches_controller_and_action? controller, action |
|---|
| 422 |
end |
|---|
| 423 |
selected.sort_by do |route| |
|---|
| 424 |
(keys - route.significant_keys).length |
|---|
| 425 |
end |
|---|
| 426 |
end |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
def extract_request_environment(request) |
|---|
| 431 |
{ :method => request.method } |
|---|
| 432 |
end |
|---|
| 433 |
end |
|---|
| 434 |
end |
|---|
| 435 |
end |
|---|