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 (see api.rubyonrails.org/classes/ActiveRecord/Base.html). This generator is only responsible for defining the methods that would be created for the association 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"

end ~~~

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

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

class Post

include Post::GeneratedAssociationMethods

end

module Post::GeneratedAssociationMethods

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

sig { params(value: T.nilable(::User)).void }
def author=(value); 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 { 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(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 ~~~

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Base

#processable_constants

Instance Method Summary collapse

Methods inherited from Base

#handles?, #initialize

Constructor Details

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

Instance Method Details

#decorate(root, constant) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 99

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

  module_name = "#{constant}::GeneratedAssociationMethods"
  root.create_module(module_name) do |mod|
    constant.reflections.each do |association_name, reflection|
      if reflection.collection?
        populate_collection_assoc_getter_setter(mod, constant, association_name, reflection)
      else
        populate_single_assoc_getter_setter(mod, constant, association_name, reflection)
      end
    end
  end

  root.path(constant) do |klass|
    klass.create_include(module_name)
  end
end

#gather_constantsObject



119
120
121
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 119

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