Class: Tapioca::Compilers::Dsl::ActiveRecordRelations::RelationGenerator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Helper::ActiveRecordConstants, ParamHelper, Reflection
Defined in:
lib/tapioca/compilers/dsl/active_record_relations.rb

Constant Summary collapse

ASSOCIATION_METHODS =
T.let(
  ::ActiveRecord::AssociationRelation.instance_methods -
    ::ActiveRecord::Relation.instance_methods,
  T::Array[Symbol]
)
COLLECTION_PROXY_METHODS =
T.let(
  ::ActiveRecord::Associations::CollectionProxy.instance_methods -
    ::ActiveRecord::AssociationRelation.instance_methods,
  T::Array[Symbol]
)
QUERY_METHODS =
T.let(begin
  # Grab all Query methods
  query_methods = ActiveRecord::QueryMethods.instance_methods(false)
  # Grab all Spawn methods
  query_methods |= ActiveRecord::SpawnMethods.instance_methods(false)
  # Remove the ones we know are private API
  query_methods -= [:arel, :build_subquery, :construct_join_dependency, :extensions, :spawn]
  # Remove "where" which needs a custom return type for WhereChains
  query_methods -= [:where]
  # Remove the methods that ...
  query_methods
    .grep_v(/_clause$/) # end with "_clause"
    .grep_v(/_values?$/) # end with "_value" or "_values"
    .grep_v(/=$/) # end with "=""
    .grep_v(/(?<!uniq)!$/) # end with "!" except for "uniq!"
end, T::Array[Symbol])
WHERE_CHAIN_QUERY_METHODS =
T.let(
  ActiveRecord::QueryMethods::WhereChain.instance_methods(false),
  T::Array[Symbol]
)
FINDER_METHODS =
T.let(ActiveRecord::FinderMethods.instance_methods(false), T::Array[Symbol])
CALCULATION_METHODS =
T.let(ActiveRecord::Calculations.instance_methods(false), T::Array[Symbol])
ENUMERABLE_QUERY_METHODS =
T.let([:any?, :many?, :none?, :one?], T::Array[Symbol])
FIND_OR_CREATE_METHODS =
T.let(
  [:find_or_create_by, :find_or_create_by!, :find_or_initialize_by, :create_or_find_by, :create_or_find_by!],
  T::Array[Symbol]
)
BUILDER_METHODS =
T.let([:new, :build, :create, :create!], T::Array[Symbol])

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 Method Summary collapse

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

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

Constructor Details

#initialize(model, constant_name) ⇒ RelationGenerator

Returns a new instance of RelationGenerator.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tapioca/compilers/dsl/active_record_relations.rb', line 180

def initialize(model, constant_name)
  @model = model
  @constant_name = constant_name
  @relation_methods_module = T.let(
    model.create_module(RelationMethodsModuleName),
    RBI::Scope
  )
  @association_relation_methods_module = T.let(
    model.create_module(AssociationRelationMethodsModuleName),
    RBI::Scope
  )
  @common_relation_methods_module = T.let(
    model.create_module(CommonRelationMethodsModuleName),
    RBI::Scope
  )
end

Instance Method Details

#generateObject



198
199
200
201
202
203
# File 'lib/tapioca/compilers/dsl/active_record_relations.rb', line 198

def generate
  create_classes_and_includes
  create_common_methods
  create_relation_methods
  create_association_relation_methods
end