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

Decorate ActiveRecord::Batches methods #1335

Merged
merged 1 commit into from
Feb 9, 2023
Merged

Conversation

bdewater
Copy link
Contributor

@bdewater bdewater commented Dec 27, 2022

Motivation

I wanted typing for the yielded values from the methods in https://github.com/rails/rails/blob/main/activerecord/lib/active_record/relation/batches.rb

Partially addresses #1281

Implementation

Mostly straightforward, return types are nilable in case the block form is used, eg:

Post.find_each { |post| do_something(post) }
#=> nil

Post.find_each
#=> #<Enumerator: ...>

Tests

Added tests.

@bdewater bdewater requested a review from a team as a code owner December 27, 2022 16:30
@bdewater
Copy link
Contributor Author

bdewater commented Dec 27, 2022

Seeing some unrelated bundler step failures, eg:

  Resolving dependencies...
  Could not find compatible versions
  
  Because every version of rails depends on RubyGems >= 3.3.13
    and Gemfile-rails-main depends on rails >= 0,
    RubyGems >= 3.3.13 is required.
  So, because current RubyGems version is = 3.1.6,
    version solving has failed.

@bdewater bdewater changed the title Annotate ActiveRecord::Batches methods Decorate ActiveRecord::Batches methods Dec 28, 2022
@st0012
Copy link
Member

st0012 commented Jan 4, 2023

@bdewater Rebasing main should resolve the test failures.

@bdewater
Copy link
Contributor Author

bdewater commented Jan 4, 2023

@st0012 done!

@bdewater bdewater force-pushed the ar-batches branch 4 times, most recently from dc9927e to 142fdbf Compare January 6, 2023 17:16
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.

Thanks it looks good to me. Few minor comments.

lib/tapioca/dsl/compilers/active_record_relations.rb Outdated Show resolved Hide resolved
lib/tapioca/dsl/compilers/active_record_relations.rb Outdated Show resolved Hide resolved
return_type: "T.nilable(T::Enumerable[T::Enumerable[#{constant_name}]])",
)
when :in_batches
use_ranges = ActiveRecord::Batches.instance_method(:find_each).parameters.include?([:key, :use_ranges])
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR but it may be worth investing in a rails_helper similar to https://github.com/Shopify/tapioca/blob/main/lib/tapioca/helpers/sorbet_helper.rb if these keep coming up. We had a similar occurrence in #1327.

@bdewater bdewater force-pushed the ar-batches branch 2 times, most recently from ebe0605 to 9e4409a Compare January 11, 2023 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants