Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for constant definition before invoking #1928

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions lib/tapioca/dsl/helpers/active_record_column_type_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting pattern, which answers the question:

Why does Proc respond to #===?! When would you ever call some_proc === some_arg instead of just some_proc.call(some_arg)?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a lot of the #=== implementations are so that you can use them in when clauses.

"::Money"
when ActiveRecord::Type::Integer
"::Integer"
when 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"
Expand All @@ -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)
Expand Down
Loading