diff options
author | Sukeerthi Adiga G <sukeerthiadiga@gmail.com> | 2011-08-05 15:21:12 +0530 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2011-08-13 16:22:32 -0700 |
commit | 86ae14df4733cac1748513994caec2f9775ae224 (patch) | |
tree | bf2a3129c923c576b47fe7968ff1ee70fdba4e71 /railties/guides/source | |
parent | a3cf68291df3e9f5b23f56a90929c601ffc26ebd (diff) | |
download | rails-86ae14df4733cac1748513994caec2f9775ae224.tar.gz rails-86ae14df4733cac1748513994caec2f9775ae224.tar.bz2 rails-86ae14df4733cac1748513994caec2f9775ae224.zip |
Dirty object methods added to active model basics
Diffstat (limited to 'railties/guides/source')
-rw-r--r-- | railties/guides/source/active_model_basics.textile | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index daf41d2296..404cc71c50 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -90,6 +90,92 @@ person.to_key #=> nil person.to_param #=> nil </ruby> +h4. Dirty + +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 + +<ruby> +require 'rubygems' +require 'active_model' + +class Person + include ActiveModel::Dirty + define_attribute_methods [:first_name, :last_name] + + def first_name + @first_name + end + + def first_name=(value) + first_name_will_change! + @first_name = value + end + + def last_name + @last_name + end + + def last_name=(value) + last_name_will_change! + @last_name = value + end + + def save + @previously_changed = changes + end + +end +</ruby> + +h5. Querying object directly for its list of all changed attributes. + +<ruby> +person = Person.new +person.first_name = "First Name" + +person.first_name #=> "First Name" +person.first_name = "First Name Changed" + +person.changed? #=> true + +#returns an list of fields arry which all has been changed before saved. +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 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"]} +</ruby> + +h5. Attribute based accessor methods + +Track whether the particular attribute has been changed or not. + +<ruby> +#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 +</ruby> + +Track what was the previous value of the attribute. +<ruby> +#attr_name_was accessor +person.first_name_was #=> "First Name" +</ruby> + + +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"] +person.last_name_change #=> nil +</ruby> + h3. Changelog -* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw
\ No newline at end of file +* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw |