Skip to content

Commit ed873f1

Browse files
authored
Merge pull request rails#49065 from RuhmUndAnsehen/fix-_to_partial_path-model_name
Fix ActiveModel::Conversion._to_partial_path not using a model's model_name.
2 parents 946550b + 3a3951a commit ed873f1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

activemodel/lib/active_model/conversion.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ module ClassMethods # :nodoc:
108108
# Provide a class level cache for #to_partial_path. This is an
109109
# internal method and should not be accessed directly.
110110
def _to_partial_path # :nodoc:
111-
@_to_partial_path ||= begin
111+
@_to_partial_path ||= if respond_to?(:model_name)
112+
"#{model_name.collection}/#{model_name.element}"
113+
else
112114
element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(name))
113115
collection = ActiveSupport::Inflector.tableize(name)
114116
"#{collection}/#{element}"

activemodel/test/cases/conversion_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def persisted?
5858
assert_equal "helicopter/comanches/comanche", Helicopter::Comanche.new.to_partial_path
5959
end
6060

61+
test "to_partial_path handles non-standard model_name" do
62+
assert_equal "attack_helicopters/ah-64", Helicopter::Apache.new.to_partial_path
63+
end
64+
6165
test "#to_param_delimiter allows redefining the delimiter used in #to_param" do
6266
old_delimiter = Contact.param_delimiter
6367
Contact.param_delimiter = "_"

activemodel/test/models/helicopter.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ class Helicopter
77
class Helicopter::Comanche
88
include ActiveModel::Conversion
99
end
10+
11+
class Helicopter::Apache
12+
include ActiveModel::Conversion
13+
14+
class << self
15+
def model_name
16+
@model_name ||= ActiveModel::Name.new(self).tap do |model_name|
17+
model_name.collection = "attack_helicopters"
18+
model_name.element = "ah-64"
19+
end
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)