-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ActiveStorage getter and setter generator for has_one_attached and has_many_attached #416
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
19acd45
updated activestrage rbi, added an activestorage generator
mojanjz e03bc14
add generator for getter and setters of has_one_attached and has_many…
mojanjz 6ba9d18
add documentation and signature
mojanjz 97728f8
apply refactor pr review changes
mojanjz 50941eb
fix typecheck error and remove activestorage shim
mojanjz 21f9728
change attachable type to T.untyped
mojanjz 7a72255
add association specs for active storage
mojanjz 001a27c
add dummy rails app to run all initializers
mojanjz 2eb41a0
change setter return type to T.untyped
mojanjz 19d83d6
remove unnecessary require in activestorage.rb
mojanjz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "active_storage" | ||
require "active_storage/reflection" | ||
rescue LoadError | ||
return | ||
end | ||
|
||
module Tapioca | ||
module Compilers | ||
module Dsl | ||
# `Tapioca::Compilers::Dsl::ActiveStorage` decorates RBI files for subclasses of | ||
# `ActiveRecord::Base` that declare [one](https://edgeguides.rubyonrails.org/active_storage_overview.html#has-one-attached) | ||
# or [many](https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached) attachments. | ||
# | ||
# For example, with the following `ActiveRecord::Base` subclass: | ||
# | ||
# ~~~rb | ||
# class Post < ApplicationRecord | ||
# has_one_attached :photo | ||
# has_many_attached :blogs | ||
# end | ||
# ~~~ | ||
# | ||
# this generator will produce the RBI file `post.rbi` with the following content: | ||
# | ||
# ~~~rbi | ||
# # typed: strong | ||
# | ||
# class Post | ||
# sig { returns(ActiveStorage::Attached::Many) } | ||
# def blogs; end | ||
# | ||
# sig { params(attachable: T.untyped).returns(T.untyped) } | ||
# def blogs=(attachable); end | ||
# | ||
# sig { returns(ActiveStorage::Attached::One) } | ||
# def photo; end | ||
# | ||
# sig { params(attachable: T.untyped).returns(T.untyped) } | ||
# def photo=(attachable); end | ||
# end | ||
# ~~~ | ||
class ActiveStorage < Base | ||
extend T::Sig | ||
|
||
sig do | ||
override.params(root: RBI::Tree, | ||
constant: T.all(Module, ::ActiveStorage::Reflection::ActiveRecordExtensions::ClassMethods)).void | ||
end | ||
def decorate(root, constant) | ||
return if constant.reflect_on_all_attachments.empty? | ||
|
||
root.create_path(constant) do |scope| | ||
constant.reflect_on_all_attachments.each do |reflection| | ||
type = type_of(reflection) | ||
name = reflection.name.to_s | ||
scope.create_method( | ||
name, | ||
return_type: type | ||
) | ||
scope.create_method( | ||
"#{name}=", | ||
parameters: [create_param("attachable", type: "T.untyped")], | ||
return_type: "T.untyped" | ||
) | ||
end | ||
end | ||
end | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
ActiveRecord::Base.descendants | ||
.reject(&:abstract_class?) | ||
.grep(::ActiveStorage::Reflection::ActiveRecordExtensions::ClassMethods) | ||
end | ||
|
||
private | ||
|
||
sig do | ||
params(reflection: ActiveRecord::Reflection::MacroReflection).returns(String) | ||
end | ||
def type_of(reflection) | ||
paracycle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case reflection | ||
when ::ActiveStorage::Reflection::HasOneAttachedReflection | ||
"ActiveStorage::Attached::One" | ||
when ::ActiveStorage::Reflection::HasManyAttachedReflection | ||
"ActiveStorage::Attached::Many" | ||
else | ||
"T.untyped" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## ActiveStorage | ||
|
||
`Tapioca::Compilers::Dsl::ActiveStorage` decorates RBI files for subclasses of | ||
`ActiveRecord::Base` that declare [one](https://edgeguides.rubyonrails.org/active_storage_overview.html#has-one-attached) | ||
or [many](https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached) attachments. | ||
|
||
For example, with the following `ActiveRecord::Base` subclass: | ||
|
||
~~~rb | ||
class Post < ApplicationRecord | ||
has_one_attached :photo | ||
has_many_attached :blogs | ||
end | ||
~~~ | ||
|
||
this generator will produce the RBI file `post.rbi` with the following content: | ||
|
||
~~~rbi | ||
# typed: strong | ||
|
||
class Post | ||
sig { returns(ActiveStorage::Attached::Many) } | ||
def blogs; end | ||
|
||
sig { params(attachable: T.untyped).returns(T.untyped) } | ||
def blogs=(attachable); end | ||
|
||
sig { returns(ActiveStorage::Attached::One) } | ||
def photo; end | ||
|
||
sig { params(attachable: T.untyped).returns(T.untyped) } | ||
def photo=(attachable); 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems like attachable can be any of the types mentioned here