Class: Tapioca::Compilers::Dsl::ActiveStorage

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/compilers/dsl/active_storage.rb

Overview

`Tapioca::Compilers::Dsl::ActiveStorage` decorates RBI files for subclasses of `ActiveRecord::Base` that declare [one](edgeguides.rubyonrails.org/active_storage_overview.html#has-one-attached) or [many](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 ~~~

Constant Summary

Constants included from Reflection

Reflection::ANCESTORS_METHOD, Reflection::CLASS_METHOD, Reflection::CONSTANTS_METHOD, Reflection::EQUAL_METHOD, Reflection::METHOD_METHOD, Reflection::NAME_METHOD, Reflection::OBJECT_ID_METHOD, Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Reflection::SINGLETON_CLASS_METHOD, Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Base

#errors, #processable_constants

Instance Method Summary collapse

Methods inherited from Base

#add_error, #handles?, #initialize

Methods included from Reflection

#ancestors_of, #are_equal?, #class_of, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Constructor Details

This class inherits a constructor from Tapioca::Compilers::Dsl::Base

Instance Method Details

#decorate(root, constant) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tapioca/compilers/dsl/active_storage.rb', line 53

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

#gather_constantsObject


74
75
76
77
78
# File 'lib/tapioca/compilers/dsl/active_storage.rb', line 74

def gather_constants
  descendants_of(::ActiveRecord::Base)
    .reject(&:abstract_class?)
    .grep(::ActiveStorage::Reflection::ActiveRecordExtensions::ClassMethods)
end