ActiveModel
ActiveRecord’s useful stuff
Gem Spec

• Part of Rails gem package
• Own standalone gem:
 • gem ‘activemodel’
• Dependencies:
 • gem ‘activesupport’
ActiveModel Packages
•   ActiveModel::AttributeMethods
•   ActiveModel::Callbacks
•   ActiveModel::Conversion
•   ActiveModel::Dirty
•   ActiveModel::Naming
•   ActiveModel::Observer
•   ActiveModel::Serialization
•   ActiveModel::Translation
•   ActiveModel::Validations
ActiveModel Packages
•   ActiveModel::AttributeMethods
•   ActiveModel::Callbacks
•   ActiveModel::Conversion
•   ActiveModel::Dirty
•   ActiveModel::Naming
•   ActiveModel::Observer
•   ActiveModel::Serialization
•   ActiveModel::Translation
•   ActiveModel::Validations
Validations

• Most Rails validations originate from ActiveModel
• Including ActiveModel::Validations gives you
 • #valid?
 • #errors
 • Class methods for validation API
Validations API
                     declarative


• validates_acceptance_of    • validates_exclusion_of
• validates_confirmation_of   • validates_length_of
• validates_each             • validates_numericality_of
• validates_format_of        • validates_presence_of
• validates_inclusion_of     • validates_with
Validations API
                  short form



• validates :field_name, :validator => options
Validation API

• validates_each :field_name, &block
• validate :method_name
• validate &block
• validates_with ValidatorClass
Callbacks

• In ActiveRecord, this is how before_create,
  after_create and around_create are made.
• Two different steps: register callbacks and
  execute callbacks.
Observers

• Use observers to decouple logic from
  models.
• Observers use convention to identify
  targets: ModelObserver observes Model
  unless explicitly overridden.
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.
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).
Questions?

Active Model

  • 1.
  • 2.
    Gem Spec • Partof Rails gem package • Own standalone gem: • gem ‘activemodel’ • Dependencies: • gem ‘activesupport’
  • 3.
    ActiveModel Packages • ActiveModel::AttributeMethods • ActiveModel::Callbacks • ActiveModel::Conversion • ActiveModel::Dirty • ActiveModel::Naming • ActiveModel::Observer • ActiveModel::Serialization • ActiveModel::Translation • ActiveModel::Validations
  • 4.
    ActiveModel Packages • ActiveModel::AttributeMethods • ActiveModel::Callbacks • ActiveModel::Conversion • ActiveModel::Dirty • ActiveModel::Naming • ActiveModel::Observer • ActiveModel::Serialization • ActiveModel::Translation • ActiveModel::Validations
  • 5.
    Validations • Most Railsvalidations originate from ActiveModel • Including ActiveModel::Validations gives you • #valid? • #errors • Class methods for validation API
  • 6.
    Validations API declarative • validates_acceptance_of • validates_exclusion_of • validates_confirmation_of • validates_length_of • validates_each • validates_numericality_of • validates_format_of • validates_presence_of • validates_inclusion_of • validates_with
  • 7.
    Validations API short form • validates :field_name, :validator => options
  • 8.
    Validation API • validates_each:field_name, &block • validate :method_name • validate &block • validates_with ValidatorClass
  • 9.
    Callbacks • In ActiveRecord,this is how before_create, after_create and around_create are made. • Two different steps: register callbacks and execute callbacks.
  • 10.
    Observers • Use observersto decouple logic from models. • Observers use convention to identify targets: ModelObserver observes Model unless explicitly overridden.
  • 11.
    Serialization • Allows formodels 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.
  • 12.
    Dirty • ActiveModel providesan 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).
  • 13.

Editor's Notes