2012-09-01 17:25:58 -04:00
Active Model Basics
===================
2011-08-05 02:46:53 -04:00
2012-11-29 07:35:13 -05:00
This guide should provide you with all you need to get started using model classes. Active Model allows for Action Pack helpers to interact with non-Active Record models. Active Model also helps building custom ORMs for use outside of the Rails framework.
2011-08-05 02:46:53 -04:00
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2011-08-05 02:46:53 -04:00
2012-09-01 17:25:58 -04:00
Introduction
------------
2011-08-05 02:46:53 -04:00
2012-12-04 16:22:38 -05:00
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.
2011-08-05 02:46:53 -04:00
2012-09-01 17:25:58 -04:00
### AttributeMethods
2011-08-05 03:43:22 -04:00
2013-05-04 06:23:18 -04:00
The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes and which methods on the object will use them.
2011-08-05 03:43:22 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 03:43:22 -04:00
class Person
include ActiveModel::AttributeMethods
attribute_method_prefix 'reset_'
attribute_method_suffix '_highest?'
2012-05-14 12:38:23 -04:00
define_attribute_methods 'age'
2011-08-05 03:43:22 -04:00
attr_accessor :age
2012-12-04 16:22:38 -05:00
private
def reset_attribute(attribute)
send("#{attribute}=", 0)
end
2011-08-05 03:43:22 -04:00
2012-12-04 16:22:38 -05:00
def attribute_highest?(attribute)
send(attribute) > 100
end
2011-08-05 03:43:22 -04:00
end
2011-08-05 04:04:28 -04:00
person = Person.new
person.age = 110
person.age_highest? # true
person.reset_age # 0
2012-12-04 16:22:38 -05:00
person.age_highest? # false
2012-09-01 17:08:06 -04:00
```
2011-08-05 02:46:53 -04:00
2012-09-01 17:25:58 -04:00
### Callbacks
2011-08-05 04:04:28 -04:00
2013-05-04 05:44:58 -04:00
Callbacks gives Active Record style callbacks. This provides an ability to define callbacks which run at appropriate times. After defining callbacks, you can wrap them with before, after and around custom methods.
2011-08-05 04:04:28 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 05:05:04 -04:00
class Person
extend ActiveModel::Callbacks
define_model_callbacks :update
before_update :reset_me
def update
2012-01-12 03:44:10 -05:00
run_callbacks(:update) do
2013-05-04 05:44:58 -04:00
# This method is called when update is called on an object.
2011-08-05 05:05:04 -04:00
end
end
def reset_me
2013-05-04 05:44:58 -04:00
# This method is called when update is called on an object as a before_update callback is defined.
2011-08-05 05:05:04 -04:00
end
end
2012-09-01 17:08:06 -04:00
```
2011-08-05 04:04:28 -04:00
2012-09-01 17:25:58 -04:00
### Conversion
2011-08-05 05:58:01 -04:00
2013-05-04 06:23:18 -04:00
If a class defines `persisted?` and `id` methods, then you can include the `Conversion` module in that class and call the Rails conversion methods on objects of that class.
2011-08-05 05:58:01 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 05:58:01 -04:00
class Person
include ActiveModel::Conversion
def persisted?
false
end
def id
nil
end
end
person = Person.new
2012-12-04 16:50:27 -05:00
person.to_model == person # => true
person.to_key # => nil
person.to_param # => nil
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:58:01 -04:00
2012-09-01 17:25:58 -04:00
### Dirty
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
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` :
2011-08-05 05:51:12 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 05:51:12 -04:00
require 'active_model'
class Person
include ActiveModel::Dirty
2012-05-14 12:38:23 -04:00
define_attribute_methods :first_name, :last_name
2011-08-05 05:51:12 -04:00
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
2012-12-04 16:22:38 -05:00
# do save work...
2011-08-05 05:51:12 -04:00
end
end
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:51:12 -04:00
2012-09-01 17:25:58 -04:00
#### Querying object directly for its list of all changed attributes.
2011-08-05 05:51:12 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 05:51:12 -04:00
person = Person.new
2012-12-04 16:50:27 -05:00
person.changed? # => false
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
person.first_name = "First Name"
2012-12-04 16:50:27 -05:00
person.first_name # => "First Name"
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
# returns if any attribute has changed.
2012-12-04 16:50:27 -05:00
person.changed? # => true
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
# returns a list of attributes that have changed before saving.
2012-12-04 16:50:27 -05:00
person.changed # => ["first_name"]
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
# returns a hash of the attributes that have changed with their original values.
2012-12-04 16:50:27 -05:00
person.changed_attributes # => {"first_name"=>nil}
2011-08-05 05:51:12 -04:00
2012-12-04 16:22:38 -05:00
# 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.
2012-12-04 16:50:27 -05:00
person.changes # => {"first_name"=>[nil, "First Name"]}
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:51:12 -04:00
2012-09-01 17:25:58 -04:00
#### Attribute based accessor methods
2011-08-05 05:51:12 -04:00
Track whether the particular attribute has been changed or not.
2012-09-01 17:08:06 -04:00
```ruby
2012-12-04 16:22:38 -05:00
# attr_name_changed?
2012-12-04 16:50:27 -05:00
person.first_name # => "First Name"
person.first_name_changed? # => true
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:51:12 -04:00
Track what was the previous value of the attribute.
2011-09-02 16:10:00 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-12-04 16:22:38 -05:00
# attr_name_was accessor
2012-12-04 16:50:27 -05:00
person.first_name_was # => "First Name"
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:51:12 -04:00
2012-08-13 23:57:53 -04:00
Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
2011-09-02 16:10:00 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-12-04 16:22:38 -05:00
# attr_name_change
2012-12-04 16:50:27 -05:00
person.first_name_change # => [nil, "First Name"]
person.last_name_change # => nil
2012-09-01 17:08:06 -04:00
```
2011-08-05 05:51:12 -04:00
2012-09-01 17:25:58 -04:00
### Validations
2011-08-05 10:45:48 -04:00
Validations module adds the ability to class objects to validate them in Active Record style.
2012-09-01 17:08:06 -04:00
```ruby
2011-08-05 10:45:48 -04:00
class Person
include ActiveModel::Validations
2011-08-24 03:25:29 -04:00
attr_accessor :name, :email, :token
2012-12-04 16:22:38 -05:00
2012-11-15 16:09:28 -05:00
validates :name, presence: true
2012-12-04 16:22:38 -05:00
validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
2012-11-15 16:09:28 -05:00
validates! :token, presence: true
2011-08-05 10:45:48 -04:00
end
2012-11-15 16:09:28 -05:00
person = Person.new(token: "2b1f325")
2012-12-04 16:50:27 -05:00
person.valid? # => false
2012-12-04 16:22:38 -05:00
person.name = 'vishnu'
person.email = 'me'
2012-12-04 16:50:27 -05:00
person.valid? # => false
2011-08-05 10:45:48 -04:00
person.email = 'me@vishnuatrai.com'
2012-12-04 16:50:27 -05:00
person.valid? # => true
2011-08-24 03:25:29 -04:00
person.token = nil
2012-12-04 16:50:27 -05:00
person.valid? # => raises ActiveModel::StrictValidationFailed
2012-09-01 17:08:06 -04:00
```