Validations
• Most Rails validations originate from ActiveModel
• Including ActiveModel::Validations gives you
• #valid?
• #errors
• Class methods for validation API
Sunday, January 29, 12
Validations API
short form
• validates :field_name, :validator => options
Sunday, January 29, 12
Slide 8
Slide 8 text
Validation API
• validates_each :field_name, &block
• validate :method_name
• validate &block
• validates_with ValidatorClass
Sunday, January 29, 12
Slide 9
Slide 9 text
Callbacks
• In ActiveRecord, this is how before_create,
after_create and around_create are made.
• Two different steps: register callbacks and
execute callbacks.
Sunday, January 29, 12
Slide 10
Slide 10 text
Observers
• Use observers to decouple logic from
models.
• Observers use convention to identify
targets: ModelObserver observes Model
unless explicitly overridden.
Sunday, January 29, 12
Slide 11
Slide 11 text
Serialization
• Allows for models to define how they
would like to be serialized in a variety of
formats (including XML and JSON).
• Important for creation of presenters that
get sent as JSON in RESTful API.
Sunday, January 29, 12
Slide 12
Slide 12 text
Dirty
• ActiveModel provides an API for detecting
whether an object is “dirty” or not
(changed).
• Could be useful for operations that may or
may not be idempotent and you need to
know which (e.g. marking a message as
read).
Sunday, January 29, 12