Skip to content

Support Ruby 3, Rails 7.0 and Mongoid 8, and prepare 2.0.0 - #173

Merged
jdguzman merged 19 commits into
masterfrom
jd/2.0
Dec 2, 2026
Merged

jdguzman merged 19 commits into
masterfrom
jd/2.0

Conversation

@jdguzman

@jdguzman jdguzman commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Brings the changes that so far only lived in the riverock fork back into this repo, gets the test suite running on current Ruby/Rails/Mongoid, and prepares the 2.0.0 release.

2.0.0 supports Ruby 3.1+, Rails 6.1 and 7.0, Mongoid 7 and 8, and ActiveAdmin 2.x. 1.0.0 stays the release for older stacks. ActiveAdmin 3 support will follow in 3.0.0, after a 2-x-stable branch is cut from the 2.0.0 tag for backports.

Changes

From the riverock fork (cherry-picked with their original authors):

  • Don't pass resource_params to build_new_resource. ActiveAdmin assigns them right after building, so relation attributes were assigned twice.
  • collection_size stops loading the documents.
  • primary_key works for subclasses of a document.
  • Select filters use Mongoid's reflection.key instead of association_primary_key, which Mongoid associations don't have. On this repo's master, every association filter raises undefined method 'association_primary_key'.
  • Ruby 3: pass the CSV options as keyword arguments.
  • Rails 7.0 / ActiveAdmin 2.14: apply the resource controller patch in to_prepare (Rails 7 doesn't autoload during initialization), and page through the controller's collection for CSV export instead of in_paginated_batches, which needs ActiveRecord. Like in_paginated_batches, the export skips the query cache.

Found by getting the suite running again:

  • Select filters on has_many associations raised undefined method '_id_eq': reflection.key is _id for has_many. Mongoid only stores the keys of belongs_to and has_and_belongs_to_many associations in the document, so select filters use the stored key for those, has_many and has_one are no longer default filters, and declaring one explicitly raises an error that says why.
  • Paged index pages said "Displaying 1 - 100 of 100" on a 30-per-page index, because Mongoid's count ignores skip and limit. collection_size now passes them, so it counts the current page in the database.

Tests and CI:

  • test_app runs on Rails 6.1 and 7.0, with a Mongoid 8 config. RAILS_VERSION and MONGOID_VERSION pick the versions, and Gemfile.lock is no longer committed, since one lockfile can't cover a matrix.
  • Poltergeist is gone (PhantomJS is long discontinued). No spec needs JavaScript, so Capybara's default driver runs them.
  • ransack-mongoid is pinned to the last commit that supports ransack < 4, which ActiveAdmin 2 requires.
  • A GitHub Actions matrix over Ruby 3.1–4.0, Rails 6.1/7.0 and Mongoid 7.5/8.1, excluding Ruby 3.4+ with Rails 6.1, which Rails 6.1 doesn't support. Codecheck runs on Ruby 3.3.

Release 2.0.0: gemspec bounds (Ruby >= 3.1, mongoid >= 7.0 < 9, activeadmin ~> 2.0, plus csv, which Ruby 3.4 no longer installs by default), the homepage, README requirements, and a CHANGELOG.

Breaking changes

  • Requires Ruby 3.1+, Mongoid 7+, and ActiveAdmin 2.x.
  • build_new_resource no longer passes resource_params (see above).

Testing

Locally, all 27 examples pass on Ruby 3.1, 3.3, 3.4 and 4.0, across the Rails 6.1/7.0 and Mongoid 7.5/8.1 combinations. RuboCop is clean, and gem build produces activeadmin-mongoid-2.0.0.

For comparison, this repo's current master fails most of the suite on the same stack (Ruby 3.3, Rails 6.1, Mongoid 7.5, ActiveAdmin 2.14), because of the association_primary_key errors above.

k1bs and others added 15 commits December 1, 2026 17:07
(cherry picked from commit a66a6e0)
Mongoid associations expose their foreign key as reflection.key, so build the
select filter's search method from it, and fall back to the method name.

(From the riverock fork: 0c79f67, 6143325.)
Ruby 3 no longer converts a trailing hash to keyword arguments, so
CSV.generate_line raised ArgumentError.

(From the riverock fork: 1c73400.)
- Define build_new_resource in to_prepare. ResourceController inherits from
  InheritedResources::Base, which lives in that gem's app/controllers, and
  Rails 7 doesn't autoload during initialization.
- CSVBuilder: ActiveAdmin 2.14 builds CSVs with controller.in_paginated_batches,
  which wraps the loop in ActiveRecord::Base.uncached. Page through the
  controller's paginated_collection instead, and drop the unused @collection.

(From the riverock fork: d6da61c.)
Mongoid's reflection.key is the foreign key for belongs_to, but _id for
has_many, which built a _id_eq search that crashed the index page. Use the
key only for belongs_to, and search other associations by the related
document's id, which ransack-mongoid resolves through the association.
Mongoid's count ignores the criteria's skip and limit, so the index showed
"Displaying 1 - 100 of 100" on a 30-per-page index. Pass skip and limit to
count, which still counts in the database instead of loading the page.
Replace the commented-out argument (and the trailing comma RuboCop flags) with
the reason: ActiveAdmin assigns resource_params right after building.
- Update test_app for Rails 6.1 and 7.0 (the Rails 6 files come from the
  riverock fork), and load Logger before Rails, which Rails 6.1 needs with
  concurrent-ruby 1.3.5.
- Add Mongoid 8 config; read the Mongo host from MONGODB_HOST.
- Pick Rails and Mongoid with RAILS_VERSION and MONGOID_VERSION, and stop
  committing Gemfile.lock, which can't cover the matrix.
- Pin ransack-mongoid to the last commit that supports ransack < 4, and
  Devise to 4.x, which ActiveAdmin 2 requires.
- Drop Poltergeist, whose PhantomJS is long discontinued. No spec needs
  JavaScript, so Capybara's default driver runs them.
- rspec-rails 6.1; pass a String object name to FormBuilder in the filter
  form builder spec, as Rails 6.1+ requires.

Co-authored-by: Nic Boie <boie0025@gmail.com>
Rails 6.1 doesn't support Ruby 3.4 or later, so those combinations are
excluded. Run RuboCop on Ruby 3.3, since the gem now needs Ruby 3.1.
Supports Ruby 3.1+, Rails 6.1 and 7.0, Mongoid 7 and 8, and ActiveAdmin 2.x:
- gemspec: Ruby >= 3.1, mongoid >= 7.0 and < 9, activeadmin ~> 2.0, and csv,
  which Ruby 3.4 no longer installs by default.
- README: 2.0.0 requirements and how to install ransack-mongoid.
- CHANGELOG with the breaking changes and fixes since 1.0.0.
setup-ruby's bundler-cache locked the Gemfile without the job's RAILS_VERSION
and MONGOID_VERSION, so every job except the default Rails 7.0 / Mongoid 8.1
failed to install in frozen mode before any spec ran.
On ubuntu-latest, now 26.04, native extensions fail to build against Ruby 3.2's
headers (unknown type name 'bool'). Pinning the image also keeps a future
runner change from changing the results.
setup-ruby writes a Gemfile.lock for the default Rails 7.0 / Mongoid 8.1 even
without bundler-cache. On Ruby 3.2 and 3.3, Bundler couldn't move that lock to
Rails 6.1 with Mongoid 7.5, though a fresh resolve works.

@boie0025 boie0025 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice work getting the suite running again. The fork changes, the to_prepare move and the collection_size count all look right to me. A few things before this merges:

  1. Gemfile.lock is still committed. The description and the new .gitignore entry say it's no longer committed, but the PR modifies it instead of removing it, and .gitignore has no effect on a file that's already tracked. It needs a git rm --cached Gemfile.lock, or the description needs updating.

  2. has_many select filters may now match nothing instead of raising. For has_many, searchable_method_name returns "#{method}_id", so filter :posts on AdminUser becomes posts_id_eq. As far as I can tell, ransack-mongoid's predicates build { name => value } from the attribute name only, so this queries AdminUser where _id equals the post id, which never matches. The smoke spec only renders /admin/admin_users; nothing submits that filter. Could you add a spec that filters admin users by a post? If it does return nothing, I think removing the filter, or leaving the error in place, is better than an empty result that looks real.

  3. CSV export and the query cache. ActiveAdmin's in_paginated_batches wraps the loop in ActiveRecord::Base.uncached so a large export doesn't keep every page in the query cache. The replacement drops that. With Mongoid's query cache enabled, a big export holds every batch in memory until the request ends. Wrapping the loop in Mongo::QueryCache.uncached { ... } would keep the original behavior.

Smaller things:

  • test_app/config/mongoid.6.yml changes the host to db:27017. That looks like a local Docker setting, and Mongoid 6 isn't supported anymore, so the file could probably just be deleted.
  • Post.ransack(*args) super end in the test app doesn't do anything. If it works around something, please say what; if not, it can go.
  • Most of the new test_app/config/initializers files are generator boilerplate with everything commented out (CSP, application_controller_renderer, new_framework_defaults_6_0). Dropping them would keep the diff to what matters.
  • searchable_has_many_through? can't be true for Mongoid associations, so that branch in select_input.rb never runs.

The test suite commit meant to untrack it but committed a regenerated copy
instead, so the .gitignore entry had no effect.
- Initializers the Rails 6 generator added that the test app doesn't use
  (most are entirely commented out).
- Mongoid 2-6 configs; 2.0 supports Mongoid 7 and 8, and spec/support picks
  the config by major version.
- Post.ransack, which only called super.
ActiveAdmin's in_paginated_batches wraps the export in
ActiveRecord::Base.uncached so a large export doesn't keep every page in
the query cache. The replacement loop dropped that. Wrap it in Mongo's
query cache uncached (Mongoid's on drivers without one).

The CSV spec now also checks the rows are exported.
The has_many filter returned nothing: filter :posts on AdminUser searched
posts_id_eq, which matched AdminUser _id against the post's id. Mongoid
documents only store the keys of belongs_to and has_and_belongs_to_many
associations; has_many and has_one keep theirs in the related documents.

- Select filters use the stored key (reflection.key), as the fork did for
  belongs_to. That also covers has_and_belongs_to_many, which the previous
  change broke.
- has_many and has_one are no longer default filters, and declaring one
  explicitly raises an error that says why, rather than showing an empty
  result.
- Remove the has_many :through branch, which Mongoid associations never
  take.
- Test app: drop filter :posts from admin users; add a
  has_and_belongs_to_many editors filter to posts, with a spec, and specs
  for the default association filters.
@jdguzman

jdguzman commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review. All addressed:

  1. Gemfile.lock (e2400fb): it's untracked now.
  2. has_many filters (50c742f): you were right. A spec that submits filter :posts returned no admin users. Mongoid only stores the keys of belongs_to and has_and_belongs_to_many associations in the document, so:
    • Select filters use the stored key (reflection.key), as the fork did. My "#{method}_id" change would also have broken has_and_belongs_to_many.
    • has_many and has_one are no longer default filters. Declaring one explicitly raises an error that says why, rather than showing an empty result.
    • The test app drops filter :posts and gains a has_and_belongs_to_many :editors filter on posts, with a spec, plus specs for the default association filters.
  3. CSV export (8a23175): the export now runs inside Mongo::QueryCache.uncached, falling back to Mongoid::QueryCache on drivers without it. The CSV spec checks that, and the exported rows.

Smaller things (9f869b1, 50c742f):

  • Deleted the Mongoid 2–6 configs, not just 6.
  • Removed Post.ransack, which only called super.
  • Dropped the unused generator initializers.
  • Removed the has_many :through branch.

Locally, 31 examples pass on Ruby 3.1, 3.3 and 4.0 across Rails 6.1/7.0 and Mongoid 7.5/8.1, and RuboCop is clean.

@boie0025 boie0025 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving. Thanks, all of it is addressed:

  • Gemfile.lock is untracked.
  • Filtering on the stored key (reflection.key) is right for both belongs_to and has_and_belongs_to_many, since editor_ids_eq matches documents whose array contains the id. Dropping has_many/has_one from the default filters, and raising a clear error when one is declared explicitly, is better than an empty result. The new specs cover each case.
  • The CSV export skips the query cache, and the spec checks it.

@jdguzman
jdguzman merged commit e2675c7 into master Dec 2, 2026
17 checks passed
@jdguzman
jdguzman deleted the jd/2.0 branch December 2, 2026 10:26
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.

3 participants