Skip to content

Commit 1c0e3f6

Browse files
authored
Fix type signature for ActiveRecord::Base.validate (to better support official Ruby documentation) (#859)
* Add unit test for #874 * Backport fix from https://github.com/ruby/gem_rbs_collection/blob/7337c95459850e31a8af86e26d369b7b4c04a91c/gems/activerecord/6.0/activerecord.rbs#L3
1 parent 27ffa6e commit 1c0e3f6

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

gems/activemodel/6.0/activemodel.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module ActiveModel
4646
attr_accessor validation_context: untyped
4747

4848
module ClassMethods
49-
type condition[T] = Symbol | ^(T) [self: T] -> boolish
49+
type condition[T] = Symbol | ^(T) [self: T] -> boolish | Proc
5050
type conditions[T] = condition[T] | Array[condition[T]]
5151

5252
# Validates each attribute against a block.

gems/activemodel/7.1/activemodel.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module ActiveModel
4646
attr_accessor validation_context: untyped
4747

4848
module ClassMethods
49-
type condition[T] = Symbol | ^(T) [self: T] -> boolish
49+
type condition[T] = Symbol | ^(T) [self: T] -> boolish | Proc
5050
type conditions[T] = condition[T] | Array[condition[T]]
5151

5252
# Validates each attribute against a block.

gems/activerecord/7.2/_test/activerecord.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@ class TestCallbackObject < ActiveRecord::Base
77
after_save ::Callbacks::ClassCallback
88
around_create ::Callbacks::ClassCallback
99
around_save ::Callbacks::ClassCallback
10+
11+
def local_condition1
12+
[true, false].sample
13+
end
14+
15+
def local_condition2
16+
[true, false].sample
17+
end
18+
19+
def local_condition3
20+
[true, false].sample
21+
end
22+
23+
# https://guides.rubyonrails.org/v7.2/active_record_validations.html#combining-validation-conditions
24+
RAILS_72_DOCS_EXAMPLE = [
25+
:local_condition1,
26+
->(my_rec) { my_rec.local_condition2 },
27+
Proc.new do |my_rec|
28+
# @type var my_rec: TestCallbackObject
29+
my_rec.local_condition3
30+
end
31+
]
32+
33+
validate :custom_validation, on: :update, unless: RAILS_72_DOCS_EXAMPLE
34+
35+
def custom_validation
36+
if self.record_timestamps?
37+
self.errors.add(:base, 'testing only')
38+
end
39+
end
1040
end
1141

1242
module Callbacks

gems/activerecord/7.2/_test/activerecord.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
class TestCallbackObject < ActiveRecord::Base
2+
def local_condition1: () -> bool
3+
def local_condition2: () -> bool
4+
def local_condition3: () -> bool
5+
6+
RAILS_72_DOCS_EXAMPLE: [Symbol, ^(TestCallbackObject) [self: TestCallbackObject] -> bool, Proc]
7+
8+
def custom_validation: () -> void
29
end
310

411
module Callbacks

0 commit comments

Comments
 (0)