Class: ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
- Defined in:
- lib/action_dispatch/routing/polymorphic_routes.rb
Overview
:nodoc:
Constant Summary collapse
- CACHE =
{ path: {}, url: {} }
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Class Method Summary collapse
- .build(action, type) ⇒ Object
- .get(action, type) ⇒ Object
- .path ⇒ Object
- .plural(prefix, suffix) ⇒ Object
- .polymorphic_method(recipient, record_or_hash_or_array, action, type, options) ⇒ Object
- .singular(prefix, suffix) ⇒ Object
- .url ⇒ Object
Instance Method Summary collapse
- #handle_class(klass) ⇒ Object
- #handle_class_call(target, klass) ⇒ Object
- #handle_list(list) ⇒ Object
- #handle_model(record) ⇒ Object
- #handle_model_call(target, record) ⇒ Object
- #handle_string(record) ⇒ Object
- #handle_string_call(target, str) ⇒ Object
-
#initialize(key_strategy, prefix, suffix) ⇒ HelperMethodBuilder
constructor
A new instance of HelperMethodBuilder.
Constructor Details
#initialize(key_strategy, prefix, suffix) ⇒ HelperMethodBuilder
Returns a new instance of HelperMethodBuilder.
241 242 243 244 245 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 241 def initialize(key_strategy, prefix, suffix) @key_strategy = key_strategy @prefix = prefix @suffix = suffix end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
239 240 241 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 239 def prefix @prefix end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
239 240 241 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 239 def suffix @suffix end |
Class Method Details
.build(action, type) ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 189 def self.build(action, type) prefix = action ? "#{action}_" : "" suffix = type if action.to_s == "new" HelperMethodBuilder.singular prefix, suffix else HelperMethodBuilder.plural prefix, suffix end end |
.get(action, type) ⇒ Object
181 182 183 184 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 181 def self.get(action, type) type = type.to_sym CACHE[type].fetch(action) { build action, type } end |
.path ⇒ Object
187 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 187 def self.path; CACHE[:path][nil]; end |
.plural(prefix, suffix) ⇒ Object
203 204 205 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 203 def self.plural(prefix, suffix) new(->(name) { name.route_key }, prefix, suffix) end |
.polymorphic_method(recipient, record_or_hash_or_array, action, type, options) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 207 def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, ) builder = get action, type case record_or_hash_or_array when Array record_or_hash_or_array = record_or_hash_or_array.compact if record_or_hash_or_array.empty? raise ArgumentError, "Nil location provided. Can't build URI." end if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy) recipient = record_or_hash_or_array.shift end method, args = builder.handle_list record_or_hash_or_array when String, Symbol method, args = builder.handle_string record_or_hash_or_array when Class method, args = builder.handle_class record_or_hash_or_array when nil raise ArgumentError, "Nil location provided. Can't build URI." else method, args = builder.handle_model record_or_hash_or_array end if .empty? recipient.public_send(method, *args) else recipient.public_send(method, *args, ) end end |
.singular(prefix, suffix) ⇒ Object
199 200 201 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 199 def self.singular(prefix, suffix) new(->(name) { name.singular_route_key }, prefix, suffix) end |
.url ⇒ Object
186 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 186 def self.url; CACHE[:url][nil]; end |
Instance Method Details
#handle_class(klass) ⇒ Object
255 256 257 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 255 def handle_class(klass) [get_method_for_class(klass), []] end |
#handle_class_call(target, klass) ⇒ Object
259 260 261 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 259 def handle_class_call(target, klass) target.public_send get_method_for_class klass end |
#handle_list(list) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 286 def handle_list(list) record_list = list.dup record = record_list.pop args = [] route = record_list.map do |parent| case parent when Symbol parent.to_s when String raise(ArgumentError, "Please use symbols for polymorphic route arguments.") when Class args << parent parent.model_name.singular_route_key else args << parent.to_model parent.to_model.model_name.singular_route_key end end route << case record when Symbol record.to_s when String raise(ArgumentError, "Please use symbols for polymorphic route arguments.") when Class @key_strategy.call record.model_name else model = record.to_model if model.persisted? args << model model.model_name.singular_route_key else @key_strategy.call model.model_name end end route << suffix named_route = prefix + route.join("_") [named_route, args] end |
#handle_model(record) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 263 def handle_model(record) args = [] model = record.to_model named_route = if model.persisted? args << model get_method_for_string model.model_name.singular_route_key else get_method_for_class model end [named_route, args] end |
#handle_model_call(target, record) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 277 def handle_model_call(target, record) if mapping = polymorphic_mapping(target, record) mapping.call(target, [record], suffix == "path") else method, args = handle_model(record) target.public_send(method, *args) end end |
#handle_string(record) ⇒ Object
247 248 249 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 247 def handle_string(record) [get_method_for_string(record), []] end |
#handle_string_call(target, str) ⇒ Object
251 252 253 |
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 251 def handle_string_call(target, str) target.public_send get_method_for_string str end |