Skip to content

gorillib model active_model support

Philip (flip) Kromer edited this page May 9, 2012 · 1 revision

gorillib/model -- active_model integration

  • direct copy of (some) ActiveModel pieces, but without the requires.

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
  1. attribute_methods -- no
  2. callbacks -- from active_model
  3. configuration -- no
  4. conversion -- by us
  5. dirty -- by us
  6. errors -- from active_model
  7. lint -- from active_model
  8. locale -- ??
  9. mass_assignment_security -- ??
  10. model -- no
  11. naming -- by us; uses pluggable inflections (with stupider inflector by default)
  12. observer_array -- no
  13. observing -- no
  14. railtie -- no
  15. secure_password -- no
  16. serialization -- from active_model; overrides read_attribute_for_serialization, sets associations
  17. serializers -- from active_model
  18. translation -- from active_model
  19. validations -- from active_model; can reshape to omit dependencies on translation, configuration
  20. validator -- from active_model
  21. 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

Dirty

  • 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
    ```
    

Errors

can come across fairly directly.

  • to call to_xml, you must provide Array#to_xml yourself.
Clone this wiki locally