2018-07-23 22:29:31 -04:00
|
|
|
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**
|
2014-12-23 17:32:50 -05:00
|
|
|
|
2012-09-01 17:25:58 -04:00
|
|
|
Active Model Basics
|
|
|
|
===================
|
2011-08-05 02:46:53 -04:00
|
|
|
|
2014-06-02 18:06:37 -04: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
|
2014-08-28 12:40:58 -04:00
|
|
|
plain Ruby objects. Active Model also helps build custom ORMs for use
|
2014-06-02 18:06:37 -04:00
|
|
|
outside of the Rails framework.
|
2011-08-05 02:46:53 -04:00
|
|
|
|
2015-11-03 19:18:56 -05:00
|
|
|
After reading this guide, you will know:
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2015-08-10 10:49:28 -04:00
|
|
|
* How an Active Record model behaves.
|
2015-11-03 19:18:56 -05:00
|
|
|
* How Callbacks and validations work.
|
2015-08-10 10:49:28 -04:00
|
|
|
* How serializers work.
|
2016-04-05 03:55:24 -04:00
|
|
|
* How Active Model integrates with the Rails internationalization (i18n) framework.
|
2012-11-29 17:25:02 -05:00
|
|
|
|
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
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
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.
|
2011-08-05 02:46:53 -04:00
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Attribute Methods
|
2011-08-05 03:43:22 -04:00
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
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.
|
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
|
2014-08-28 12:40:58 -04:00
|
|
|
person.age_highest? # => true
|
|
|
|
person.reset_age # => 0
|
|
|
|
person.age_highest? # => false
|
2012-09-01 17:08:06 -04:00
|
|
|
```
|
2011-08-05 02:46:53 -04:00
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Callbacks
|
2011-08-05 04:04:28 -04:00
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
`ActiveModel::Callbacks` gives Active Record style callbacks. This provides an
|
|
|
|
ability to define callbacks which run at appropriate times.
|
2018-05-08 22:48:07 -04:00
|
|
|
After defining callbacks, you can wrap them with before, after, and around
|
2014-06-02 18:06:37 -04:00
|
|
|
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
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Conversion
|
2011-08-05 05:58:01 -04:00
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
If a class defines `persisted?` and `id` methods, then you can include the
|
2017-03-12 11:25:24 -04:00
|
|
|
`ActiveModel::Conversion` module in that class, and call the Rails conversion
|
2014-06-02 18:06:37 -04:00
|
|
|
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
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Dirty
|
2011-08-05 05:51:12 -04:00
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
An object becomes dirty when it has gone through one or more changes to its
|
2014-08-28 12:40:58 -04:00
|
|
|
attributes and has not been saved. `ActiveModel::Dirty` 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
|
|
|
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
|
2017-03-12 13:11:53 -04:00
|
|
|
@last_name
|
2011-08-05 05:51:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_name=(value)
|
|
|
|
last_name_will_change!
|
|
|
|
@last_name = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2012-12-04 16:22:38 -05:00
|
|
|
# do save work...
|
2013-09-23 09:59:05 -04:00
|
|
|
changes_applied
|
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
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
# returns true if any of the attributes have unsaved changes.
|
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
|
|
|
|
2017-03-12 11:25:24 -04: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
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
# returns a Hash of changes, with the attribute names as the keys, and the
|
|
|
|
# values as an array of the old and new values 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
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
Track 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
|
2014-08-28 12:40:58 -04:00
|
|
|
person.first_name_was # => nil
|
2012-09-01 17:08:06 -04:00
|
|
|
```
|
2011-08-05 05:51:12 -04:00
|
|
|
|
2014-06-02 18:06:37 -04:00
|
|
|
Track both previous and current value of the changed attribute. Returns an array
|
2017-03-12 11:25:24 -04:00
|
|
|
if changed, otherwise 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
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Validations
|
2011-08-05 10:45:48 -04:00
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
The `ActiveModel::Validations` module adds the ability to validate objects
|
2014-08-28 12:40:58 -04:00
|
|
|
like in Active Record.
|
2011-08-05 10:45:48 -04:00
|
|
|
|
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
|
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
person = Person.new
|
|
|
|
person.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
|
|
|
```
|
2014-07-05 12:44:03 -04:00
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Naming
|
2014-07-05 12:44:03 -04:00
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
`ActiveModel::Naming` adds a number of class methods which make naming and routing
|
2014-07-15 15:12:23 -04:00
|
|
|
easier to manage. The module defines the `model_name` class method which
|
2014-07-05 12:44:03 -04:00
|
|
|
will define a number of accessors using some `ActiveSupport::Inflector` methods.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
end
|
|
|
|
|
|
|
|
Person.model_name.name # => "Person"
|
|
|
|
Person.model_name.singular # => "person"
|
|
|
|
Person.model_name.plural # => "people"
|
|
|
|
Person.model_name.element # => "person"
|
|
|
|
Person.model_name.human # => "Person"
|
|
|
|
Person.model_name.collection # => "people"
|
|
|
|
Person.model_name.param_key # => "person"
|
|
|
|
Person.model_name.i18n_key # => :person
|
|
|
|
Person.model_name.route_key # => "people"
|
|
|
|
Person.model_name.singular_route_key # => "person"
|
2014-08-28 12:40:58 -04:00
|
|
|
```
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Model
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2017-03-12 11:25:24 -04:00
|
|
|
`ActiveModel::Model` adds the ability for a class to work with Action Pack and
|
2014-08-28 12:40:58 -04:00
|
|
|
Action View right out of the box.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class EmailContact
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
attr_accessor :name, :email, :message
|
2014-06-02 18:06:37 -04:00
|
|
|
validates :name, :email, :message, presence: true
|
|
|
|
|
|
|
|
def deliver
|
|
|
|
if valid?
|
|
|
|
# deliver email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
When including `ActiveModel::Model` you get some features like:
|
|
|
|
|
2014-10-05 08:58:42 -04:00
|
|
|
- model name introspection
|
2014-06-02 18:06:37 -04:00
|
|
|
- conversions
|
|
|
|
- translations
|
|
|
|
- validations
|
|
|
|
|
|
|
|
It also gives you the ability to initialize an object with a hash of attributes,
|
|
|
|
much like any Active Record object.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
email_contact = EmailContact.new(name: 'David',
|
|
|
|
email: 'david@example.com',
|
|
|
|
message: 'Hello World')
|
|
|
|
email_contact.name # => 'David'
|
|
|
|
email_contact.email # => 'david@example.com'
|
|
|
|
email_contact.valid? # => true
|
|
|
|
email_contact.persisted? # => false
|
|
|
|
```
|
|
|
|
|
|
|
|
Any class that includes `ActiveModel::Model` can be used with `form_for`,
|
|
|
|
`render` and any other Action View helper methods, just like Active Record
|
|
|
|
objects.
|
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Serialization
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2015-10-17 21:47:56 -04:00
|
|
|
`ActiveModel::Serialization` provides basic serialization for your object.
|
2017-03-12 11:37:15 -04:00
|
|
|
You need to declare an attributes Hash which contains the attributes you want to
|
2014-06-02 18:06:37 -04:00
|
|
|
serialize. Attributes must be strings, not symbols.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
{'name' => nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
Now you can access a serialized Hash of your object using the `serializable_hash` method.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
person = Person.new
|
|
|
|
person.serializable_hash # => {"name"=>nil}
|
|
|
|
person.name = "Bob"
|
|
|
|
person.serializable_hash # => {"name"=>"Bob"}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### ActiveModel::Serializers
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
Active Model also provides the `ActiveModel::Serializers::JSON` module
|
|
|
|
for JSON serializing / deserializing. This module automatically includes the
|
|
|
|
previously discussed `ActiveModel::Serialization` module.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
##### ActiveModel::Serializers::JSON
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
To use `ActiveModel::Serializers::JSON` you only need to change the
|
|
|
|
module you are including from `ActiveModel::Serialization` to `ActiveModel::Serializers::JSON`.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
include ActiveModel::Serializers::JSON
|
|
|
|
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
{'name' => nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
The `as_json` method, similar to `serializable_hash`, provides a Hash representing
|
|
|
|
the model.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
person = Person.new
|
|
|
|
person.as_json # => {"name"=>nil}
|
|
|
|
person.name = "Bob"
|
|
|
|
person.as_json # => {"name"=>"Bob"}
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
You can also define the attributes for a model from a JSON string.
|
|
|
|
However, you need to define the `attributes=` method on your class:
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
include ActiveModel::Serializers::JSON
|
|
|
|
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def attributes=(hash)
|
|
|
|
hash.each do |key, value|
|
|
|
|
send("#{key}=", value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
{'name' => nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
Now it is possible to create an instance of `Person` and set attributes using `from_json`.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
json = { name: 'Bob' }.to_json
|
|
|
|
person = Person.new
|
|
|
|
person.from_json(json) # => #<Person:0x00000100c773f0 @name="Bob">
|
|
|
|
person.name # => "Bob"
|
|
|
|
```
|
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Translation
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
`ActiveModel::Translation` provides integration between your object and the Rails
|
|
|
|
internationalization (i18n) framework.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
extend ActiveModel::Translation
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
With the `human_attribute_name` method, you can transform attribute names into a
|
|
|
|
more human-readable format. The human-readable format is defined in your locale file(s).
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
* config/locales/app.pt-BR.yml
|
|
|
|
|
|
|
|
```yml
|
|
|
|
pt-BR:
|
|
|
|
activemodel:
|
|
|
|
attributes:
|
|
|
|
person:
|
|
|
|
name: 'Nome'
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Person.human_attribute_name('name') # => "Nome"
|
|
|
|
```
|
|
|
|
|
2014-08-28 12:40:58 -04:00
|
|
|
### Lint Tests
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2015-10-17 21:47:56 -04:00
|
|
|
`ActiveModel::Lint::Tests` allows you to test whether an object is compliant with
|
2014-08-28 12:40:58 -04:00
|
|
|
the Active Model API.
|
2014-06-02 18:06:37 -04:00
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
* `app/models/person.rb`
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
2014-08-28 12:40:58 -04:00
|
|
|
class Person
|
2014-06-02 18:06:37 -04:00
|
|
|
include ActiveModel::Model
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-12 11:37:15 -04:00
|
|
|
* `test/models/person_test.rb`
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class PersonTest < ActiveSupport::TestCase
|
|
|
|
include ActiveModel::Lint::Tests
|
|
|
|
|
2015-11-03 19:18:56 -05:00
|
|
|
setup do
|
2014-06-02 18:06:37 -04:00
|
|
|
@model = Person.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
2015-12-18 07:01:05 -05:00
|
|
|
$ rails test
|
2014-06-02 18:06:37 -04:00
|
|
|
|
|
|
|
Run options: --seed 14596
|
|
|
|
|
|
|
|
# Running:
|
|
|
|
|
|
|
|
......
|
|
|
|
|
|
|
|
Finished in 0.024899s, 240.9735 runs/s, 1204.8677 assertions/s.
|
|
|
|
|
|
|
|
6 runs, 30 assertions, 0 failures, 0 errors, 0 skips
|
|
|
|
```
|
|
|
|
|
|
|
|
An object is not required to implement all APIs in order to work with
|
|
|
|
Action Pack. This module only intends to provide guidance in case you want all
|
|
|
|
features out of the box.
|
2014-10-04 20:20:59 -04:00
|
|
|
|
2014-08-11 08:21:20 -04:00
|
|
|
### SecurePassword
|
|
|
|
|
|
|
|
`ActiveModel::SecurePassword` provides a way to securely store any
|
2017-03-12 11:37:15 -04:00
|
|
|
password in an encrypted form. When you include this module, a
|
2014-10-05 08:58:42 -04:00
|
|
|
`has_secure_password` class method is provided which defines
|
2018-07-06 13:05:29 -04:00
|
|
|
a `password` accessor with certain validations on it by default.
|
2014-08-11 08:21:20 -04:00
|
|
|
|
|
|
|
#### Requirements
|
|
|
|
|
2015-10-17 21:47:56 -04:00
|
|
|
`ActiveModel::SecurePassword` depends on [`bcrypt`](https://github.com/codahale/bcrypt-ruby 'BCrypt'),
|
2017-10-06 02:02:56 -04:00
|
|
|
so include this gem in your `Gemfile` to use `ActiveModel::SecurePassword` correctly.
|
2018-07-06 13:05:29 -04:00
|
|
|
In order to make this work, the model must have an accessor named `XXX_digest`.
|
2018-07-08 06:56:07 -04:00
|
|
|
Where `XXX` is the attribute name of your desired password.
|
2018-07-06 13:05:29 -04:00
|
|
|
The following validations are added automatically:
|
2014-08-11 08:21:20 -04:00
|
|
|
|
|
|
|
1. Password should be present.
|
2018-07-06 13:05:29 -04:00
|
|
|
2. Password should be equal to its confirmation (provided `XXX_confirmation` is passed along).
|
2015-10-17 21:47:56 -04:00
|
|
|
3. The maximum length of a password is 72 (required by `bcrypt` on which ActiveModel::SecurePassword depends)
|
2014-08-11 08:21:20 -04:00
|
|
|
|
|
|
|
#### Examples
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Person
|
|
|
|
include ActiveModel::SecurePassword
|
|
|
|
has_secure_password
|
2018-07-08 06:56:07 -04:00
|
|
|
has_secure_password :recovery_password, validations: false
|
2018-07-06 13:05:29 -04:00
|
|
|
|
2018-07-08 06:56:07 -04:00
|
|
|
attr_accessor :password_digest, :recovery_password_digest
|
2014-08-11 08:21:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
person = Person.new
|
|
|
|
|
|
|
|
# When password is blank.
|
|
|
|
person.valid? # => false
|
|
|
|
|
|
|
|
# When the confirmation doesn't match the password.
|
|
|
|
person.password = 'aditya'
|
|
|
|
person.password_confirmation = 'nomatch'
|
|
|
|
person.valid? # => false
|
|
|
|
|
2015-10-17 21:47:56 -04:00
|
|
|
# When the length of password exceeds 72.
|
2014-08-11 08:21:20 -04:00
|
|
|
person.password = person.password_confirmation = 'a' * 100
|
|
|
|
person.valid? # => false
|
|
|
|
|
2016-08-10 16:25:45 -04:00
|
|
|
# When only password is supplied with no password_confirmation.
|
|
|
|
person.password = 'aditya'
|
|
|
|
person.valid? # => true
|
|
|
|
|
2014-08-11 08:21:20 -04:00
|
|
|
# When all validations are passed.
|
|
|
|
person.password = person.password_confirmation = 'aditya'
|
|
|
|
person.valid? # => true
|
2018-07-06 13:05:29 -04:00
|
|
|
|
2018-07-08 06:56:07 -04:00
|
|
|
person.recovery_password = "42password"
|
2018-07-06 13:05:29 -04:00
|
|
|
|
|
|
|
person.authenticate('aditya') # => person
|
|
|
|
person.authenticate('notright') # => false
|
|
|
|
person.authenticate_password('aditya') # => person
|
|
|
|
person.authenticate_password('notright') # => false
|
|
|
|
|
2018-07-08 06:56:07 -04:00
|
|
|
person.authenticate_recovery_password('42password') # => person
|
|
|
|
person.authenticate_recovery_password('notright') # => false
|
2018-07-06 13:05:29 -04:00
|
|
|
|
2018-07-08 06:56:07 -04:00
|
|
|
person.password_digest # => "$2a$04$gF8RfZdoXHvyTjHhiU4ZsO.kQqV9oonYZu31PRE4hLQn3xM2qkpIy"
|
|
|
|
person.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
|
2014-10-04 20:20:59 -04:00
|
|
|
```
|