Module: Errgonomic::Rails::ActiveModelAttributeDefault

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

Overview

A declared default reaches the record's attribute without passing a writer: it is held as given and cast the first time the attribute is read. Unwrapping where it is declared is the only point above the type, and it keeps the stored default a plain value, as an assigned one is.

Instance Method Summary collapse

Instance Method Details

#attribute(name, type = nil, **options) ⇒ Object

Examples:

DefaultedNote.new.rank # => 0
DefaultedNote.new.title # => nil
ProcDefaultedNote.new.title # => 'Wanderer'


759
760
761
762
# File 'lib/errgonomic/rails/active_record_optional.rb', line 759

def attribute(name, type = nil, **options)
  options[:default] = Errgonomic::Rails.unwrap_option_default(options[:default]) if options.key?(:default)
  super(name, type, **options)
end