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

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
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 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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tapioca/compilers/dsl/active_record_scope.rb', line 52

def decorate(root, constant)
  scope_method_names = constant.send(:generated_relation_methods).instance_methods(false)
  return if scope_method_names.empty?

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

    model.create_module(module_name) do |mod|
      scope_method_names.each do |scope_method|
        generate_scope_method(scope_method.to_s, mod)
      end
    end

    model.create_extend(module_name)
  end
end

#gather_constantsObject


70
71
72
# File 'lib/tapioca/compilers/dsl/active_record_scope.rb', line 70

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