Class: Jets::Router::Compat::RouteSet::RouteWithParams

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/router/compat/route_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(route, parameterized_parts, params) ⇒ RouteWithParams

Returns a new instance of RouteWithParams.



137
138
139
140
141
# File 'lib/jets/router/compat/route_set.rb', line 137

def initialize(route, parameterized_parts, params)
  @route = route
  @parameterized_parts = parameterized_parts
  @params = params
end

Instance Method Details

#paramsObject



143
144
145
146
147
148
149
# File 'lib/jets/router/compat/route_set.rb', line 143

def params
  params = @params.dup
  # So controller and action do not show up in the query string
  params.delete(:controller)
  params.delete(:action)
  params
end

#path(_) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jets/router/compat/route_set.rb', line 151

def path(_)
  # Match with Jets router instead of Rails router
  matcher = Jets::Router::Matcher.new
  route = matcher.find_by_controller_action(@parameterized_parts[:controller], @parameterized_parts[:action])
  if route
    path = replace_placeholders(route.path, @parameterized_parts)
    path.starts_with?('/') ? path : "/#{path}"
  else
    # Mimic Rails MissingRoute error
    constraints = @params
    missing_keys = []
    unmatched_keys = []
    routes = Jets.application.routes
    name = nil
    ActionDispatch::Journey::Formatter::MissingRoute.new(constraints, missing_keys, unmatched_keys, routes, name).path(name)
  end
end

#replace_placeholders(path, params) ⇒ Object



169
170
171
172
173
174
# File 'lib/jets/router/compat/route_set.rb', line 169

def replace_placeholders(path, params)
  params.each do |key, value|
    path = path.gsub(":#{key}", value.to_param)
  end
  path
end