Module: Jets::Router::Compat::RouteSet

Included in:
RouteSet
Defined in:
lib/jets/router/compat/route_set.rb

Overview

Related:

Jets::Controller::Compat::ActionDispatch::Routing::UrlFor
Jets::Controller::Request::Compat::RouteSet
Jets::Router::Compat::RouteSet

Compat::RouteSet makes the Jets::Router::RouteSet “compatible” with the Rails RouteSet. Importantly, it provides a custom url_for method that mimics the Rails url_for method.

Defined Under Namespace

Classes: Generator, RouteWithParams

Constant Summary collapse

RESERVED_OPTIONS =

Original: RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,

:trailing_slash, :anchor, :params, :only_path, :script_name,
:original_script_name, :relative_url_root]

UNKNOWN = ->(options) { ActionDispatch::Http::URL.url_for(options) }

ActionDispatch::Routing::RouteSet::RESERVED_OPTIONS
UNKNOWN =
ActionDispatch::Routing::RouteSet::UNKNOWN

Instance Method Summary collapse

Instance Method Details

#mounted_helpersObject

interface method



112
113
114
# File 'lib/jets/router/compat/route_set.rb', line 112

def mounted_helpers
  Module.new
end

#polymorphic_mappingsObject

This is only used for Custom resolve polymporphic path support Stub out. Jets does not currently support this. interface method



93
94
95
# File 'lib/jets/router/compat/route_set.rb', line 93

def polymorphic_mappings
  {}
end

#url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil, reserved = RESERVED_OPTIONS) ⇒ Object

This method is a version of the Rails RouteSet#url_for method. It only resolves simple routes for simple cases:

when nil
when Hash, ActionController::Parameters

This is because ActionDispatch::Routing::UrlFor#url_for gets called before and then calls out to this method. Only have to account for 2 cases to mimic Jets behavior.

Based on Jets ActionDispatch::Routing::RouteSet#url_for



34
35
36
37
38
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
# File 'lib/jets/router/compat/route_set.rb', line 34

def url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil, reserved = RESERVED_OPTIONS)
  user = password = nil

  if options[:user] && options[:password]
    user     = options.delete :user
    password = options.delete :password
  end

  # save recall_controller for url_options when controller not specified by user
  # IE: redirect_to action: :routes
  recall_controller = options[:_recall][:controller]
  recall = options.delete(:_recall) { {} }

  original_script_name = options.delete(:original_script_name)
  # script_name = find_script_name options (original)
  script_name = options.delete(:script_name)

  if original_script_name
    script_name = original_script_name + script_name
  end

  path_options = options.dup
  reserved.each { |ro| path_options.delete ro }
  path_options[:controller] ||= recall_controller

  route_with_params = generate(route_name, path_options, recall)
  path = route_with_params.path(method_name)

  if options[:trailing_slash] && !options[:format] && !path.end_with?("/")
    path += "/"
  end

  params = route_with_params.params

  if options.key? :params
    if options[:params]&.respond_to?(:to_hash)
      params.merge! options[:params]
    else
      params[:params] = options[:params]
    end
  end

  options[:path]        = path
  options[:script_name] = script_name
  options[:params]      = params
  options[:user]        = user
  options[:password]    = password

  url_strategy.call options
end