Sie sind auf Seite 1von 40

Public

Service Announcement
Randall THOMAS
Bryan LILES
An ActiveModel Extravaganza
ActiveRecord
What is ActiveModel?
= form_for @sound do |f|
%p
= f.label :name
= f.text_field :name

%p= submit_tag
class Sound
include HasherCrasher::Ohm
end
class Sound
include ActiveRecord
end
Awesome HELPERS

External API
You already use ActiveModel
How did they do it?
errors responds to :[],:full_messages
responds to :to_key
class has a model_name
responds to :persisted?
responds to :to_param

YOUR CLASS

The Contract
Helpers
Everyone needs a little Lint
class MyOrmTest < ActiveSupport::TestCase
include ActiveModel::Lint::Tests
# more tests
end
describe MyThing do
it_should_behave_like AnActiveModel
# http://gist.github.com/579839
# include more specs here...
end
errors responds to #[],#full_messages
responds to #to_key
class has a model_name
responds to #persisted?
responds to #to_param
YOUR CLASS

The Contract
TATFT lives. Woo!
Attribute Magic
class MyOrm
include ActiveModel::AttributeMethods
define_attribute_methods [:name, :data]
attr_accessor :name, :data

def attributes
%w[name data].inject({}) do |m,n|
m[n] = send(n);m
end
end
end
Callbacks
class MyOrm
extend ActiveModel::Callbacks
define_model_callbacks :create, :update,
:destroy, :save

before_create :do_some_stuff

def do_some_stuff
end
end
Naming
class RandomSoap
include MyOrm
end

RandomSoap.model_name # => "RandomSoap"


RandomSoap.model_name.human # => "Random
Soap"
Adding Errors
class MyOrm
extend ActiveModel::Naming

def initialize
@errors = ActiveModel::Errors.new(self)
end

attr_accessor :name
attr_reader :errors

#....
end
p = Person.new
p.validate! # => ["can not be nil"]
p.errors.full_messages # => ["name can not
be nil"]
Serialization
class Person
include ActiveModel::Serializers::JSON
attr_accessor :name
def attributes
@attributes || = {'name'} => nil
end
end

person = Person.new
person.to_json
Validation

Validation
class Person
include ActiveModel::Validations

attr_accessor :fn, :ln

# record, attr, value


validates_each :fn, :ln do |r, a, v|
if v.to_s[0] == ?z
r.errors.add a, 'starts with z.'
end
end
end
person = Person.new
person.valid? # => true
person.invalid? # => false

person.fn = 'zoolander'
person.valid? # => false
person.invalid? # => true
person.errors # => #<OrderedHash {:fn=>
["starts with z."]}>
Introspection

Observers

Dirty Objects
@bryanl
Bryan Liles
http://smartic.us
Media Credits
Photo by Stephen Poff - http://flic.kr/p/4ALkWx
http://www.venganza.org/materials/
http://www.youtube.com/watch?v=KBIdcUxdgo0
http://merbist.com/about/
Photo by Stuck in Customs - http://flic.kr/p/7UvckP
Photo by Anita/anubis-/Ana Isabel (ve) - http://flic.kr/p/7yHg6o
Photo by robpatrick - http://flic.kr/p/JbZP
Photo by joanna8555 - http://flic.kr/p/6VPgCT
Photo by shaun wong - http://flic.kr/p/4x5hJ1
Photo by davidlkel - http://flic.kr/p/3ZnEoe
Photo by Sarah Quinn Armitt - http://flic.kr/p/2LkSe
Photo by tarabrown - http://flic.kr/p/8m3vmp
Photo by gentlepurespace - http://flic.kr/p/4B97mX

Photo by wfyurasko - http://flic.kr/p/4vUz21

Photo by -mrsraggle- - http://flic.kr/p/5V4xdo


Photo by Barack Obama - http://flic.kr/p/A5MbH
Photo by Tony2 (NOT IN USE!) - http://flic.kr/p/8p5BiZ
Photo by angela7dreams - http://flic.kr/p/7LAaS

Das könnte Ihnen auch gefallen