class Tapioca::Dsl::Compilers::ActiveStorage
~~~
end
def photo=(attachable); end
sig { params(attachable: T.untyped).returns(T.untyped) }
def photo; end
sig { returns(ActiveStorage::Attached::One) }
def blogs=(attachable); end
sig { params(attachable: T.untyped).returns(T.untyped) }
def blogs; end
sig { returns(ActiveStorage::Attached::Many) }
class Post
# typed: strong
~~~rbi
this compiler will produce the RBI file ‘post.rbi` with the following content:
~~~
end
has_many_attached :blogs
has_one_attached :photo
class Post < ApplicationRecord
~~~rb
For example, with the following `ActiveRecord::Base` subclass:
or [many](edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached) attachments.
`ActiveRecord::Base` that declare [one](edgeguides.rubyonrails.org/active_storage_overview.html#has-one-attached)
`Tapioca::Dsl::Compilers::ActiveStorage` decorates RBI files for subclasses of
def decorate
def decorate 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
def gather_constants
def gather_constants descendants_of(::ActiveRecord::Base) .reject(&:abstract_class?) .grep(::ActiveStorage::Reflection::ActiveRecordExtensions::ClassMethods) end
def type_of(reflection)
def type_of(reflection) case reflection when ::ActiveStorage::Reflection::HasOneAttachedReflection "ActiveStorage::Attached::One" when ::ActiveStorage::Reflection::HasManyAttachedReflection "ActiveStorage::Attached::Many" else "T.untyped" end end