Class: ActionController::Routing::RouteSet::Mapper

Inherits:
Object
  • Object
show all
Includes:
ActionController::Resources
Defined in:
lib/action_controller/routing/route_set.rb,
lib/action_controller/resources.rb

Overview

Mapper instances are used to build routes. The object passed to the draw block in config/routes.rb is a Mapper instance.

Mapper instances have relatively few instance methods, in order to avoid clashes with named routes.

Instance Method Summary collapse

Methods included from ActionController::Resources

#resource, #resources

Constructor Details

#initialize(set) ⇒ Mapper

:doc:



10
11
12
# File 'lib/action_controller/routing/route_set.rb', line 10

def initialize(set) #:nodoc:
  @set = set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(route_name, *args, &proc) ⇒ Object

:nodoc:



53
54
55
56
# File 'lib/action_controller/routing/route_set.rb', line 53

def method_missing(route_name, *args, &proc) #:nodoc:
  super unless args.length >= 1 && proc.nil?
  @set.add_named_route(route_name, *args)
end

Instance Method Details

#connect(path, options = {}) ⇒ Object

Create an unnamed route with the provided path and options. See ActionController::Routing for an introduction to routes.



16
17
18
# File 'lib/action_controller/routing/route_set.rb', line 16

def connect(path, options = {})
  @set.add_route(path, options)
end

#named_route(name, path, options = {}) ⇒ Object

:nodoc:



30
31
32
# File 'lib/action_controller/routing/route_set.rb', line 30

def named_route(name, path, options = {}) #:nodoc:
  @set.add_named_route(name, path, options)
end

#namespace(name, options = {}, &block) ⇒ Object

Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. Example:

map.namespace(:admin) do |admin|
  admin.resources :products,
    :has_many => [ :tags, :images, :variants ]
end

This will create admin_products_url pointing to “admin/products”, which will look for an Admin::ProductsController. It’ll also create admin_product_tags_url pointing to “admin/products/#product_id/tags”, which will look for Admin::TagsController.



45
46
47
48
49
50
51
# File 'lib/action_controller/routing/route_set.rb', line 45

def namespace(name, options = {}, &block)
  if options[:namespace]
    with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
  else
    with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
  end
end

#root(options = {}) ⇒ Object

Creates a named route called “root” for matching the root level request.



21
22
23
24
25
26
27
28
# File 'lib/action_controller/routing/route_set.rb', line 21

def root(options = {})
  if options.is_a?(Symbol)
    if source_route = @set.named_routes.routes[options]
      options = source_route.defaults.merge({ :conditions => source_route.conditions })
    end
  end
  named_route("root", '', options)
end