Class: ActionDispatch::Routing::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_friendly_urls/route_sets/rails3.rb,
lib/rails_friendly_urls/route_sets/route_set.rb

Overview

Monkey Patched Rails’ class ActionDispatch::Routing::RouteSet.

Author:

  • Carlos Alonso

Instance Method Summary collapse

Instance Method Details

#recognize_path(path, environment = {}) ⇒ Object

Monkey Patched Rails’ method to recognize redirections as well as, for some reason, the original Rails’ method doesn’t.

Raises:

  • (ActionController::RoutingError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_friendly_urls/route_sets/route_set.rb', line 13

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})
  rescue URI::InvalidURIError => e
    raise ActionController::RoutingError, e.message
  end

  req = @request_class.new(env)
  @router.recognize(req) do |route, _matches, params|
    params = _matches if params.nil?
    params.merge!(extras)
    params.merge!(req.parameters.symbolize_keys)
    params.each do |key, value|
      if value.is_a?(String)
        value = value.dup.force_encoding(Encoding::BINARY)
        params[key] = URI.parser.unescape(value)
      end
    end

    old_params = env[params_key]
    env[params_key] = (old_params || {}).merge(params)
    dispatcher = route.app
    while dispatcher.is_a?(Mapper::Constraints) && dispatcher.matches?(env) do
      dispatcher = dispatcher.app
    end

    if dispatcher.is_a?(Dispatcher)
      if dispatcher.controller(params, false)
        dispatcher.prepare_params!(params)
        return params
      else
        raise ActionController::RoutingError, "A route matches #{path.inspect}, but references missing controller: #{params[:controller].camelize}Controller"
      end
    elsif dispatcher.is_a?(redirect_class)
      return { status: 301, path: path_from_dispatcher(dispatcher) }
    end
  end

  raise ActionController::RoutingError, "No route matches #{path.inspect}"
end

#url_for(options = {}) ⇒ Object

Monkey Patched Rails’ method: Includes a call to RailsFriendlyUrls::Manager.url_for when the Rails’ URL Helper is building a url for a path to use the configured SEO Friendly substutition if any.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_friendly_urls/route_sets/rails3.rb', line 14

def url_for(options = {})
  finalize!
  options = (options || {}).reverse_merge!(default_url_options)

  handle_positional_args(options)

  user, password = extract_authentication(options)
  path_segments  = options.delete(:_path_segments)
  script_name    = options.delete(:script_name)

  path = (script_name.blank? ? _generate_prefix(options) : script_name.chomp('/')).to_s

  path_options = options.except(*RESERVED_OPTIONS)
  path_options = yield(path_options) if block_given?

  path_addition, params = generate(path_options, path_segments || {})
  path << path_addition
  params.merge!(options[:params] || {})

  path = RailsFriendlyUrls::Manager.url_for path

  ActionDispatch::Http::URL.url_for(options.merge!({
    :path => path,
    :params => params,
    :user => user,
    :password => password
  }))
end