Class: Tapioca::Compilers::Dsl::ActiveRecordAssociations
- Extended by:
- T::Sig
- Includes:
- Helper::ActiveRecordConstants
- 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 ; end
sig { params(value: T.nilable(::User)).void }
def (value); end
sig { params(attributes: T.untyped).returns(T.untyped) }
def (attributes); end
sig { params(args: T.untyped, blk: T.untyped).returns(::User) }
def (*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 (*args, &blk); end
sig { params(args: T.untyped, blk: T.untyped).returns(::User) }
def (*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 ; end
sig { returns(T.nilable(::Category)) }
def reload_category; end
end
end ~~~
Defined Under Namespace
Classes: MissingConstantError, SourceReflectionError
Constant Summary collapse
- ReflectionType =
T.type_alias do T.any(::ActiveRecord::Reflection::ThroughReflection, ::ActiveRecord::Reflection::AssociationReflection) end
Constants included from Helper::ActiveRecordConstants
Helper::ActiveRecordConstants::AssociationMethodsModuleName, Helper::ActiveRecordConstants::AssociationRelationClassName, Helper::ActiveRecordConstants::AssociationRelationMethodsModuleName, Helper::ActiveRecordConstants::AssociationRelationWhereChainClassName, Helper::ActiveRecordConstants::AssociationsCollectionProxyClassName, Helper::ActiveRecordConstants::AttributeMethodsModuleName, Helper::ActiveRecordConstants::CommonRelationMethodsModuleName, Helper::ActiveRecordConstants::RelationClassName, Helper::ActiveRecordConstants::RelationMethodsModuleName, Helper::ActiveRecordConstants::RelationWhereChainClassName
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, #generator_enabled?, #handles?, #initialize, resolve
Methods included from ParamHelper
#create_block_param, #create_kw_opt_param, #create_kw_param, #create_kw_rest_param, #create_opt_param, #create_param, #create_rest_param, #create_typed_param
Methods included from Reflection
#ancestors_of, #are_equal?, #class_of, #constantize, #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
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 127 def decorate(root, constant) return if constant.reflections.empty? root.create_path(constant) do |model| model.create_module(AssociationMethodsModuleName) do |mod| populate_nested_attribute_writers(mod, constant) populate_associations(mod, constant) end model.create_include(AssociationMethodsModuleName) end end |
#gather_constants ⇒ Object
141 142 143 |
# File 'lib/tapioca/compilers/dsl/active_record_associations.rb', line 141 def gather_constants descendants_of(::ActiveRecord::Base).reject(&:abstract_class?) end |