Skip to content

Commit 2d1b7ab

Browse files
committed
type belongs_to_default in activerecord version 7.2
1 parent bfbac61 commit 2d1b7ab

5 files changed

Lines changed: 12 additions & 3 deletions

File tree

gems/activerecord/7.2/_test/activerecord-7.2.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class User < ApplicationRecord
3434
end
3535

3636
class Article < ApplicationRecord
37+
belongs_to :user, default: -> { default_user }
38+
39+
def default_user
40+
User.first
41+
end
3742
end
3843

3944
User.where.missing.to_sql

gems/activerecord/7.2/_test/activerecord-7.2.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ module Test
2121
class ActiveRecord_Associations_CollectionProxy < ::ActiveRecord::Associations::CollectionProxy
2222
include ::ActiveRecord::Relation::Methods[Article, Integer]
2323
end
24+
25+
def default_user: () -> User?
2426
end
2527
end

gems/activerecord/7.2/activerecord.rbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ module ActiveRecord
4242
extend ::ActiveRecord::Validations::ClassMethods
4343
extend ::ActiveSupport::Callbacks::ClassMethods
4444

45+
type belongs_to_default[T] = ^(T) [self: T] -> ::ActiveRecord::Base?
46+
4547
def self.abstract_class=: (bool) -> void
4648
def self.scope: (Symbol, Proc) ?{ () -> untyped } -> void
47-
def self.belongs_to: (Symbol, ?untyped, **untyped) -> void
49+
def self.belongs_to: (Symbol, ?untyped, **untyped, ?default: belongs_to_default[instance]) -> void
4850
def self.has_many: (Symbol, ?untyped, **untyped) -> void
4951
def self.has_one: (Symbol, ?untyped, **untyped) -> void
5052
def self.has_and_belongs_to_many: (untyped name, ?untyped? scope, **untyped options) ?{ () -> untyped } -> untyped

gems/activerecord/8.0/_test/activerecord-8.0.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Article < ApplicationRecord
3737
belongs_to :user, default: -> { default_user }
3838

3939
def default_user
40-
User.first!
40+
User.first
4141
end
4242
end
4343

gems/activerecord/8.0/_test/activerecord-8.0.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module Test
2222
include ::ActiveRecord::Relation::Methods[Article, Integer]
2323
end
2424

25-
def default_user: () -> User
25+
def default_user: () -> User?
2626
end
2727
end

0 commit comments

Comments
 (0)