Skip to content

Commit b943c6a

Browse files
committed
Defer creating the Active Model attribute-method module
There's no need to create the module before we have something to put in it.
1 parent d1deb4e commit b943c6a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

activemodel/lib/active_model/attribute_methods.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def define_attribute_method_pattern(pattern, attr_name, owner:, as:, override: f
373373
# person.name_short? # => NoMethodError
374374
# person.first_name # => NoMethodError
375375
def undefine_attribute_methods
376-
generated_attribute_methods.module_eval do
376+
@generated_attribute_methods&.module_eval do
377377
undef_method(*instance_methods)
378378
end
379379
attribute_method_patterns_cache.clear
@@ -402,7 +402,7 @@ def generated_attribute_methods
402402
end
403403

404404
def instance_method_already_implemented?(method_name)
405-
generated_attribute_methods.method_defined?(method_name)
405+
@generated_attribute_methods&.method_defined?(method_name)
406406
end
407407

408408
# The methods +method_missing+ and +respond_to?+ of this module are

activerecord/test/cases/base_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def test_generated_relation_methods_module_name
116116
assert_equal "Post::GeneratedRelationMethods", mod.inspect
117117
end
118118

119+
def test_no_anonymous_modules
120+
assert_empty Photo.ancestors.select { |m| m.name.nil? }
121+
end
122+
119123
def test_arel_attribute_normalization
120124
assert_equal Post.arel_table["body"], Post.arel_table[:body]
121125
assert_equal Post.arel_table["body"], Post.arel_table[:text]

0 commit comments

Comments
 (0)