$ show RubyMoney/money-rails
money-rails is a Ruby gem that integrates the RubyMoney library with Rails, providing a `monetize` macro for ActiveRecord and Mongoid models. It allows developers to map integer cents columns (e.g., `price_cents`) to Money objects, handling currency conversions, validation, formatting, and serialization. The gem includes migration helpers, a custom ActiveModel validator, ActionView helpers, an ActiveJob serializer, test helpers, and configuration for global currency defaults and rounding modes. It supports multiple Rails versions (7.0–8.1), Mongoid 7–9, and Ruby >= 3.1. The codebase is well-structured, extensively tested, and follows Rails plugin conventions with clean separation of concerns.
The LLM identified 4 files worth highlighting for reuse:
A concise ActiveJob serializer for Money objects that serializes cents and currency as a hash. It demonstrates the correct pattern for implementing ActiveJob::Serializers::ObjectSerializer with `serialize?`, `serialize`, `deserialize`, and `klass` methods. Fully self-contained and reusable in any Rails app that uses Money with ActiveJob.
A custom RSpec matcher (and Minitest-compatible) `monetize` that tests whether a model attribute is monetized correctly, supporting chains like `.with_currency`, `.as`, `.allow_nil`, and `.with_model_currency`. It is cleanly separated from the rest of the gem and can be extracted to test any monetization library with minimal modification.
A utility class that extracts column options for migration helpers (`add_monetize`, `t.money`). It merges configuration defaults with per-call options, builds column names, and separates column options from meta-keys. Demonstrates a clean, testable extraction pattern for complex migration DSLs.
Implements Mongoid custom field serialization for Money, providing `mongoize`, `demongoize`, and `evolve` class methods. It handles nil, Hash, Numeric, and String inputs gracefully. This pattern is directly reusable for any custom type in Mongoid and demonstrates correct field type implementation.