aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-05-08 11:17:02 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-05-12 15:54:45 -0700
commite14ec1266cfc91b9ee23a75aa82baff9760d2c42 (patch)
tree7f582a101be4f593a45ab3518e20a6ff12fc86a1 /actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
parent6634e9c8c5e1909769db45bc43d6ea80844a441e (diff)
downloadrails-e14ec1266cfc91b9ee23a75aa82baff9760d2c42.tar.gz
rails-e14ec1266cfc91b9ee23a75aa82baff9760d2c42.tar.bz2
rails-e14ec1266cfc91b9ee23a75aa82baff9760d2c42.zip
push model and class handling to a helper builder object
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/polymorphic_routes.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/polymorphic_routes.rb84
1 files changed, 48 insertions, 36 deletions
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}"