Permalink
Browse files

Improve the Active Model guide [ci skip]

  • Loading branch information...
1 parent 6300d83 commit 7f395600ce5fd0857538d0a6ea664e9a077da362 @carlosantoniodasilva carlosantoniodasilva committed Dec 4, 2012
Showing with 27 additions and 33 deletions.
  1. +27 −33 guides/source/active_model_basics.md
@@ -10,7 +10,7 @@ After reading this guide, you will know:
Introduction
------------
-Active Model is a library containing various modules used in developing frameworks that need to interact with the Rails Action Pack library. Active Model provides a known set of interfaces for usage in classes. Some of modules are explained below.
+Active Model is a library containing various modules used in developing frameworks that need to interact with the Rails Action Pack library. Active Model provides a known set of interfaces for usage in classes. Some of modules are explained below.
### AttributeMethods
@@ -26,23 +26,21 @@ class Person
attr_accessor :age
-private
- def reset_attribute(attribute)
- send("#{attribute}=", 0)
- end
+ private
+ def reset_attribute(attribute)
+ send("#{attribute}=", 0)
+ end
- def attribute_highest?(attribute)
- send(attribute) > 100 ? true : false
- end
-
+ def attribute_highest?(attribute)
+ send(attribute) > 100
+ end
end
person = Person.new
person.age = 110
person.age_highest? # true
person.reset_age # 0
-person.age_highest? # false
-
+person.age_highest? # false
```
### Callbacks
@@ -94,7 +92,7 @@ person.to_param #=> nil
### Dirty
-An object becomes dirty when it has gone through one or more changes to its attributes and has not been saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Let's consider a Person class with attributes first_name and last_name
+An object becomes dirty when it has gone through one or more changes to its attributes and has not been saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Let's consider a Person class with attributes `first_name` and `last_name`:
```ruby
require 'active_model'
@@ -123,58 +121,55 @@ class Person
def save
@previously_changed = changes
+ # do save work...
end
-
end
```
#### Querying object directly for its list of all changed attributes.
```ruby
person = Person.new
-person.first_name = "First Name"
+person.changed? #=> false
+person.first_name = "First Name"
person.first_name #=> "First Name"
-person.first_name = "First Name Changed"
+# returns if any attribute has changed.
person.changed? #=> true
-#returns a list of fields array which all has been changed before saved.
+# returns a list of attributes that have changed before saving.
person.changed #=> ["first_name"]
-#returns a hash of the fields that have changed with their original values.
-person.changed_attributes #=> {"first_name" => "First Name Changed"}
+# returns a hash of the attributes that have changed with their original values.
+person.changed_attributes #=> {"first_name"=>nil}
-#returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
-person.changes #=> {"first_name" => ["First Name","First Name Changed"]}
+# returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
+person.changes #=> {"first_name"=>[nil, "First Name"]}
```
#### Attribute based accessor methods
Track whether the particular attribute has been changed or not.
```ruby
-#attr_name_changed?
+# attr_name_changed?
person.first_name #=> "First Name"
-
-#assign some other value to first_name attribute
-person.first_name = "First Name 1"
-
person.first_name_changed? #=> true
```
Track what was the previous value of the attribute.
```ruby
-#attr_name_was accessor
+# attr_name_was accessor
person.first_name_was #=> "First Name"
```
Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
```ruby
-#attr_name_change
-person.first_name_change #=> ["First Name", "First Name 1"]
+# attr_name_change
+person.first_name_change #=> [nil, "First Name"]
person.last_name_change #=> nil
```
@@ -187,17 +182,16 @@ class Person
include ActiveModel::Validations
attr_accessor :name, :email, :token
-
+
validates :name, presence: true
- validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
+ validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
validates! :token, presence: true
-
end
person = Person.new(token: "2b1f325")
person.valid? #=> false
-person.name = 'vishnu'
-person.email = 'me'
+person.name = 'vishnu'
+person.email = 'me'
person.valid? #=> false
person.email = 'me@vishnuatrai.com'
person.valid? #=> true

0 comments on commit 7f39560

Please sign in to comment.