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.
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
plain Ruby object. Active Model also helps building custom ORMs for use
outside of the Rails framework.
After reading this guide, you will know:
After reading this guide, you will be able to add to plain Ruby objects:
* The ability to behaves like an Active Record model.
* Add callbacks and validations like Active Record.
* Add serializers.
* Integrate with the Rails internationalization (i18n) framework.
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.
Active Model is a library containing various modules used in developing
classes that need some features present on Active Record.
Some of these modules are explained below.
### AttributeMethods
### ActiveModel::AttributeMethods
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.
The `ActiveModel::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.
```ruby
classPerson
...
...
@@ -43,9 +55,12 @@ person.reset_age # 0
person.age_highest?# false
```
### Callbacks
### ActiveModel::Callbacks
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.
`ActiveModel::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.
```ruby
classPerson
...
...
@@ -67,9 +82,11 @@ class Person
end
```
### Conversion
### ActiveModel::Conversion
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.
If a class defines `persisted?` and `id` methods, then you can include the
`ActiveModel::Conversion` module in that class and call the Rails conversion
methods on objects of that class.
```ruby
classPerson
...
...
@@ -90,9 +107,12 @@ person.to_key # => nil
person.to_param# => nil
```
### Dirty
### ActiveModel::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`:
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'
...
...
@@ -165,7 +185,8 @@ Track what was the previous value of the attribute.
person.first_name_was# => "First Name"
```
Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
Track both previous and current value of the changed attribute. Returns an array