Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
More has_one/belongs_to association methods (#344)
Browse files Browse the repository at this point in the history
Adds these:

https://guides.rubyonrails.org/association_basics.html#methods-added-by-belongs-to
https://guides.rubyonrails.org/association_basics.html#methods-added-by-has-one


* More has_one/belongs_to association methods

Adds these:

- https://guides.rubyonrails.org/association_basics.html#methods-added-by-belongs-to
- https://guides.rubyonrails.org/association_basics.html#methods-added-by-has-one

* bump all specs

* Fix broken build

* update pegged sorbet version

* these methods take params

* bump rbi

* use the spec format that matches what srb rbi is generating

* update rbi accordingly
  • Loading branch information
ghiculescu authored Jun 3, 2020
1 parent 344e7d4 commit 8aa6119
Show file tree
Hide file tree
Showing 57 changed files with 956 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/sorbet-rails/model_plugins/active_record_assoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,30 @@ def populate_single_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection
assoc_class = assoc_should_be_untyped?(reflection) ? "T.untyped" : "::#{reflection.klass.name}"
assoc_type = (belongs_to_and_required?(reflection) || has_one_and_required?(reflection)) ? assoc_class : "T.nilable(#{assoc_class})"

params = [
Parameter.new("*args", type: "T.untyped"),
Parameter.new("&block", type: "T.nilable(T.proc.params(object: #{assoc_class}).void)")
]

assoc_module_rbi.create_method(
assoc_name.to_s,
return_type: assoc_type,
)
assoc_module_rbi.create_method(
"build_#{assoc_name}",
parameters: params,
return_type: assoc_class,
)
assoc_module_rbi.create_method(
"create_#{assoc_name}",
parameters: params,
return_type: assoc_class,
)
assoc_module_rbi.create_method(
"create_#{assoc_name}!",
parameters: params,
return_type: assoc_class,
)
assoc_module_rbi.create_method(
"#{assoc_name}=",
parameters: [
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_headmaster.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,30 @@ module Headmaster::GeneratedAssociationMethods
sig { returns(::School) }
def school; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def build_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school!(*args, &block); end

sig { params(value: ::School).void }
def school=(value); end

sig { returns(::Wizard) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: ::Wizard).void }
def wizard=(value); end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/test_data/v5.0/expected_potion.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ module Potion::GeneratedAssociationMethods
sig { returns(T.nilable(::Wizard)) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: T.nilable(::Wizard)).void }
def wizard=(value); end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/test_data/v5.0/expected_robe.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ module Robe::GeneratedAssociationMethods
sig { returns(T.nilable(::Wizard)) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: T.nilable(::Wizard)).void }
def wizard=(value); end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/test_data/v5.0/expected_school.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ module School::GeneratedAssociationMethods
sig { returns(::Headmaster) }
def headmaster; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Headmaster).void)).returns(::Headmaster) }
def build_headmaster(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Headmaster).void)).returns(::Headmaster) }
def create_headmaster(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Headmaster).void)).returns(::Headmaster) }
def create_headmaster!(*args, &block); end

sig { params(value: ::Headmaster).void }
def headmaster=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_spell/habtm_spell_books.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ module Spell::HABTM_SpellBooks::GeneratedAssociationMethods
sig { returns(T.nilable(::Spell)) }
def left_side; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def build_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def create_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def create_left_side!(*args, &block); end

sig { params(value: T.nilable(::Spell)).void }
def left_side=(value); end

sig { returns(::SpellBook) }
def spell_book; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def build_spell_book(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def create_spell_book(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def create_spell_book!(*args, &block); end

sig { params(value: ::SpellBook).void }
def spell_book=(value); end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/test_data/v5.0/expected_spell_book.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ module SpellBook::GeneratedAssociationMethods
sig { returns(::Wizard) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: ::Wizard).void }
def wizard=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_spell_book/habtm_spells.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ module SpellBook::HABTM_Spells::GeneratedAssociationMethods
sig { returns(T.nilable(::SpellBook)) }
def left_side; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def build_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def create_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::SpellBook).void)).returns(::SpellBook) }
def create_left_side!(*args, &block); end

