Skip to content

[HOLD] [FCM-8671] Rails 8 update#4

Open
arman-radicle wants to merge 31 commits into
mainfrom
FCM-8671-rails-8-0-upgrade
Open

[HOLD] [FCM-8671] Rails 8 update#4
arman-radicle wants to merge 31 commits into
mainfrom
FCM-8671-rails-8-0-upgrade

Conversation

@arman-radicle

@arman-radicle arman-radicle commented Jul 23, 2026

Copy link
Copy Markdown

Overview / Approach

  • update gemspec
  • add missing migration for places table
  • update README

Testing

This gem's own specs fully pass:

bundle exec rspec
..........................................

Finished in 0.14754 seconds (files took 0.56241 seconds to load)
42 examples, 0 failures

It's functionality within rma was fully tested in that repo.

Database or Extract changes

N/A

Deployment plan

No deployment, this gem will be installed as part of rma Rails 8 upgrade PR.

HIPAA / Security

N/A

Billing

N/A

rcugut and others added 29 commits January 6, 2022 13:13
handle ruby 3.x keyword arguments delegation
Updates CI to support Ruby 3+ and Rails v7
Updates push gem action with a valid ruby version
Write attribute with polymorphic integer type
Should still able to set other reflection successfully
Upgrade rails to 7.2.x version; clean up unsupported rails and ruby version
* Update to allow for rails 8 compatibility

* update gem version

* modify rakefile to support both sqlite3 and mysql

* modify the test matrix

* cleanup Rakefile

* Use sqlite3 for tests

* update readme

* set version to 3.4

* remove rails 6.x support

* Test rails 7 with ruby 3.0
Rails 8.1 changed `ActiveRecord::PredicateBuilder::PolymorphicArrayValue#initialize`
from `(associated_table, values)` to `(reflection, values)`, dropping the
`@associated_table` ivar that this gem reads via
`@associated_table.send(:reflection)` in `type_to_ids_mapping`. Without an
update, every polymorphic-array predicate query (e.g. `Model.where(thing: [a, b])`)
raises `NoMethodError: undefined method 'reflection' for nil` on Rails 8.1.

The minimal fix: read `@reflection` directly when set (Rails 8.1+), fall back to
`@associated_table.send(:reflection)` for older Rails versions. The rest of the
method runs unchanged.

This also adds Gemfile.rails-8.1-stable to the CI matrix so future regressions
are caught upstream rather than in downstream apps.

## Verification

