Skip to content
Browse files

adding a method to handle strings using the same api

  • Loading branch information...
1 parent 89e0718 commit 6634e9c8c5e1909769db45bc43d6ea80844a441e @tenderlove tenderlove committed
Showing with 32 additions and 22 deletions.
  1. +32 −22 actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
View
54 actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -140,8 +140,10 @@ def polymorphic_url(record_or_hash_or_array, options = {})
suffix,
inflection
when String, Symbol
- args = []
- method = prefix + "#{record_or_hash_or_array}_#{suffix}"
+ method, args = handle_string record_or_hash_or_array,
+ prefix,
+ suffix,
+ inflection
when Class
method, args = handle_class record_or_hash_or_array,
prefix,
@@ -171,6 +173,28 @@ def polymorphic_path(record_or_hash_or_array, options)
polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
end
+
+ %w(edit new).each do |action|
+ module_eval <<-EOT, __FILE__, __LINE__ + 1
+ def #{action}_polymorphic_url(record_or_hash, options = {}) # def edit_polymorphic_url(record_or_hash, options = {})
+ polymorphic_url( # polymorphic_url(
+ record_or_hash, # record_or_hash,
+ options.merge(:action => "#{action}")) # options.merge(:action => "edit"))
+ end # end
+ #
+ def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_polymorphic_path(record_or_hash, options = {})
+ polymorphic_url( # polymorphic_url(
+ record_or_hash, # record_or_hash,
+ options.merge(:action => "#{action}", :routing_type => :path)) # options.merge(:action => "edit", :routing_type => :path))
+ end # end
+ EOT
+ end
+
+ private
+
+ ROUTE_KEY = lambda { |name| name.route_key }
+ SINGULAR_ROUTE_KEY = lambda { |name| name.singular_route_key }
+
def handle_list(list, prefix, suffix, inflection)
record_list = list.dup
record = record_list.pop
@@ -211,26 +235,6 @@ def handle_list(list, prefix, suffix, inflection)
[named_route, args]
end
- %w(edit new).each do |action|
- module_eval <<-EOT, __FILE__, __LINE__ + 1
- def #{action}_polymorphic_url(record_or_hash, options = {}) # def edit_polymorphic_url(record_or_hash, options = {})
- polymorphic_url( # polymorphic_url(
- record_or_hash, # record_or_hash,
- options.merge(:action => "#{action}")) # options.merge(:action => "edit"))
- end # end
- #
- def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_polymorphic_path(record_or_hash, options = {})
- polymorphic_url( # polymorphic_url(
- record_or_hash, # record_or_hash,
- options.merge(:action => "#{action}", :routing_type => :path)) # options.merge(:action => "edit", :routing_type => :path))
- end # end
- EOT
- end
-
- private
- ROUTE_KEY = lambda { |name| name.route_key }
- SINGULAR_ROUTE_KEY = lambda { |name| name.singular_route_key }
-
def handle_model(record, prefix, suffix, inflection)
args = []
@@ -252,6 +256,12 @@ def handle_class(klass, prefix, suffix, inflection)
[prefix + "#{name}_#{suffix}", []]
end
+ def handle_string(record, prefix, suffix, inflection)
+ args = []
+ method = prefix + "#{record}_#{suffix}"
+ [method, args]
+ end
+
def model_path_helper_call(record)
handle_model record, ''.freeze, "path".freeze, ROUTE_KEY
end

0 comments on commit 6634e9c

Please sign in to comment.
Something went wrong with that request. Please try again.