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

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



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

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

  root.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



126
127
128
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 126

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