sig { params(value: T.nilable(::SpellBook)).void }
def left_side=(value); end

sig { returns(::Spell) }
def spell; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def build_spell(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def create_spell(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Spell).void)).returns(::Spell) }
def create_spell!(*args, &block); end

sig { params(value: ::Spell).void }
def spell=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_squib.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ module Squib::GeneratedAssociationMethods
sig { returns(T.nilable(::School)) }
def school; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def build_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school!(*args, &block); end

sig { params(value: T.nilable(::School)).void }
def school=(value); end

Expand All @@ -276,6 +285,15 @@ module Squib::GeneratedAssociationMethods
sig { returns(T.nilable(::Wand)) }
def wand; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def build_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand!(*args, &block); end

sig { params(value: T.nilable(::Wand)).void }
def wand=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_subject/habtm_wizards.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ module Subject::HABTM_Wizards::GeneratedAssociationMethods
sig { returns(T.nilable(::Subject)) }
def left_side; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def build_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def create_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def create_left_side!(*args, &block); end

sig { params(value: T.nilable(::Subject)).void }
def left_side=(value); end

sig { returns(T.nilable(::Wizard)) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: T.nilable(::Wizard)).void }
def wizard=(value); end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/test_data/v5.0/expected_wand.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ module Wand::GeneratedAssociationMethods
sig { returns(::Wizard) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: ::Wizard).void }
def wizard=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_wizard.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ module Wizard::GeneratedAssociationMethods
sig { returns(T.nilable(::School)) }
def school; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def build_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school!(*args, &block); end

sig { params(value: T.nilable(::School)).void }
def school=(value); end

Expand All @@ -316,6 +325,15 @@ module Wizard::GeneratedAssociationMethods
sig { returns(T.nilable(::Wand)) }
def wand; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def build_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand!(*args, &block); end

sig { params(value: T.nilable(::Wand)).void }
def wand=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_wizard/habtm_subjects.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ module Wizard::HABTM_Subjects::GeneratedAssociationMethods
sig { returns(T.nilable(::Wizard)) }
def left_side; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_left_side(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_left_side!(*args, &block); end

sig { params(value: T.nilable(::Wizard)).void }
def left_side=(value); end

sig { returns(T.nilable(::Subject)) }
def subject; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def build_subject(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def create_subject(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Subject).void)).returns(::Subject) }
def create_subject!(*args, &block); end

sig { params(value: T.nilable(::Subject)).void }
def subject=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ module Wizard::GeneratedAssociationMethods
sig { returns(T.nilable(T.untyped)) }
def school; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: T.untyped).void)).returns(T.untyped) }
def build_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: T.untyped).void)).returns(T.untyped) }
def create_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: T.untyped).void)).returns(T.untyped) }
def create_school!(*args, &block); end

sig { params(value: T.nilable(T.untyped)).void }
def school=(value); end

Expand All @@ -310,6 +319,15 @@ module Wizard::GeneratedAssociationMethods
sig { returns(T.nilable(::Wand)) }
def wand; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def build_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wand).void)).returns(::Wand) }
def create_wand!(*args, &block); end

sig { params(value: T.nilable(::Wand)).void }
def wand=(value); end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/test_data/v5.1/expected_headmaster.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,30 @@ module Headmaster::GeneratedAssociationMethods
sig { returns(::School) }
def school; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def build_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::School).void)).returns(::School) }
def create_school!(*args, &block); end

sig { params(value: ::School).void }
def school=(value); end

sig { returns(::Wizard) }
def wizard; end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def build_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard(*args, &block); end

sig { params(args: T.untyped, block: T.nilable(T.proc.params(object: ::Wizard).void)).returns(::Wizard) }
def create_wizard!(*args, &block); end

sig { params(value: ::Wizard).void }
def wizard=(value); end
end
Expand Down
Loading

0 comments on commit 8aa6119

Please sign in to comment.