From 9ca1b4d6e32b84349d9c9cb13257572331b09718 Mon Sep 17 00:00:00 2001 From: Kaan Ozkan Date: Mon, 17 Jun 2024 09:49:21 -0400 Subject: [PATCH 1/2] Check for constant definition before invoking Closes https://github.com/Shopify/tapioca/issues/1913 --- lib/tapioca/dsl/helpers/active_record_column_type_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb b/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb index 6daf7a817..a00e50368 100644 --- a/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb +++ b/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb @@ -102,7 +102,7 @@ def type_for_activerecord_value(column_type) "::Money" when ActiveRecord::Type::Integer "::Integer" - when ActiveRecord::Encryption::EncryptedAttributeType + when defined?(ActiveRecord::Encryption) && ActiveRecord::Encryption::EncryptedAttributeType # Reflect to see if `ActiveModel::Type::Value` is being used first. getter_type = Tapioca::Dsl::Helpers::ActiveModelTypeHelper.type_for(column_type) return getter_type unless getter_type == "T.untyped" From d741d3d4121ad8674479166c2718fb746bc35cf8 Mon Sep 17 00:00:00 2001 From: Kaan Ozkan Date: Mon, 17 Jun 2024 16:59:42 -0400 Subject: [PATCH 2/2] Use lambda to check for constant existence --- .../active_record_column_type_helper.rb | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb b/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb index a00e50368..745be67c1 100644 --- a/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb +++ b/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb @@ -98,11 +98,13 @@ def column_type_for(column_name) sig { params(column_type: T.untyped).returns(String) } def type_for_activerecord_value(column_type) case column_type - when defined?(MoneyColumn) && MoneyColumn::ActiveRecordType + when ->(type) { defined?(MoneyColumn) && MoneyColumn::ActiveRecordType === type } "::Money" when ActiveRecord::Type::Integer "::Integer" - when defined?(ActiveRecord::Encryption) && ActiveRecord::Encryption::EncryptedAttributeType + when ->(type) { + defined?(ActiveRecord::Encryption) && ActiveRecord::Encryption::EncryptedAttributeType === type + } # Reflect to see if `ActiveModel::Type::Value` is being used first. getter_type = Tapioca::Dsl::Helpers::ActiveModelTypeHelper.type_for(column_type) return getter_type unless getter_type == "T.untyped" @@ -128,20 +130,30 @@ def type_for_activerecord_value(column_type) "::String" when ActiveRecord::Type::Serialized serialized_column_type(column_type) - when defined?(ActiveRecord::Normalization::NormalizedValueType) && - ActiveRecord::Normalization::NormalizedValueType + when ->(type) { + defined?(ActiveRecord::Normalization::NormalizedValueType) && + ActiveRecord::Normalization::NormalizedValueType === type + } type_for_activerecord_value(column_type.cast_type) - when defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid) && - ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid + when ->(type) { + defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid) && + ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid === type + } "::String" - when defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Cidr) && - ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Cidr + when ->(type) { + defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Cidr) && + ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Cidr === type + } "::IPAddr" - when defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Hstore) && - ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Hstore + when ->(type) { + defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Hstore) && + ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Hstore === type + } "T::Hash[::String, ::String]" - when defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array) && - ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array + when ->(type) { + defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array) && + ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array === type + } "T::Array[#{type_for_activerecord_value(column_type.subtype)}]" else ActiveModelTypeHelper.type_for(column_type)