Class: ActionDispatch::Routing::DeprecatedMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/routing/deprecated_mapper.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Resource, SingletonResource

Constant Summary collapse

INHERITABLE_OPTIONS =
:namespace, :shallow

Instance Method Summary collapse

Constructor Details

#initialize(set) ⇒ DeprecatedMapper

:nodoc:


33
34
35
36
37
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 33

def initialize(set) #:nodoc:
  ActiveSupport::Deprecation.warn "You are using the old router DSL which will be 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/"
  @set = set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:


204
205
206
207
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 204

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

Instance Method Details

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


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 39

def connect(path, options = {})
  options = options.dup

  if conditions = options.delete(:conditions)
    conditions = conditions.dup
    method = [conditions.delete(:method)].flatten.compact
    method.map! { |m|
      m = m.to_s.upcase

      if m == "HEAD"
        raise ArgumentError, "HTTP method HEAD is invalid in route conditions. Rails processes HEAD requests the same as GETs, returning just the response headers"
      end

      unless HTTP_METHODS.include?(m.downcase.to_sym)
        raise ArgumentError, "Invalid HTTP method specified in route conditions"
      end

      m
    }

    if method.length > 1
      method = Regexp.union(*method)
    elsif method.length == 1
      method = method.first
    else
      method = nil
    end
  end

  path_prefix = options.delete(:path_prefix)
  name_prefix = options.delete(:name_prefix)
  namespace  = options.delete(:namespace)

  name = options.delete(:_name)
  name = "#{name_prefix}#{name}" if name_prefix

  requirements = options.delete(:requirements) || {}
  defaults = options.delete(:defaults) || {}
  options.each do |k, v|
    if v.is_a?(Regexp)
      if value = options.delete(k)
        requirements[k.to_sym] = value
      end
    else
      value = options.delete(k)
      defaults[k.to_sym] = value.is_a?(Symbol) ? value : value.to_param
    end
  end

  requirements.each do |_, requirement|
    if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
      raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
    end
    if requirement.multiline?
      raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
    end
  end

  requirements[:controller] ||= @set.controller_constraints

  if defaults[:controller]
    defaults[:action] ||= 'index'
    defaults[:controller] = defaults[:controller].to_s
    defaults[:controller] = "#{namespace}#{defaults[:controller]}" if namespace
  end

  if defaults[:action]
    defaults[:action] = defaults[:action].to_s
  end

  if path.is_a?(String)
    path = "#{path_prefix}/#{path}" if path_prefix
    path = path.gsub('.:format', '(.:format)')
    path = optionalize_trailing_dynamic_segments(path, requirements, defaults)
    glob = $1.to_sym if path =~ /\/\*(\w+)$/
    path = ::Rack::Mount::Utils.normalize_path(path)

    if glob && !defaults[glob].blank?
      raise ActionController::RoutingError, "paths cannot have non-empty default values"
    end
  end

  app = Routing::RouteSet::Dispatcher.new(:defaults => defaults, :glob => glob)

  conditions = {}
  conditions[:request_method] = method if method
  conditions[:path_info] = path if path

  @set.add_route(app, conditions, requirements, defaults, name)
end

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

:nodoc:


191
192
193
194
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 191

def named_route(name, path, options = {}) #:nodoc:
  options[:_name] = name
  connect(path, options)
end

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


196
197
198
199
200
201
202
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 196

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

#resource(*entities, &block) ⇒ Object


361
362
363
364
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 361

def resource(*entities, &block)
  options = entities.extract_options!
  entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
end

#resources(*entities, &block) ⇒ Object


356
357
358
359
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 356

def resources(*entities, &block)
  options = entities.extract_options!
  entities.each { |entity| map_resource(entity, options.dup, &block) }
end

#root(options = {}) ⇒ Object

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


182
183
184
185
186
187
188
189
# File 'lib/action_dispatch/routing/deprecated_mapper.rb', line 182

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