Class: Jets::Router::Helpers::NamedRoutes::NamedRouteMethod
- Inherits:
-
Object
- Object
- Jets::Router::Helpers::NamedRoutes::NamedRouteMethod
- Includes:
- Controller::Decorate::ApigwStage
- Defined in:
- lib/jets/router/helpers/named_routes/generated.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(route, args, context, engine_class) ⇒ NamedRouteMethod
constructor
A new instance of NamedRouteMethod.
-
#normalize_options(path, args) ⇒ Object
Normalized with format in options Helps to mimic Rails behavior.
- #path ⇒ Object
-
#path_with_replaced_placeholders ⇒ Object
IE: /posts/1.
- #placeholders ⇒ Object
- #prepend_engine_mounted_at(path) ⇒ Object
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 = (@path, args) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
132 133 134 |
# File 'lib/jets/router/helpers/named_routes/generated.rb', line 132 def @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 (path, args) args = args.dup = args. # If there is still an argument in rest, it's the format. Rails behavior. rest = args[placeholders.size..-1] format = rest.last [:format] = format if format end |
#path ⇒ Object
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_placeholders ⇒ Object
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 |
#placeholders ⇒ Object
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 |