From e14ec1266cfc91b9ee23a75aa82baff9760d2c42 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 8 May 2014 11:17:02 -0700 Subject: push model and class handling to a helper builder object --- .../action_dispatch/routing/polymorphic_routes.rb | 84 ++++++++++++---------- 1 file changed, 48 insertions(+), 36 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb') diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 544f155a09..a86896a232 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -105,15 +105,17 @@ module ActionDispatch opts = options.except(:action, :routing_type) + prefix = action_prefix options + suffix = routing_type options + if options[:action] == 'new' inflection = SINGULAR_ROUTE_KEY + builder = HelperMethodBuilder.singular else inflection = ROUTE_KEY + builder = HelperMethodBuilder.plural end - prefix = action_prefix options - suffix = routing_type options - case record_or_hash_or_array when Array if record_or_hash_or_array.empty? || record_or_hash_or_array.any?(&:nil?) @@ -135,28 +137,24 @@ module ActionDispatch opts = record_or_hash_or_array.dup.merge!(opts) record = opts.delete(:id) - method, args = handle_model record, - prefix, - suffix, - inflection + method, args = builder.handle_model record, prefix, suffix + when String, Symbol method, args = handle_string record_or_hash_or_array, prefix, suffix, inflection when Class - method, args = handle_class record_or_hash_or_array, - prefix, - suffix, - inflection + method, args = builder.handle_class record_or_hash_or_array, + prefix, + suffix when nil raise ArgumentError, "Nil location provided. Can't build URI." else - method, args = handle_model record_or_hash_or_array, - prefix, - suffix, - inflection + method, args = builder.handle_model record_or_hash_or_array, + prefix, + suffix end @@ -192,6 +190,41 @@ module ActionDispatch private + class HelperMethodBuilder # :nodoc: + def self.singular + new(->(name) { name.singular_route_key }) + end + + def self.plural + new(->(name) { name.route_key }) + end + + def initialize(key_strategy) + @key_strategy = key_strategy + end + + def handle_class(klass, prefix, suffix) + name = @key_strategy.call klass.model_name + [prefix + "#{name}_#{suffix}", []] + end + + def handle_model(record, prefix, suffix) + args = [] + + model = record.to_model + name = if record.persisted? + args << model + model.class.model_name.singular_route_key + else + @key_strategy.call model.class.model_name + end + + named_route = prefix + "#{name}_#{suffix}" + + [named_route, args] + end + end + ROUTE_KEY = lambda { |name| name.route_key } SINGULAR_ROUTE_KEY = lambda { |name| name.singular_route_key } @@ -235,27 +268,6 @@ module ActionDispatch [named_route, args] end - def handle_model(record, prefix, suffix, inflection) - args = [] - - model = record.to_model - name = if record.persisted? - args << model - model.class.model_name.singular_route_key - else - inflection.call model.class.model_name - end - - named_route = prefix + "#{name}_#{suffix}" - - [named_route, args] - end - - def handle_class(klass, prefix, suffix, inflection) - name = inflection.call klass.model_name - [prefix + "#{name}_#{suffix}", []] - end - def handle_string(record, prefix, suffix, inflection) args = [] method = prefix + "#{record}_#{suffix}" -- cgit v1.2.3