Class: ActionDispatch::Routing::RouteSet
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::RouteSet
- Defined in:
- lib/action_dispatch/routing/route_set.rb
Overview
:nodoc:
Defined Under Namespace
Modules: MountedHelpers Classes: Dispatcher, Generator, NamedRouteCollection
Constant Summary collapse
- PARAMETERS_KEY =
'action_dispatch.request.path_parameters'
- RESERVED_OPTIONS =
[:host, :protocol, :port, :subdomain, :domain, :tld_length, :trailing_slash, :anchor, :params, :only_path, :script_name]
Instance Attribute Summary collapse
-
#default_scope ⇒ Object
Returns the value of attribute default_scope.
-
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
-
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#named_routes ⇒ Object
Returns the value of attribute named_routes.
-
#request_class ⇒ Object
Returns the value of attribute request_class.
-
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
-
#router ⇒ Object
Returns the value of attribute router.
-
#set ⇒ Object
(also: #routes)
Returns the value of attribute set.
-
#valid_conditions ⇒ Object
Returns the value of attribute valid_conditions.
Class Method Summary collapse
Instance Method Summary collapse
- #_generate_prefix(options = {}) ⇒ Object
- #add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) ⇒ Object
- #append(&block) ⇒ Object
- #call(env) ⇒ Object
- #clear! ⇒ Object
- #define_mounted_helper(name) ⇒ Object
- #draw(&block) ⇒ Object
- #empty? ⇒ Boolean
- #eval_block(block) ⇒ Object
-
#extra_keys(options, recall = {}) ⇒ Object
Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.
- #finalize! ⇒ Object
- #generate(options, recall = {}, extras = false) ⇒ Object
- #generate_extras(options, recall = {}) ⇒ Object
-
#initialize(request_class = ActionDispatch::Request) ⇒ RouteSet
constructor
A new instance of RouteSet.
- #install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
- #mounted_helpers ⇒ Object
- #prepend(&block) ⇒ Object
- #recognize_path(path, environment = {}) ⇒ Object
- #url_for(options) ⇒ Object
- #url_helpers ⇒ Object
Constructor Details
#initialize(request_class = ActionDispatch::Request) ⇒ RouteSet
Returns a new instance of RouteSet.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/action_dispatch/routing/route_set.rb', line 233 def initialize(request_class = ActionDispatch::Request) self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names.dup self. = {} self.request_class = request_class @valid_conditions = {} request_class.public_instance_methods.each { |m| @valid_conditions[m.to_sym] = true } @valid_conditions[:controller] = true @valid_conditions[:action] = true self.valid_conditions.delete(:id) @append = [] @prepend = [] @disable_clear_and_finalize = false @finalized = false @set = Journey::Routes.new @router = Journey::Router.new(@set, { :parameters_key => PARAMETERS_KEY, :request_class => request_class}) @formatter = Journey::Formatter.new @set end |
Instance Attribute Details
#default_scope ⇒ Object
Returns the value of attribute default_scope.
223 224 225 |
# File 'lib/action_dispatch/routing/route_set.rb', line 223 def default_scope @default_scope end |
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
225 226 227 |
# File 'lib/action_dispatch/routing/route_set.rb', line 225 def @default_url_options end |
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
224 225 226 |
# File 'lib/action_dispatch/routing/route_set.rb', line 224 def disable_clear_and_finalize @disable_clear_and_finalize end |
#formatter ⇒ Object
Returns the value of attribute formatter.
223 224 225 |
# File 'lib/action_dispatch/routing/route_set.rb', line 223 def formatter @formatter end |
#named_routes ⇒ Object
Returns the value of attribute named_routes.
223 224 225 |
# File 'lib/action_dispatch/routing/route_set.rb', line 223 def named_routes @named_routes end |
#request_class ⇒ Object
Returns the value of attribute request_class.
225 226 227 |
# File 'lib/action_dispatch/routing/route_set.rb', line 225 def request_class @request_class end |
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
224 225 226 |
# File 'lib/action_dispatch/routing/route_set.rb', line 224 def resources_path_names @resources_path_names end |
#router ⇒ Object
Returns the value of attribute router.
223 224 225 |
# File 'lib/action_dispatch/routing/route_set.rb', line 223 def router @router end |
#set ⇒ Object Also known as: routes
Returns the value of attribute set.
223 224 225 |
# File 'lib/action_dispatch/routing/route_set.rb', line 223 def set @set end |
#valid_conditions ⇒ Object
Returns the value of attribute valid_conditions.
225 226 227 |
# File 'lib/action_dispatch/routing/route_set.rb', line 225 def valid_conditions @valid_conditions end |
Class Method Details
.default_resources_path_names ⇒ Object
229 230 231 |
# File 'lib/action_dispatch/routing/route_set.rb', line 229 def self.default_resources_path_names { :new => 'new', :edit => 'edit' } end |
Instance Method Details
#_generate_prefix(options = {}) ⇒ Object
575 576 577 |
# File 'lib/action_dispatch/routing/route_set.rb', line 575 def _generate_prefix( = {}) nil end |
#add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) ⇒ Object
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/action_dispatch/routing/route_set.rb', line 365 def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i) path = build_path(conditions.delete(:path_info), requirements, SEPARATORS, anchor) conditions = build_conditions(conditions, valid_conditions, path.names.map { |x| x.to_sym }) route = @set.add_route(app, path, conditions, defaults, name) named_routes[name] = route if name route end |
#append(&block) ⇒ Object
268 269 270 |
# File 'lib/action_dispatch/routing/route_set.rb', line 268 def append(&block) @append << block end |
#call(env) ⇒ Object
606 607 608 609 |
# File 'lib/action_dispatch/routing/route_set.rb', line 606 def call(env) finalize! @router.call(env) end |
#clear! ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/action_dispatch/routing/route_set.rb', line 295 def clear! @finalized = false named_routes.clear set.clear formatter.clear @prepend.each { |blk| eval_block(blk) } end |
#define_mounted_helper(name) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/action_dispatch/routing/route_set.rb', line 315 def define_mounted_helper(name) return if MountedHelpers.method_defined?(name) routes = self MountedHelpers.class_eval do define_method "_#{name}" do RoutesProxy.new(routes, self._routes_context) end end MountedHelpers.class_eval <<-RUBY def #{name} @#{name} ||= _#{name} end RUBY end |
#draw(&block) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/action_dispatch/routing/route_set.rb', line 261 def draw(&block) clear! unless @disable_clear_and_finalize eval_block(block) finalize! unless @disable_clear_and_finalize nil end |
#empty? ⇒ Boolean
361 362 363 |
# File 'lib/action_dispatch/routing/route_set.rb', line 361 def empty? routes.empty? end |
#eval_block(block) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/action_dispatch/routing/route_set.rb', line 276 def eval_block(block) if block.arity == 1 raise "You are using the old router DSL which has been removed in Rails 3.1. " << "Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/" end mapper = Mapper.new(self) if default_scope mapper.with_default_scope(default_scope, &block) else mapper.instance_exec(&block) end end |
#extra_keys(options, recall = {}) ⇒ Object
Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.
560 561 562 |
# File 'lib/action_dispatch/routing/route_set.rb', line 560 def extra_keys(, recall={}) generate_extras(, recall).last end |
#finalize! ⇒ Object
289 290 291 292 293 |
# File 'lib/action_dispatch/routing/route_set.rb', line 289 def finalize! return if @finalized @append.each { |blk| eval_block(blk) } @finalized = true end |
#generate(options, recall = {}, extras = false) ⇒ Object
568 569 570 |
# File 'lib/action_dispatch/routing/route_set.rb', line 568 def generate(, recall = {}, extras = false) Generator.new(, recall, self, extras).generate end |
#generate_extras(options, recall = {}) ⇒ Object
564 565 566 |
# File 'lib/action_dispatch/routing/route_set.rb', line 564 def generate_extras(, recall={}) generate(, recall, true) end |
#install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
303 304 305 306 |
# File 'lib/action_dispatch/routing/route_set.rb', line 303 def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) Array(destinations).each { |d| d.module_eval { include Helpers } } named_routes.install(destinations, regenerate_code) end |
#mounted_helpers ⇒ Object
311 312 313 |
# File 'lib/action_dispatch/routing/route_set.rb', line 311 def mounted_helpers MountedHelpers end |
#prepend(&block) ⇒ Object
272 273 274 |
# File 'lib/action_dispatch/routing/route_set.rb', line 272 def prepend(&block) @prepend << block end |
#recognize_path(path, environment = {}) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/action_dispatch/routing/route_set.rb', line 611 def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://} extras = environment[:extras] || {} begin env = Rack::MockRequest.env_for(path, {:method => method, :params => extras}) rescue URI::InvalidURIError => e raise ActionController::RoutingError, e. end req = @request_class.new(env) @router.recognize(req) do |route, matches, params| params.each do |key, value| if value.is_a?(String) value = value.dup.force_encoding(Encoding::BINARY) if value.encoding_aware? params[key] = URI.parser.unescape(value) end end dispatcher = route.app while dispatcher.is_a?(Mapper::Constraints) && dispatcher.matches?(env) do dispatcher = dispatcher.app end if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params, false) dispatcher.prepare_params!(params) return params end end raise ActionController::RoutingError, "No route matches #{path.inspect}" end |
#url_for(options) ⇒ Object
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/action_dispatch/routing/route_set.rb', line 579 def url_for() finalize! = ( || {}).reverse_merge!() handle_positional_args() user, password = extract_authentication() path_segments = .delete(:_path_segments) script_name = .delete(:script_name) path = (script_name.blank? ? _generate_prefix() : script_name.chomp('/')).to_s = .except(*RESERVED_OPTIONS) = yield() if block_given? path_addition, params = generate(, path_segments || {}) path << path_addition params.merge!([:params] || {}) ActionDispatch::Http::URL.url_for(.merge!({ :path => path, :params => params, :user => user, :password => password })) end |
#url_helpers ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/action_dispatch/routing/route_set.rb', line 332 def url_helpers @url_helpers ||= begin routes = self helpers = Module.new do extend ActiveSupport::Concern include UrlFor @_routes = routes class << self delegate :url_for, :to => '@_routes' end extend routes.named_routes.module # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that # we can include? # Yes plz - JP included do routes.install_helpers(self) singleton_class.send(:redefine_method, :_routes) { routes } end define_method(:_routes) { @_routes || routes } end helpers end end |