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 ~~~

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



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

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

  root.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, mod)
      end
    end

    model.create_extend(module_name)
  end
end

#gather_constantsObject



72
73
74
# File 'lib/tapioca/compilers/dsl/active_record_scope.rb', line 72

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