Active Model

662 views
614 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
662
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Active Model

    1. 1. ActiveModelActiveRecord’s useful stuff
    2. 2. Gem Spec• Part of Rails gem package• Own standalone gem: • gem ‘activemodel’• Dependencies: • gem ‘activesupport’
    3. 3. ActiveModel Packages• ActiveModel::AttributeMethods• ActiveModel::Callbacks• ActiveModel::Conversion• ActiveModel::Dirty• ActiveModel::Naming• ActiveModel::Observer• ActiveModel::Serialization• ActiveModel::Translation• ActiveModel::Validations
    4. 4. ActiveModel Packages• ActiveModel::AttributeMethods• ActiveModel::Callbacks• ActiveModel::Conversion• ActiveModel::Dirty• ActiveModel::Naming• ActiveModel::Observer• ActiveModel::Serialization• ActiveModel::Translation• ActiveModel::Validations
    5. 5. Validations• Most Rails validations originate from ActiveModel• Including ActiveModel::Validations gives you • #valid? • #errors • Class methods for validation API
    6. 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. 7. Validations API short form• validates :field_name, :validator => options
    8. 8. Validation API• validates_each :field_name, &block• validate :method_name• validate &block• validates_with ValidatorClass
    9. 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. 10. Observers• Use observers to decouple logic from models.• Observers use convention to identify targets: ModelObserver observes Model unless explicitly overridden.
    11. 11. 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.
    12. 12. 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).
    13. 13. Questions?

    ×