Class: ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | actionpack/lib/action_dispatch/routing/polymorphic_routes.rb |
Constant Summary
-
CACHE =
# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 186{ path: {}, url: {} }
Class Method Summary
Instance Attribute Summary
Instance Method Summary
- #handle_class(klass)
- #handle_class_call(target, klass)
- #handle_list(list)
- #handle_model(record)
- #handle_model_call(target, record)
- #handle_string(record)
- #handle_string_call(target, str)
- #get_method_for_class(klass) private
- #get_method_for_string(str) private
- #polymorphic_mapping(target, record) private
Constructor Details
.new(key_strategy, prefix, suffix) ⇒ HelperMethodBuilder
Class Method Details
.build(action, type)
[ GitHub ].get(action, type)
[ GitHub ].path
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 194
def self.path; CACHE[:path][nil]; end
.plural(prefix, suffix)
[ GitHub ].polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 214
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)
[ GitHub ].url
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 193
def self.url; CACHE[:url][nil]; end
Instance Attribute Details
#prefix (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 246
attr_reader :suffix, :prefix
#suffix (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 246
attr_reader :suffix, :prefix
Instance Method Details
#get_method_for_class(klass) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 347
def get_method_for_class(klass) name = @key_strategy.call klass.model_name get_method_for_string name end
#get_method_for_string(str) (private)
[ GitHub ]#handle_class(klass)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 262
def handle_class(klass) [get_method_for_class(klass), []] end
#handle_class_call(target, klass)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 266
def handle_class_call(target, klass) target.public_send get_method_for_class klass end
#handle_list(list)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 293
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)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 270
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)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 284
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)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 254
def handle_string(record) [get_method_for_string(record), []] end
#handle_string_call(target, str)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 258
def handle_string_call(target, str) target.public_send get_method_for_string str end
#polymorphic_mapping(target, record) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb', line 339
def polymorphic_mapping(target, record) if record.respond_to?(:to_model) target._routes.polymorphic_mappings[record.to_model.model_name.name] else target._routes.polymorphic_mappings[record.class.name] end end