Class: Tapioca::Compilers::Dsl::ActiveRecordScope

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

Overview

Tapioca::Compilers::Dsl::ActiveRecordScope decorates RBI files for subclasses of ActiveRecord::Base which declare [scope fields](api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope).

For example, with the following ActiveRecord::Base subclass:

~~~rb class Post < ApplicationRecord

scope :public_kind, -> { where.not(kind: 'private') }
scope :private_kind, -> { where(kind: 'private') }

end ~~~

this generator will produce the RBI file post.rbi with the following content:

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

extend GeneratedRelationMethods

module GeneratedRelationMethods
  sig { params(args: T.untyped, blk: T.untyped).returns(T.untyped) }
  def private_kind(*args, &blk); end

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

end ~~~

Constant Summary

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tapioca/compilers/dsl/active_record_scope.rb', line 55

def decorate(root, constant)
  method_names = scope_method_names(constant)

  return if method_names.empty?

  root.create_path(constant) do |model|
    relations_enabled = generator_enabled?("ActiveRecordRelations")

    relation_methods_module = model.create_module(RelationMethodsModuleName)
    assoc_relation_methods_mod = model.create_module(AssociationRelationMethodsModuleName) if relations_enabled

    method_names.each do |scope_method|
      generate_scope_method(
        relation_methods_module,
        scope_method.to_s,
        relations_enabled ? RelationClassName : "T.untyped"
      )

      next unless relations_enabled

      generate_scope_method(
        assoc_relation_methods_mod,
        scope_method.to_s,
        AssociationRelationClassName
      )
    end

    model.create_extend(RelationMethodsModuleName)
  end
end

#gather_constantsObject



87
88
89
# File 'lib/tapioca/compilers/dsl/active_record_scope.rb', line 87

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