Module: Errgonomic::Rails::ActiveRecordOptional
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/errgonomic/rails/active_record_optional.rb
Overview
Concern to make ActiveRecord optional attributes and associations return an Option.
Five pragmatic compromises below satisfy ActiveRecord's assumptions about how accessors behave. They are deliberate exceptions to "Option behaves like Rust's Option", and the set is closed: a sixth would be a signal that ActiveRecord is pushing back somewhere unmapped, deserving a design discussion rather than a quiet patch.
- None#nil? answers true, so AR internals and ordinary nil checks treat an absent value as absent. Equality does not follow suit: None() == nil stays false.
- Some delegates persisted?, marked_for_destruction?, and touch_later to its record, so a Some can stand in for it during persistence.
- Quoting and predicate-building prepends unwrap Options at the SQL boundary, so an Option can be passed to where/quote.
- SomeValidator provides a presence-style validation for Option attributes.
- Readers that ActiveRecord's own machinery reads raw are never wrapped: an attribute declared with encrypts, whose length validator sits outside Model.validators and calls to_s on the value, and a singular association with nested attributes, which are assigned through the reader and ask the value whether it is a new record.
errgonomic_optional_except is not on the list: it is configuration, an escape hatch for whatever conflict shows up next, not a semantic exception.