Your SlideShare is downloading. ×
  • Like
Active Model
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Active Model

  • 502 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
502
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
9
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. ActiveModelActiveRecord’s useful stuff
  • 2. Gem Spec• Part of 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 Rails validations 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 observers to decouple logic from models.• Observers use convention to identify targets: ModelObserver observes Model unless explicitly overridden.
  • 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. 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. Questions?