Skip to content

Commit

Permalink
This fix allows the polymorphic_method in the HelperMethodBuilder cla…
Browse files Browse the repository at this point in the history
…ss in polymorphic_routes.rb discern whether or not a singular resource is being passed into it. If it finds a singular resource it will pass in format: nil to the options so that we can have an edited object using the patch method get sent to the proper URL instead of appending a period and number to the end.
  • Loading branch information
paulreece committed Oct 27, 2023
1 parent ea4c48a commit de4ffae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ def self.plural(prefix, suffix)

def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
builder = get action, type

case record_or_hash_or_array
when Array
record_or_hash_or_array = record_or_hash_or_array.compact
Expand All @@ -228,10 +227,12 @@ def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, op
else
method, args = builder.handle_model record_or_hash_or_array
end
method_type = method.delete_suffix "_path"

if options.empty?
if options.empty? && method_type != method_type.singularize
recipient.public_send(method, *args)
else
options[:format] ||= nil
recipient.public_send(method, *args, options)
end
end
Expand Down
27 changes: 27 additions & 0 deletions actionpack/test/dispatch/prefix_generation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def klass.name; self end
def to_model; self; end
def persisted?; true; end
end
class Author
extend ActiveModel::Naming

def to_param
"1"
end

def self.model_name
klass = +"Author"
def klass.name; self end

ActiveModel::Name.new(klass)
end

def to_model; self; end
def persisted?; true; end
end

class WithMountedEngine < ActionDispatch::IntegrationTest
class BlogEngine < Rails::Engine
Expand All @@ -46,6 +63,8 @@ class BlogEngine < Rails::Engine
get "/absolute_option_redirect", to: redirect(path: "/foo")
get "/absolute_custom_root", to: redirect { |params, request| "/" }
get "/absolute_custom_redirect", to: redirect { |params, request| "/foo" }

resource :author
end
end

Expand Down Expand Up @@ -327,6 +346,14 @@ def setup
assert_equal "http://www.example.com/awesome/blog/posts/1", path
end

test "[OBJECT] generating engine's route with polymorphic_url from singular resource" do
path = engine_object.polymorphic_path(Author.new)
assert_equal "/awesome/blog/author", path

path = engine_object.polymorphic_url(Author.new, host: "www.example.com")
assert_equal "http://www.example.com/awesome/blog/author", path
end

private
def verify_redirect(url, status = 301)
assert_equal status, response.status
Expand Down

0 comments on commit de4ffae

Please sign in to comment.