-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add relation type to railtie and compiler to generate it into rbis
- Loading branch information
1 parent
e80b65d
commit a0ee861
Showing
7 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
boba (0.0.12) | ||
boba (0.0.13) | ||
sorbet-static-and-runtime (~> 0.5) | ||
tapioca (<= 0.16.5) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
# frozen_string_literal: true | ||
|
||
module Boba | ||
VERSION = "0.0.12" | ||
VERSION = "0.0.13" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(ActiveRecord::Base) | ||
|
||
require "tapioca/dsl/helpers/active_record_constants_helper" | ||
require "tapioca/dsl/compilers/active_record_relations" | ||
|
||
return unless defined?(Tapioca::Dsl::Compilers::ActiveRecordRelations) | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
# `Tapioca::Dsl::Compilers::ActiveRecordRelationTypes` extends `Tapioca::Dsl::Compilers::ActiveRecordRelationTypes` | ||
# to generate a `RelationType` type alias for each class. This type alias is defined a runtime through the Boba | ||
# railtie, and is useful for typing signatures to accept or return relations. For instance, with the following | ||
# `ActiveRecord::Base` subclass: | ||
# ~~~rb | ||
# class Post < ApplicationRecord | ||
# end | ||
# ~~~ | ||
# | ||
# This compiler will produce the RBI file `post.rbi` with the following content: | ||
# ~~~rbi | ||
# # post.rbi | ||
# # typed: true | ||
# | ||
# class Post | ||
# RelationType = T.any(PrivateRelation, PrivateAssociationRelation, PrivateCollectionProxy) | ||
# end | ||
# ~~~ | ||
# So that the following method will accept any of the private relation types as an argument: | ||
# ~~~rb | ||
# sig { params(posts: Post::RelationType).void } | ||
# def process_posts(posts) | ||
# # ... | ||
# end | ||
# ~~~ | ||
class ActiveRecordRelationTypes < Compiler | ||
extend T::Sig | ||
|
||
ConstantType = type_member { { fixed: T.class_of(::ActiveRecord::Base) } } | ||
|
||
sig { override.void } | ||
def decorate | ||
root.create_path(constant) do |rbi_class| | ||
relation_type_alias = "T.any(" \ | ||
"#{Tapioca::Dsl::Helpers::ActiveRecordConstantsHelper::RelationClassName}, " \ | ||
"#{Tapioca::Dsl::Helpers::ActiveRecordConstantsHelper::AssociationRelationClassName}, " \ | ||
"#{Tapioca::Dsl::Helpers::ActiveRecordConstantsHelper::AssociationsCollectionProxyClassName}" \ | ||
")" | ||
rbi_class.create_type_variable("RelationType", type: "T.type_alias { #{relation_type_alias} }") | ||
end | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
Tapioca::Dsl::Compilers::ActiveRecordRelations.gather_constants | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters