Skip to content

Commit acf3f2b

Browse files
committed
Enable Lint/NestedMethodDefinition cop
This would have prevented the nested test mistake in rails#51826. Unfortunately there is a few false positive that need to be opted out, but overall I think it's a win.
1 parent f5b548e commit acf3f2b

File tree

8 files changed

+27
-16
lines changed

8 files changed

+27
-16
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ Layout/TrailingWhitespace:
215215
Style/RedundantPercentQ:
216216
Enabled: true
217217

218+
Lint/NestedMethodDefinition:
219+
Enabled: true
220+
218221
Lint/AmbiguousOperator:
219222
Enabled: true
220223

actionpack/test/controller/params_wrapper_test.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ def test_handles_empty_content_type
257257
end
258258

259259
def test_derived_wrapped_keys_from_nested_attributes
260-
def User.nested_attributes_options
260+
assert_not_respond_to User, :nested_attributes_options
261+
User.define_singleton_method(:nested_attributes_options) do
261262
{ person: {} }
262263
end
263264

@@ -268,6 +269,8 @@ def User.nested_attributes_options
268269
assert_parameters("username" => "sikachu", "person_attributes" => { "title" => "Developer" }, "user" => { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } })
269270
end
270271
end
272+
ensure
273+
User.singleton_class.undef_method(:nested_attributes_options)
271274
end
272275
end
273276

actionview/test/template/log_subscriber_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setup
2222

2323
unless Rails.respond_to?(:root)
2424
@defined_root = true
25-
def Rails.root; :defined_root; end # Minitest `stub` expects the method to be defined.
25+
Rails.define_singleton_method(:root) { :defined_root } # Minitest `stub` expects the method to be defined.
2626
end
2727
end
2828

activemodel/lib/active_model/lint.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Tests
3030
# of the model, and is used to a generate unique DOM id for the object.
3131
def test_to_key
3232
assert_respond_to model, :to_key
33-
def model.persisted?() false end
33+
def_method(model, :persisted?) { false }
3434
assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
3535
end
3636

@@ -45,8 +45,8 @@ def model.persisted?() false end
4545
# any of the possible implementation strategies on the implementer.
4646
def test_to_param
4747
assert_respond_to model, :to_param
48-
def model.to_key() [1] end
49-
def model.persisted?() false end
48+
def_method(model, :to_key) { [1] }
49+
def_method(model, :persisted?) { false }
5050
assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false"
5151
end
5252

@@ -105,6 +105,10 @@ def test_errors_aref
105105
end
106106

107107
private
108+
def def_method(receiver, name, &block)
109+
::Object.instance_method(:define_singleton_method).bind_call(receiver, name, &block)
110+
end
111+
108112
def model
109113
assert_respond_to @model, :to_model
110114
@model.to_model

activerecord/test/cases/associations/eager_test.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,6 @@ def test_polymorphic_type_condition
10261026
end
10271027

10281028
def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
1029-
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
1030-
def assert_equal_after_sort(item1, item2, item3 = nil)
1031-
assert_equal(item1.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id })
1032-
assert_equal(item3.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id }) if item3
1033-
end
10341029
# Test regular association, association with conditions, association with
10351030
# STI, and association with conditions assured not to be true
10361031
post_types = [:posts, :other_posts, :special_posts]
@@ -1754,4 +1749,10 @@ def test_preloading_has_many_through_with_custom_scope
17541749
def find_all_ordered(klass, include = nil)
17551750
klass.order("#{klass.table_name}.#{klass.primary_key}").includes(include).to_a
17561751
end
1752+
1753+
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
1754+
def assert_equal_after_sort(item1, item2, item3 = nil)
1755+
assert_equal(item1.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id })
1756+
assert_equal(item3.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id }) if item3
1757+
end
17571758
end

activerecord/test/cases/relations_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ def test_only
17301730

17311731
def test_anonymous_extension
17321732
relation = Post.where(author_id: 1).order("id ASC").extending do
1733-
def author
1733+
def author # rubocop:disable Lint/NestedMethodDefinition
17341734
"lifo"
17351735
end
17361736
end

activesupport/test/concern_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ def test_prepended_and_included_class_methods
201201
prepended = Module.new.extend(ActiveSupport::Concern)
202202

203203
@klass.class_eval { @foo = [] }
204-
included.class_methods { def foo; @foo << :included; end }
204+
included.class_methods { def foo; @foo << :included; end } # rubocop:disable Lint/NestedMethodDefinition
205205
@klass.class_eval { def self.foo; super; @foo << :class; end }
206-
prepended.class_methods { def foo; super; @foo << :prepended; end }
206+
prepended.class_methods { def foo; super; @foo << :prepended; end } # rubocop:disable Lint/NestedMethodDefinition
207207

208208
@klass.include included
209209
@klass.prepend prepended

activesupport/test/lazy_load_hooks_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_hook_with_yield_true_afterward
128128

129129
def test_hook_uses_class_eval_when_base_is_a_class
130130
ActiveSupport.on_load(:uses_class_eval) do
131-
def first_wrestler
131+
def first_wrestler # rubocop:disable Lint/NestedMethodDefinition
132132
"John Cena"
133133
end
134134
end
@@ -142,7 +142,7 @@ def first_wrestler
142142
def test_hook_uses_class_eval_when_base_is_a_module
143143
mod = Module.new
144144
ActiveSupport.on_load(:uses_class_eval2) do
145-
def last_wrestler
145+
def last_wrestler # rubocop:disable Lint/NestedMethodDefinition
146146
"Dwayne Johnson"
147147
end
148148
end
@@ -157,7 +157,7 @@ def last_wrestler
157157

158158
def test_hook_uses_instance_eval_when_base_is_an_instance
159159
ActiveSupport.on_load(:uses_instance_eval) do
160-
def second_wrestler
160+
def second_wrestler # rubocop:disable Lint/NestedMethodDefinition
161161
"Hulk Hogan"
162162
end
163163
end

0 commit comments

Comments
 (0)