diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-07 17:19:27 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-12 15:54:44 -0700 |
commit | 4b8ab797b4653d8764c687e73cb630c32ae8bb4b (patch) | |
tree | 59272a4358bc814dde9b94df2abdb12069c1ffaa | |
parent | e8210450ad141176efdc29895b835a0e132d14d5 (diff) | |
download | rails-4b8ab797b4653d8764c687e73cb630c32ae8bb4b.tar.gz rails-4b8ab797b4653d8764c687e73cb630c32ae8bb4b.tar.bz2 rails-4b8ab797b4653d8764c687e73cb630c32ae8bb4b.zip |
break each polymorphic type to it's own method
-rw-r--r-- | actionpack/lib/action_dispatch/routing/polymorphic_routes.rb | 105 | ||||
-rw-r--r-- | actionview/lib/action_view/routing_url_for.rb | 6 |
2 files changed, 86 insertions, 25 deletions
diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 8de69b6753..6cc47dad1c 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -105,6 +105,15 @@ module ActionDispatch opts = options.except(:action, :routing_type) + if options[:action] == 'new' + inflection = SINGULAR_ROUTE_KEY + else + inflection = ROUTE_KEY + 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?) @@ -114,29 +123,57 @@ module ActionDispatch recipient = record_or_hash_or_array.shift end - record_list = record_or_hash_or_array.dup - record = record_list.pop + method, args = handle_list record_or_hash_or_array, + prefix, + suffix, + inflection when Hash unless record_or_hash_or_array[:id] raise ArgumentError, "Nil location provided. Can't build URI." end opts = record_or_hash_or_array.dup.merge!(opts) - record_list = [] record = opts.delete(:id) + + method, args = handle_model record, + prefix, + suffix, + inflection + when String, Symbol + args = [] + method = prefix + "#{record_or_hash_or_array}_#{suffix}" + when Class + method, args = handle_class record_or_hash_or_array, + prefix, + suffix, + inflection + when nil raise ArgumentError, "Nil location provided. Can't build URI." else - - record_list = [] - record = record_or_hash_or_array + method, args = handle_model record_or_hash_or_array, + prefix, + suffix, + inflection end - if options[:action] == 'new' - inflection = lambda { |name| name.singular_route_key } + + if opts.empty? + recipient.send(method, *args) else - inflection = lambda { |name| name.route_key } + recipient.send(method, *args, opts) end + end + + # Returns the path component of a URL for the given record. It uses + # <tt>polymorphic_url</tt> with <tt>routing_type: :path</tt>. + def polymorphic_path(record_or_hash_or_array, options) + polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path)) + end + + def handle_list(list, prefix, suffix, inflection) + record_list = list.dup + record = record_list.pop args = [] @@ -168,21 +205,10 @@ module ActionDispatch end end - route << routing_type(options) - - named_route = action_prefix(options) + route.join("_") - - if opts.empty? - recipient.send(named_route, *args) - else - recipient.send(named_route, *args, opts) - end - end + route << suffix - # Returns the path component of a URL for the given record. It uses - # <tt>polymorphic_url</tt> with <tt>routing_type: :path</tt>. - def polymorphic_path(record_or_hash_or_array, options = {}) - polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path)) + named_route = prefix + route.join("_") + [named_route, args] end %w(edit new).each do |action| @@ -202,6 +228,38 @@ module ActionDispatch 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 = [] + + 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 model_path_helper_call(record) + handle_model record, ''.freeze, "path".freeze, ROUTE_KEY + end + + def class_path_helper_call(klass) + handle_class klass, ''.freeze, "path".freeze, ROUTE_KEY + end + def action_prefix(options) options[:action] ? "#{options[:action]}_" : '' end @@ -212,4 +270,3 @@ module ActionDispatch end end end - diff --git a/actionview/lib/action_view/routing_url_for.rb b/actionview/lib/action_view/routing_url_for.rb index b9e4b590e7..782f1865d6 100644 --- a/actionview/lib/action_view/routing_url_for.rb +++ b/actionview/lib/action_view/routing_url_for.rb @@ -85,8 +85,12 @@ module ActionView _back_url when Array polymorphic_path(options, options.extract_options!) + when Class + method = class_path_helper_call options + send method else - polymorphic_path(options) + method, args = model_path_helper_call options + send(method, *args) end end |