Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improve the Active Model guide [ci skip]

  • Loading branch information...
commit 7f395600ce5fd0857538d0a6ea664e9a077da362 1 parent 6300d83
Carlos Antonio da Silva authored December 04, 2012

Showing 1 changed file with 27 additions and 33 deletions. Show diff stats Hide diff stats

  1. 60  guides/source/active_model_basics.md
60  guides/source/active_model_basics.md
Source Rendered
@@ -10,7 +10,7 @@ After reading this guide, you will know:
10 10
 Introduction
11 11
 ------------
12 12
 
13  
-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.  
  13
+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.
14 14
 
15 15
 ### AttributeMethods
16 16
 
@@ -26,23 +26,21 @@ class Person
26 26
 
27 27
   attr_accessor :age
28 28
 
29  
-private
30  
-  def reset_attribute(attribute)
31  
-    send("#{attribute}=", 0)
32  
-  end
  29
+  private
  30
+    def reset_attribute(attribute)
  31
+      send("#{attribute}=", 0)
  32
+    end
33 33
 
34  
-  def attribute_highest?(attribute)
35  
-    send(attribute) > 100 ? true : false
36  
-  end
37  
-  
  34
+    def attribute_highest?(attribute)
  35
+      send(attribute) > 100
  36
+    end
38 37
 end
39 38
 
40 39
 person = Person.new
41 40
 person.age = 110
42 41
 person.age_highest?  # true
43 42
 person.reset_age     # 0
44  
-person.age_highest?  # false 
45  
-
  43
+person.age_highest?  # false
46 44
 ```
47 45
 
48 46
 ### Callbacks
@@ -94,7 +92,7 @@ person.to_param            #=> nil
94 92
 
95 93
 ### Dirty
96 94
 
97  
-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
  95
+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`:
98 96
 
99 97
 ```ruby
100 98
 require 'active_model'
@@ -123,8 +121,8 @@ class Person
123 121
 
124 122
   def save
125 123
     @previously_changed = changes
  124
+    # do save work...
126 125
   end
127  
-
128 126
 end
129 127
 ```
130 128
 
@@ -132,21 +130,22 @@ end
132 130
 
133 131
 ```ruby
134 132
 person = Person.new
135  
-person.first_name = "First Name"
  133
+person.changed? #=> false
136 134
 
  135
+person.first_name = "First Name"
137 136
 person.first_name #=> "First Name"
138  
-person.first_name = "First Name Changed"
139 137
 
  138
+# returns if any attribute has changed.
140 139
 person.changed? #=> true
141 140
 
142  
-#returns a list of fields array which all has been changed before saved.
  141
+# returns a list of attributes that have changed before saving.
143 142
 person.changed #=> ["first_name"]
144 143
 
145  
-#returns a hash of the fields that have changed with their original values.
146  
-person.changed_attributes #=> {"first_name" => "First Name Changed"}
  144
+# returns a hash of the attributes that have changed with their original values.
  145
+person.changed_attributes #=> {"first_name"=>nil}
147 146
 
148  
-#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.
149  
-person.changes #=> {"first_name" => ["First Name","First Name Changed"]}
  147
+# 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.
  148
+person.changes #=> {"first_name"=>[nil, "First Name"]}
150 149
 ```
151 150
 
152 151
 #### Attribute based accessor methods
@@ -154,27 +153,23 @@ person.changes #=> {"first_name" => ["First Name","First Name Changed"]}
154 153
 Track whether the particular attribute has been changed or not.
155 154
 
156 155
 ```ruby
157  
-#attr_name_changed?
  156
+# attr_name_changed?
158 157
 person.first_name #=> "First Name"
159  
-
160  
-#assign some other value to first_name attribute
161  
-person.first_name = "First Name 1"
162  
-
163 158
 person.first_name_changed? #=> true
164 159
 ```
165 160
 
166 161
 Track what was the previous value of the attribute.
167 162
 
168 163
 ```ruby
169  
-#attr_name_was accessor
  164
+# attr_name_was accessor
170 165
 person.first_name_was  #=> "First Name"
171 166
 ```
172 167
 
173 168
 Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
174 169
 
175 170
 ```ruby
176  
-#attr_name_change
177  
-person.first_name_change #=> ["First Name", "First Name 1"]
  171
+# attr_name_change
  172
+person.first_name_change #=> [nil, "First Name"]
178 173
 person.last_name_change #=> nil
179 174
 ```
180 175
 
@@ -187,17 +182,16 @@ class Person
187 182
   include ActiveModel::Validations
188 183
 
189 184
   attr_accessor :name, :email, :token
190  
-  
  185
+
191 186
   validates :name, presence: true
192  
-  validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i  
  187
+  validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
193 188
   validates! :token, presence: true
194  
-  
195 189
 end
196 190
 
197 191
 person = Person.new(token: "2b1f325")
198 192
 person.valid?                        #=> false
199  
-person.name  = 'vishnu'
200  
-person.email  = 'me'
  193
+person.name = 'vishnu'
  194
+person.email = 'me'
201 195
 person.valid?                        #=> false
202 196
 person.email = 'me@vishnuatrai.com'
203 197
 person.valid?                        #=> true

0 notes on commit 7f39560

Please sign in to comment.
Something went wrong with that request. Please try again.