-
Notifications
You must be signed in to change notification settings - Fork 57
gorillib model active_model support
- direct copy of (some) ActiveModel pieces, but without the
require
s.
create active_model_lite
, an upward-compatible bowdlerization of the active_model suite. You include exactly one of them.
- direct from ActiveModel:
- ActiveModel::Errors
- ActiveModel::Translation
- attribute_methods -- no
- callbacks -- from active_model
- configuration -- no
- conversion -- by us
- dirty -- by us
- errors -- from active_model
- lint -- from active_model
- locale -- ??
- mass_assignment_security -- ??
- model -- no
- naming -- by us; uses pluggable inflections (with stupider inflector by default)
- observer_array -- no
- observing -- no
- railtie -- no
- secure_password -- no
- serialization -- from active_model; overrides
read_attribute_for_serialization
, sets associations - serializers -- from active_model
- translation -- from active_model
- validations -- from active_model; can reshape to omit dependencies on translation, configuration
- validator -- from active_model
- version --
a1. descendants_tracker -- from active_support
Directly: conversion, naming, dirty Yes: callbacks, errors, lint, serialization/serializers, validations/validators No: attribute_methods, configuration, locale, mass_assignment_security, observing/observer_array, railtie, secure_password, translation
-
include ActiveModel::Dirty
in your object -
Call
define_attribute_methods
passing each method you want to track -
Call
attr_name_will_change!
before each change to the tracked attribute -
If you wish to also track previous changes on save or update, add
@previously_changed = changes
inside of your save or update method. -
A minimal implementation could be:
```ruby class Person include ActiveModel::Dirty define_attribute_methods [:name] def name() @name ; end def name=(val) name_will_change! unless val == @name @name = val end def save @previously_changed = changes @changed_attributes.clear end end ```
can come across fairly directly.
- to call
to_xml
, you must provideArray#to_xml
yourself.