diff options
Diffstat (limited to 'activemodel/README.rdoc')
-rw-r--r-- | activemodel/README.rdoc | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index fc34d69ff6..a399fe9051 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -1,8 +1,8 @@ = Active Model -- model interfaces for Rails Active Model provides a known set of interfaces for usage in model classes. -They allow for Action Pack helpers to interact with non-ActiveRecord models, -for example. Active Model also helps building custom ORMs for use outside of +They allow for Action Pack helpers to interact with non-Active Record models, +for example. Active Model also helps with building custom ORMs for use outside of the Rails framework. Prior to Rails 3.0, if a plugin or gem developer wanted to have an object @@ -11,7 +11,7 @@ code from Rails, or monkey patch entire helpers to make them handle objects that did not exactly conform to the Active Record interface. This would result in code duplication and fragile applications that broke on upgrades. Active Model solves this by defining an explicit API. You can read more about the -API in ActiveModel::Lint::Tests. +API in <tt>ActiveModel::Lint::Tests</tt>. Active Model provides a default module that implements the basic API required to integrate with Action Pack out of the box: <tt>ActiveModel::Model</tt>. @@ -24,8 +24,8 @@ to integrate with Action Pack out of the box: <tt>ActiveModel::Model</tt>. end person = Person.new(name: 'bob', age: '18') - person.name # => 'bob' - person.age # => '18' + person.name # => 'bob' + person.age # => '18' person.valid? # => true It includes model name introspections, conversions, translations and @@ -75,15 +75,19 @@ behavior out of the box: * Tracking value changes - The ActiveModel::Dirty module allows for tracking attribute changes: + class Person + include ActiveModel::Dirty + + attr_accessor :name + end person = Person.new - person.name # => nil - person.changed? # => false + person.name # => nil + person.changed? # => false person.name = 'bob' - person.changed? # => true - person.changed # => ['name'] - person.changes # => { 'name' => [nil, 'bob'] } + person.changed? # => true + person.changed # => ['name'] + person.changes # => { 'name' => [nil, 'bob'] } person.name = 'robert' person.save person.previous_changes # => {'name' => ['bob, 'robert']} @@ -129,32 +133,36 @@ behavior out of the box: {Learn more}[link:classes/ActiveModel/Naming.html] -* Observer support +* Making objects serializable - ActiveModel::Observers allows your object to implement the Observer - pattern in a Rails App and take advantage of all the standard observer - functions. + ActiveModel::Serialization provides a standard interface for your object + to provide +to_json+ or +to_xml+ serialization. - class PersonObserver < ActiveModel::Observer - def after_create(person) - person.logger.info("New person added!") - end + class SerialPerson + include ActiveModel::Serialization - def after_destroy(person) - person.logger.warn("Person with an id of #{person.id} was destroyed!") + attr_accessor :name + + def attributes + {'name' => name} end end - {Learn more}[link:classes/ActiveModel/Observer.html] - -* Making objects serializable + s = SerialPerson.new + s.serializable_hash # => {"name"=>nil} - ActiveModel::Serialization provides a standard interface for your object - to provide +to_json+ or +to_xml+ serialization. + class SerialPerson + include ActiveModel::Serializers::JSON + end s = SerialPerson.new - s.serializable_hash # => {"name"=>nil} s.to_json # => "{\"name\":null}" + + class SerialPerson + include ActiveModel::Serializers::Xml + end + + s = SerialPerson.new s.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person... {Learn more}[link:classes/ActiveModel/Serialization.html] |