Class: Tapioca::Dsl::Compilers::ActiveRecordFixtures

Inherits:
Tapioca::Dsl::Compiler show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/compilers/active_record_fixtures.rb

Overview

‘Tapioca::Dsl::Compilers::ActiveRecordFixtures` decorates RBIs for test fixture methods that are created dynamically by Rails.

For example, given an application with a posts table, we can have a fixture file

~~~yaml first_post:

author: John
title: My post

~~~

Rails will allow us to invoke ‘posts(:first_post)` in tests to get the fixture record. The generated RBI by this compiler will produce the following

~~~rbi # test_case.rbi # typed: true class ActiveSupport::TestCase

sig { params(fixture_names: Symbol).returns(T.untyped) }
def posts(*fixture_names); end

end ~~~

Constant Summary collapse

ConstantType =
type_member(fixed: T.class_of(ActiveSupport::TestCase))

Constants included from Runtime::Reflection

Runtime::Reflection::ANCESTORS_METHOD, Runtime::Reflection::CLASS_METHOD, Runtime::Reflection::CONSTANTS_METHOD, Runtime::Reflection::EQUAL_METHOD, Runtime::Reflection::METHOD_METHOD, Runtime::Reflection::NAME_METHOD, Runtime::Reflection::OBJECT_ID_METHOD, Runtime::Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Runtime::Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Runtime::Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Runtime::Reflection::SINGLETON_CLASS_METHOD, Runtime::Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Tapioca::Dsl::Compiler

#constant, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tapioca::Dsl::Compiler

#add_error, #compiler_enabled?, handles?, #initialize, processable_constants

Methods included from T::Generic::TypeStoragePatch

#[], #type_member, #type_template

Methods included from Runtime::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

Methods included from Helpers::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

Constructor Details

This class inherits a constructor from Tapioca::Dsl::Compiler

Class Method Details

.gather_constantsObject



62
63
64
# File 'lib/tapioca/dsl/compilers/active_record_fixtures.rb', line 62

def self.gather_constants
  [ActiveSupport::TestCase]
end

Instance Method Details

#decorateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tapioca/dsl/compilers/active_record_fixtures.rb', line 44

def decorate
  method_names = fixture_loader.ancestors # get all ancestors from class that includes AR fixtures
    .drop(1) # drop the anonymous class itself from the array
    .reject(&:name) # only collect anonymous ancestors because fixture methods are always on an anonymous module
    .map! do |mod|
      [mod.private_instance_methods(false), mod.instance_methods(false)]
    end
    .flatten # merge methods into a single list
  return if method_names.empty?

  root.create_path(constant) do |mod|
    method_names.each do |name|
      create_fixture_method(mod, name.to_s)
    end
  end
end