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

Use type_for_attribute to determine primary_key for find #1851

Merged
merged 1 commit into from May 3, 2024

Conversation

alex-tan
Copy link
Contributor

Motivation

Issue #1845
We use custom ID types for our models. The current sigs for find don't reflect the use of ActiveRecord::Attributes::ClassMethods.attribute

Implementation

I utilized Tapioca::Dsl::Helpers::ActiveModelTypeHelper.type_for which reflects on the signatures of the ActiveModel::Type::Value subclasses to determine what other potential types should be in the signatures.

Tests

I have added tests.

sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])).returns(::Post) }
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])]).returns(T::Enumerable[::Post]) }
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))).returns(::Post) }
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))]).returns(T::Enumerable[::Post]) }
Copy link
Contributor

Choose a reason for hiding this comment

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

The code should not generate a nilable type, since Model.find(nil) will raise:
[...]/activerecord-7.1.3.2/lib/active_record/relation/finder_methods.rb:497:in 'find_with_ids': Couldn't find Model without an ID (ActiveRecord::RecordNotFound)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, this is because the deserialize sig says that's the return value so it's extracting from that. I could do a Regex replace of T.nilable( and )?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I guess that's the quickest way. Keep in mind you could also see ::T.nilable since it uses this:

if type.start_with?("T.nilable(", "::T.nilable(") || type == "T.untyped" || type == "::T.untyped"

@alex-tan alex-tan marked this pull request as ready for review April 8, 2024 12:16
@alex-tan alex-tan requested a review from a team as a code owner April 8, 2024 12:16
@KaanOzkan KaanOzkan added the enhancement New feature or request label Apr 10, 2024
@KaanOzkan
Copy link
Contributor

Thanks @alex-tan, this needs a rebase on main and a CLA agreement using https://github.com/Shopify/tapioca/actions/runs/8599879286/job/23563693229?pr=1851#step:2:12

@alex-tan
Copy link
Contributor Author

Thanks @KaanOzkan. I'm working on getting the CLA signed by my company. I'll get this rebased in the meantime.

@alex-tan alex-tan force-pushed the primary-key-attr-reflection branch from da45252 to c2d409d Compare April 10, 2024 14:11
@andyw8 andyw8 removed their request for review April 11, 2024 16:42
@alex-tan alex-tan force-pushed the primary-key-attr-reflection branch from cf26ef7 to 9c563a2 Compare April 15, 2024 14:19
@alex-tan
Copy link
Contributor Author

alex-tan commented May 2, 2024

I have signed the CLA!

@alex-tan alex-tan requested a review from KaanOzkan May 2, 2024 18:24
Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

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

Strange failure, maybe a rebase will fix it.

@alex-tan alex-tan force-pushed the primary-key-attr-reflection branch from 9c563a2 to 8912c37 Compare May 3, 2024 13:31
@alex-tan
Copy link
Contributor Author

alex-tan commented May 3, 2024

Ah weird. I just rebased again 👍

@KaanOzkan KaanOzkan merged commit 923a1ce into Shopify:main May 3, 2024
16 checks passed
@alex-tan alex-tan deleted the primary-key-attr-reflection branch May 3, 2024 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants