Class: Jets::Router::Helpers::NamedRoutes::NamedRouteMethod

Inherits:
Object
  • Object
show all
Includes:
Controller::Decorate::ApigwStage
Defined in:
lib/jets/router/helpers/named_routes/generated.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Controller::Decorate::ApigwStage

#add_apigw_stage, #add_apigw_stage?

Constructor Details

#initialize(route, args, context, engine_class) ⇒ NamedRouteMethod

Returns a new instance of NamedRouteMethod.



133
134
135
136
137
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 133

def initialize(route, args, context, engine_class)
  @route, @args, @context, @engine_class = route, args, context, engine_class
  @path = route.path # With placeholders still IE: /posts/:id
  @options = normalize_options(@path, args)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



132
133
134
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 132

def options
  @options
end

Instance Method Details

#normalize_options(path, args) ⇒ Object

Normalized with format in options Helps to mimic Rails behavior. Processes extra arguments to support format and query string.

post_path(post, "json", {foo: 'bar'}) => "/posts/1.json?foo=bar"
post_path(post, {foo: 'bar'})         => "/posts/1?foo=bar"


146
147
148
149
150
151
152
153
154
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 146

def normalize_options(path, args)
  args = args.dup
  options = args.extract_options!
  # If there is still an argument in rest, it's the format. Rails behavior.
  rest = args[placeholders.size..-1]
  format = rest.last
  options[:format] = format if format
  options
end

#pathObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 156

def path
  query_string = @options.dup # make copy to modify
  format = query_string.delete(:format)
  query_string.delete(:action)
  query_string.delete(:controller)
  query_string.delete(:only_path)

  path = path_with_replaced_placeholders
  path = prepend_engine_mounted_at(path)
  path = "#{path}.#{format}" if format
  path = "#{path}?#{query_string.to_query}" unless query_string.empty?
  path = add_apigw_stage(path)
  path
end

#path_with_replaced_placeholdersObject

IE: /posts/1



175
176
177
178
179
180
181
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 175

def path_with_replaced_placeholders
  path = @path
  placeholders.each_with_index do |placeholder, index|
    path = path.gsub(placeholder, @args[index].to_param)
  end
  path
end

#placeholdersObject



183
184
185
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 183

def placeholders
  @path.scan(/:\w+/)
end

#prepend_engine_mounted_at(path) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 187

def prepend_engine_mounted_at(path)
  mount = Jets::Router::EngineMount.find_by(engine: @engine_class)
  if mount
    path = path.delete_prefix('/')
    [mount.at, path].compact.join('/')
  else
    path
  end
end