Permalink
Browse files

push model and class handling to a helper builder object

  • Loading branch information...
tenderlove committed May 8, 2014
1 parent 6634e9c commit e14ec1266cfc91b9ee23a75aa82baff9760d2c42
Showing with 48 additions and 36 deletions.
  1. +48 −36 actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -105,15 +105,17 @@ def polymorphic_url(record_or_hash_or_array, options = {})
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 @@ def polymorphic_url(record_or_hash_or_array, options = {})
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 @@ def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_p
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 @@ def handle_list(list, prefix, suffix, inflection)
[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}"

0 comments on commit e14ec12

Please sign in to comment.