Class: Tapioca::Compilers::Dsl::ActiveRecordAssociations

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

Overview

`Tapioca::Compilers::Dsl::ActiveRecordAssociations` refines RBI files for subclasses of [`ActiveRecord::Base`](api.rubyonrails.org/classes/ActiveRecord/Base.html). This generator is only responsible for defining the methods that would be created for the associations that are defined in the Active Record model.

For example, with the following model class:

~~~rb class Post < ActiveRecord::Base

belongs_to :category
has_many :comments
has_one :author, class_name: "User"

accepts_nested_attributes_for :category, :comments, :author

end ~~~

this generator will produce the following methods in the RBI file `post.rbi`:

~~~rbi # post.rbi # typed: true

class Post

include Post::GeneratedAssociationMethods

module Post::GeneratedAssociationMethods
  sig { returns(T.nilable(::User)) }
  def author; end

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

  sig { params(attributes: T.untyped).returns(T.untyped) }
  def author_attributes=(attributes); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::User) }
  def build_author(*args, &blk); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::Category) }
  def build_category(*args, &blk); end

  sig { returns(T.nilable(::Category)) }
  def category; end

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

  sig { params(attributes: T.untyped).returns(T.untyped) }
  def category_attributes=(attributes); end

  sig { returns(T::Array[T.untyped]) }
  def comment_ids; end

  sig { params(ids: T::Array[T.untyped]).returns(T::Array[T.untyped]) }
  def comment_ids=(ids); end

  sig { returns(::ActiveRecord::Associations::CollectionProxy[::Comment]) }
  def comments; end

  sig { params(value: T::Enumerable[::Comment]).void }
  def comments=(value); end

  sig { params(attributes: T.untyped).returns(T.untyped) }
  def comments_attributes=(attributes); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::User) }
  def create_author(*args, &blk); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::User) }
  def create_author!(*args, &blk); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::Category) }
  def create_category(*args, &blk); end

  sig { params(args: T.untyped, blk: T.untyped).returns(::Category) }
  def create_category!(*args, &blk); end

  sig { returns(T.nilable(::User)) }
  def reload_author; end

  sig { returns(T.nilable(::Category)) }
  def reload_category; end
end

end ~~~

Constant Summary collapse

ReflectionType =
T.type_alias do
  T.any(::ActiveRecord::Reflection::ThroughReflection, ::ActiveRecord::Reflection::AssociationReflection)
end

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


108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 108

def decorate(root, constant)
  return if constant.reflections.empty?

  root.create_path(constant) do |model|
    module_name = "GeneratedAssociationMethods"

    model.create_module(module_name) do |mod|
      populate_nested_attribute_writers(mod, constant)
      populate_associations(mod, constant)
    end

    model.create_include(module_name)
  end
end

#gather_constantsObject


124
125
126
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 124

def gather_constants
  descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end