Skip to content
This repository
Browse code

Dirty object methods added to active model basics

  • Loading branch information...
commit 86ae14df4733cac1748513994caec2f9775ae224 1 parent a3cf682
Sukeerthi Adiga authored August 05, 2011 fxn committed August 13, 2011
88  railties/guides/source/active_model_basics.textile
Source Rendered
@@ -90,6 +90,92 @@ person.to_key              #=> nil
90 90
 person.to_param            #=> nil
91 91
 </ruby>
92 92
 
  93
+h4. Dirty
  94
+
  95
+An object becomes dirty when an object is gone through one or more changes to its attributes and not yet saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Lets consider a Person class with attributes first_name and last_name
  96
+
  97
+<ruby>
  98
+require 'rubygems'
  99
+require 'active_model'
  100
+
  101
+class Person
  102
+  include ActiveModel::Dirty
  103
+  define_attribute_methods [:first_name, :last_name]
  104
+
  105
+  def first_name
  106
+    @first_name
  107
+  end
  108
+
  109
+  def first_name=(value)
  110
+    first_name_will_change!
  111
+    @first_name = value
  112
+  end
  113
+
  114
+  def last_name
  115
+    @last_name
  116
+  end
  117
+
  118
+  def last_name=(value)
  119
+    last_name_will_change!
  120
+    @last_name = value
  121
+  end
  122
+
  123
+  def save
  124
+    @previously_changed = changes
  125
+  end
  126
+
  127
+end
  128
+</ruby>
  129
+
  130
+h5. Querying object directly for its list of all changed attributes.
  131
+
  132
+<ruby>
  133
+person = Person.new
  134
+person.first_name = "First Name"
  135
+
  136
+person.first_name #=> "First Name"
  137
+person.first_name = "First Name Changed"
  138
+
  139
+person.changed? #=> true
  140
+
  141
+#returns an list of fields arry which all has been changed before saved.
  142
+person.changed #=> ["first_name"]
  143
+
  144
+#returns a hash of the fields that have changed with their original values.
  145
+person.changed_attributes #=> {"first_name" => "First Name Changed"}
  146
+
  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" => ["First Name","First Name Changed"]}
  149
+</ruby>
  150
+
  151
+h5. Attribute based accessor methods
  152
+
  153
+Track whether the particular attribute has been changed or not.
  154
+
  155
+<ruby>
  156
+#attr_name_changed?
  157
+person.first_name #=> "First Name"
  158
+
  159
+#assign some other value to first_name attribute
  160
+person.first_name = "First Name 1"
  161
+
  162
+person.first_name_changed? #=> true
  163
+</ruby>
  164
+
  165
+Track what was the previous value of the attribute.
  166
+<ruby>
  167
+#attr_name_was accessor
  168
+person.first_name_was  #=> "First Name"
  169
+</ruby>
  170
+
  171
+
  172
+Track  both previous and current value of the changed attribute. Returns an array if changed else returns nil
  173
+<ruby>
  174
+#attr_name_change
  175
+person.first_name_change #=> ["First Name", "First Name 1"]
  176
+person.last_name_change #=> nil
  177
+</ruby>
  178
+
93 179
 h3. Changelog
94 180
 
95  
-* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw
  181
+* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw

0 notes on commit 86ae14d

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