The existing spec at `polymorphic_integer_type_spec.rb:135` ("properly finds
the object when passing an array of sources") exercises exactly this code path:

  Link.where(source: [source])

Verified locally:
- Rails 8.0: 41/41 passing
- Rails 8.1: 41/41 passing
- Rails 8.1 *without* this fix: 1 failure (the array-of-sources test) —
  proves the test catches the regression and the fix resolves it.

Downstream apps (clio/accounting, clio/manage, clio/themis, etc.) currently
maintain in-app monkey patches for this; bumping to 3.5.0 lets them delete
those.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The existing array-of-sources test only exercises a single-type array
(`Link.where(source: [cat])`). The fix's value also covers multi-type
arrays (`Link.where(source: [cat, dog])`) — a broader branch of
`type_to_ids_mapping` where the result hash spans multiple keys.

Locks in coverage on both Rails 8.0 (backwards compat) and 8.1.

Suggested by code review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Support Rails 8.1 in PolymorphicArrayValueExtension
@arman-radicle arman-radicle changed the title Rails 8 update - update README and gemspec for Rails 8 compatibility; add missing migration for places table [HOLD] [FCM-8671] Rails 8 update Jul 23, 2026
@ihollander ihollander self-assigned this Jul 23, 2026

@ihollander ihollander left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fun with forked dependencies 🙃

Instead of manually patching our fork, should we try to sync our fork with the upstream? It looks like there were a few commits related to Rails 8 compatibility on the upstream (clio#67, clio#68), might be nice to get our fork fully up-to-date as part of this work in case those other compatibility-related changes end up impacting us in ways that aren't covered by our tests...

git remote add upstream git@github.com:clio/polymorphic_integer_type.git
git fetch upstream
git merge upstream/master

I'm also wondering how big of a lift it would be to just stop using this gem altogether - as far as I can tell, we only use it in one place in the rma codebase (here), and it seems like this gem is a pretty common pain point for Rails upgrades... I can make a Jira card for that (separate from the Rails 8 upgrade) if that seems like a worthwhile direction to pursue.

@arman-radicle
arman-radicle force-pushed the FCM-8671-rails-8-0-upgrade branch from e6bf083 to 073022e Compare July 23, 2026 18:26
@arman-radicle

arman-radicle commented Jul 23, 2026

Copy link
Copy Markdown
Author

Our fork is now updated with upstream, my actual changes are:

  • lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb

    • The one big change: the replace_keys guard that sets polymorphic_name for plain polymorphic and base_class for integer-type. This is what keeps rma's Enrollment => 'Patient::Segment' audit-type consistency after the upstream sync.
  • spec/fcm_polymorphic_name_guard_spec.rb

    • New regression spec locking in the guard's behavior (proves polymorphic_name wins over class name)
  • spec/support/country.rb

    • New test fixture for model whose polymorphic_name is 'Nation'
  • spec/support/link.rb

    • Added belongs_to :legacy_source, polymorphic: true + polymorphic_class_for override for the spec
  • spec/support/migrations/10_create_country_table_and_legacy_source_reference.rb

    • New migration backing the fixture
  • spec/spec_helper.rb

    • One line: require 'support/country'
  • README.md

    • FCM annotations

and the rest are upstream churn

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Updates this gem/fork for Rails 8.x compatibility while adding regression coverage for the fork-specific guard that preserves custom polymorphic_name behavior on plain polymorphic associations.

Changes:

  • Add Rails 8.0/8.1 appraisal-style Gemfiles and expand CI matrix to cover Rails 8 with appropriate Ruby exclusions.
  • Introduce new FCM regression fixtures/specs (Country + legacy_source) and adjust test migrations/helpers accordingly.
  • Update gem metadata/docs/changelog to reflect Rails 8 compatibility and the fork delta.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
spec/support/place.rb Removes old Place fixture used for plain polymorphic testing.
spec/support/migrations/11_add_legacy_source_reference_to_links_table.rb Removes legacy migration for legacy_source reference.
spec/support/migrations/10_create_country_table_and_legacy_source_reference.rb Adds new fixture migration for countries + legacy_source columns.
spec/support/link.rb Updates Link fixture to include plain legacy_source association and custom class resolution.
spec/support/country.rb Adds Country fixture overriding polymorphic_name.
spec/spec_helper.rb Simplifies migration execution and updates required fixtures.
spec/polymorphic_integer_type_spec.rb Adds coverage for mixed-type arrays in where(source: [...]); removes older plain polymorphic test.
spec/fcm_polymorphic_name_guard_spec.rb Adds regression specs asserting the fork guard behavior.
README.md Updates setup guidance, DB compatibility notes, and documents the fork delta.
Rakefile Switches DB rake tasks to SQLite-oriented behavior and normalizes formatting.
polymorphic_integer_type.gemspec Updates dependency constraints and Ruby requirement; normalizes gemspec formatting.
lib/polymorphic_integer_type/version.rb Bumps version to 3.5.0.
lib/polymorphic_integer_type/extensions.rb Refactors belongs_to option handling and simplifies association extension hookup.
lib/polymorphic_integer_type/belongs_to_valid_options_extension.rb Removes the (now unnecessary) ActiveRecord valid-options patch.
lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb Reworks _type assignment behavior with the fork-specific guard for plain polymorphic associations.
lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb Updates Rails 8.1 compatibility for polymorphic array predicate building.
lib/polymorphic_integer_type.rb Removes require for deleted valid-options extension.
gemfiles/Gemfile.rails-8.1-stable Adds Rails 8.1 stable branch Gemfile.
gemfiles/Gemfile.rails-8.0-stable Adds Rails 8.0 stable branch Gemfile.
gemfiles/Gemfile.rails-6.1-stable Removes Rails 6.1 stable branch Gemfile.
CHANGELOG.md Adds Rails 8.1 entry and appends Rails 8.0 notes.
.github/workflows/ci.yml Adds Rails 8 gemfiles and expands Ruby matrix with version exclusions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Rakefile
Comment thread README.md
Comment thread CHANGELOG.md
Comment thread CHANGELOG.md

@ihollander ihollander left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-upping approval, thanks for simplifying our patch and adding annotations around the FCM-specific functionality! It'll be nice to have these if we need to re-sync with the upstream again.

Comment on lines +28 to +33
owner[reflection.foreign_type] =
if owner.class.respond_to?("#{reflection.foreign_type}_mapping")
record.class.base_class
else
record.class.polymorphic_name
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is nice, much simpler than the multi-file change that we had before to fix the STI inheritance issue! Checking respond_to?("#{reflection.foreign_type}_mapping") seems like a good signal that the association is using this gem based on this line 👍

@ihollander

Copy link
Copy Markdown

Also made a Jira card, if we want to consider removing our dependency on this gem or trying to get our patch merged in the upstream repo: https://radicle-health.atlassian.net/browse/FCM-8700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants