Module: Errgonomic::Rails::ActiveModelAttributeWrite

Defined in:
lib/errgonomic/rails/active_record_optional.rb

Overview

An attribute writer hands its value to a type cast that has never heard of an Option, and each type fails its own way: a Some is truthy and not one of ActiveModel's FALSE_VALUES, so a wrapped false cast to true. Unwrap before the attribute is built rather than inside the cast, so the value assigned, dirty tracking and the before-type-cast reader all agree on what was assigned. Every writer passes here, as do new, assign_attributes and update.

Instance Method Summary collapse

Instance Method Details

#write_from_user(name, value) ⇒ Object

Examples:

Note.new(pinned: Some(false)).pinned # => Some(false)
Note.new(pinned: None()).pinned # => None()
Note.new(title: Some('The Dark Forest')).title # => Some('The Dark Forest')
Note.new(rank: Some(3)).rank # => Some(3)
Note.new(due_on: Some(Date.new(2026, 7, 31))).due_on # => Some(Date.new(2026, 7, 31))

A wrapper never reaches the attribute behind the reader

Note.new(pinned: Some(false)).attributes['pinned'] # => false
Note.new(pinned: Some(false)).read_attribute_before_type_cast('pinned') # => false


658
659
660
# File 'lib/errgonomic/rails/active_record_optional.rb', line 658

def write_from_user(name, value)
  super(name, Errgonomic::Rails.unwrap_option(value))
end