Class: Tapioca::Compilers::Dsl::ActiveModelSecurePassword

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

Overview

`Tapioca::Compilers::Dsl::ActiveModelSecurePassword` decorates RBI files for all classes that use [`ActiveModel::SecurePassword`](api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html).

For example, with the following class:

~~~rb class User

include ActiveModel::SecurePassword

has_secure_password
has_secure_password :token

end ~~~

this generator will produce an RBI file with the following content: ~~~rbi # typed: true

class User

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def authenticate(unencrypted_password); end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def authenticate_password(unencrypted_password); end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def authenticate_token(unencrypted_password); end

sig { returns(T.untyped) }
def password; end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def password=(unencrypted_password); end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def password_confirmation=(unencrypted_password); end

sig { returns(T.untyped) }
def token; end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def token=(unencrypted_password); end

sig { params(unencrypted_password: T.untyped).returns(T.untyped) }
def token_confirmation=(unencrypted_password); 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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tapioca/compilers/dsl/active_model_secure_password.rb', line 68

def decorate(root, constant)
  instance_methods_modules = if constant < ActiveModel::SecurePassword::InstanceMethodsOnActivation
    # pre Rails 6.0, this used to be a single static module
    [ActiveModel::SecurePassword::InstanceMethodsOnActivation]
  else
    # post Rails 6.0, this is now using a dynmaic module builder pattern
    # and we can have multiple different ones included into the model
    constant.ancestors.grep(ActiveModel::SecurePassword::InstanceMethodsOnActivation)
  end

  return if instance_methods_modules.empty?

  methods = instance_methods_modules.flat_map { |mod| mod.instance_methods(false) }
  return if methods.empty?

  root.create_path(constant) do |klass|
    methods.each do |method|
      create_method_from_def(klass, constant.instance_method(method))
    end
  end
end

#gather_constantsObject


91
92
93
94
95
96
97
# File 'lib/tapioca/compilers/dsl/active_model_secure_password.rb', line 91

def gather_constants
  # This selects all classes that are `ActiveModel::SecurePassword::ClassMethods === klass`.
  # In other words, we select all classes that have `ActiveModel::SecurePassword::ClassMethods`
  # as an ancestor of its singleton class, i.e. all classes that have extended the
  # `ActiveModel::SecurePassword::ClassMethods` module.
  all_classes.grep(::ActiveModel::SecurePassword::ClassMethods)
end