From 7bc1ca351523949f6b4ce96018e95e61cbc7719e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 1 Sep 2012 17:08:06 -0400 Subject: Convert code blocks into GFM style --- guides/source/active_model_basics.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'guides/source/active_model_basics.md') diff --git a/guides/source/active_model_basics.md b/guides/source/active_model_basics.md index 2c30ddb84c..c02afd1c9a 100644 --- a/guides/source/active_model_basics.md +++ b/guides/source/active_model_basics.md @@ -14,7 +14,7 @@ h4. AttributeMethods The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes, which methods on the object will use them. - +```ruby class Person include ActiveModel::AttributeMethods @@ -41,13 +41,13 @@ person.age_highest? # true person.reset_age # 0 person.age_highest? # false - +``` h4. Callbacks Callbacks gives Active Record style callbacks. This provides the ability to define the callbacks and those will run at appropriate time. After defining a callbacks you can wrap with before, after and around custom methods. - +```ruby class Person extend ActiveModel::Callbacks @@ -65,13 +65,13 @@ class Person # This method will call when you are calling update on object as a before_update callback as defined. end end - +``` h4. Conversion If a class defines persisted? and id methods then you can include Conversion module in that class and you can able to call Rails conversion methods to objects of that class. - +```ruby class Person include ActiveModel::Conversion @@ -88,13 +88,13 @@ person = Person.new person.to_model == person #=> true person.to_key #=> nil person.to_param #=> nil - +``` h4. 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 - +```ruby require 'active_model' class Person @@ -124,11 +124,11 @@ class Person end end - +``` h5. Querying object directly for its list of all changed attributes. - +```ruby person = Person.new person.first_name = "First Name" @@ -145,13 +145,13 @@ 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"]} - +``` h5. Attribute based accessor methods Track whether the particular attribute has been changed or not. - +```ruby #attr_name_changed? person.first_name #=> "First Name" @@ -159,28 +159,28 @@ person.first_name #=> "First Name" 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 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"] person.last_name_change #=> nil - +``` h4. Validations Validations module adds the ability to class objects to validate them in Active Record style. - +```ruby class Person include ActiveModel::Validations @@ -201,4 +201,4 @@ person.email = 'me@vishnuatrai.com' person.valid? #=> true person.token = nil person.valid? #=> raises ActiveModel::StrictValidationFailed - +``` -- cgit v1.2.3