2016年2月13日 星期六

ActiveModel: Make Any Ruby Object Feel Like ActiveRecord


  • ActiveModel API

the important thing about the ActiveModel API is that your models can become ActiveModel compliant without using a single line of Rails code
use "include ActiveModel::Lint::Tests” to make sure your model become ActiveModel compliant

  • The Validations System

use "include ActiveModel::Validations” to add validation module to your model

The validations system calls read_attribute_for_validation to get the attribute, but by default, it aliases that method to send, which supports the standard Ruby attribute system of attr accessor

validates_presence_of is using the more primitive validates_with, passing it the validator class, merging in {:attributes => attribute names} into the options passed to the validator

  • Serialization

include ActiveModel::Serialization

ActiveRecord also comes with default serialization for JSON and XML


  • AttributeMethods: Makes it easy to add attributes that are set like table_name :foo
  • Callbacks: ActiveRecord-style lifecycle callbacks.
  • Dirty: Support for dirty tracking
  • Naming: Default implementations of model.model_name, which are used by ActionPack (for instance, when you do render :partial => model
  • Observing: ActiveRecord-style observers
  • StateMachine: A simple state-machine implementation for models
  • Translation: The core translation support

沒有留言:

張貼留言