Unify collection #to_comma extensions - #179
Merged
Merged
Conversation
Array, Mongoid::Criteria, and DataMapper::Collection each defined an identical one-liner #to_comma. Extract it into Comma::CollectionExport and include it in all three, removing the duplication. ActiveRecord::Relation keeps its own implementation since it selects :find_each vs :each based on limit/order and logs a warning. Fixes #165 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The refactor cleanly removes duplication while preserving the existing to_comma behavior and ensures load order correctness via updated requires.
Pull request overview
This PR removes duplicated #to_comma implementations across non-ActiveRecord collection integrations by extracting the shared one-liner into a single Comma::CollectionExport module, keeping ActiveRecord::Relation#to_comma untouched due to its special iterator-selection behavior.
Changes:
- Introduces
Comma::CollectionExport#to_commaas the shared implementation. - Updates
Array,Mongoid::Criteria, andDataMapper::Collectionextensions toinclude Comma::CollectionExportinstead of redefiningto_comma. - Adjusts
lib/comma.rbrequire ordering socollection_exportloads before extensions that include it, and updates RuboCop todo excludes accordingly.
File summaries
| File | Description |
|---|---|
| lib/comma/collection_export.rb | Adds shared to_comma implementation for collection-like types. |
| lib/comma/array.rb | Replaces inline to_comma with include Comma::CollectionExport. |
| lib/comma/mongoid.rb | Replaces inline to_comma with include Comma::CollectionExport for Mongoid criteria. |
| lib/comma/data_mapper_collection.rb | Replaces inline to_comma with include Comma::CollectionExport for DataMapper collections. |
| lib/comma.rb | Requires collection_export before extensions that depend on it. |
| .rubocop_todo.yml | Excludes new lib file from Style/Documentation to match existing project pattern. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace the duplicated
#to_commaone-liner onArray,Mongoid::Criteria, andDataMapper::Collectionwith a sharedComma::CollectionExportmodule.Why
Three files implemented the exact same method body. Extracting it into one module removes the duplication without touching
ActiveRecord::Relation, which has genuinely different logic (:find_eachvs:eachselection based on limit/order, plus a warning log) and stays separate.Fixes #165
Changes
lib/comma/collection_export.rbwithComma::CollectionExport#to_commalib/comma/array.rb,lib/comma/mongoid.rb,lib/comma/data_mapper_collection.rbnowinclude Comma::CollectionExportinstead of defining their ownto_commalib/comma.rbsocollection_exportloads beforedata_mapper_collection(which includes it eagerly at load time)lib/comma/collection_export.rbto theStyle/Documentationexclude list in.rubocop_todo.yml, matching the existing convention for this project's lib filesNo public API change;
ActiveRecord::Relation#to_commais unaffected.Test plan
bundle exec rspec— 57 examples, 0 failuresbundle exec rubocop— no offensesspec/comma/rails/mongoid_spec.rbandspec/comma/rails/data_mapper_collection_spec.rbare pre-existing and gated behindif defined? Mongoid/DataMapper; neither gem is a dependency anywhere in this project, so these specs don't execute here or in CI (unrelated to this change)🤖 Generated with Claude Code