mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Adding RDoc for active_model dirty
This commit is contained in:
parent
db274a02ed
commit
4a2d2ef91a
2 changed files with 44 additions and 27 deletions
|
@ -9,9 +9,8 @@ duplication and fragile applications that broke on upgrades.
|
|||
Active Model is a solution for this problem.
|
||||
|
||||
Active Model provides a known set of interfaces that your objects can implement
|
||||
to then present a common interface to the Action Pack helpers.
|
||||
|
||||
You can include functionality from the following modules:
|
||||
to then present a common interface to the Action Pack helpers. You can include
|
||||
functionality from the following modules:
|
||||
|
||||
* Adding callbacks to your class
|
||||
|
||||
|
@ -41,28 +40,7 @@ You can include functionality from the following modules:
|
|||
|
||||
* Tracking changes in your object
|
||||
|
||||
class Person
|
||||
include ActiveModel::Dirty
|
||||
|
||||
define_attribute_methods [:name]
|
||||
|
||||
def name
|
||||
@name
|
||||
end
|
||||
|
||||
def name=(val)
|
||||
name_will_change!
|
||||
@name = val
|
||||
end
|
||||
|
||||
def save
|
||||
@previously_changed = changes
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
...provides all the value tracking features implemented by ActiveRecord
|
||||
Provides all the value tracking features implemented by ActiveRecord...
|
||||
|
||||
person = Person.new
|
||||
person.name # => nil
|
||||
|
@ -76,3 +54,4 @@ You can include functionality from the following modules:
|
|||
person.previous_changes # => {'name' => ['bob, 'robert']}
|
||||
|
||||
{Learn more}[link:classes/ActiveModel/Dirty.html]
|
||||
|
||||
|
|
|
@ -1,5 +1,43 @@
|
|||
module ActiveModel
|
||||
# Track unsaved attribute changes.
|
||||
# <tt>ActiveModel::Dirty</tt> provides a way to track changes in your
|
||||
# object in the same way as ActiveRecord does.
|
||||
#
|
||||
# The requirements to implement ActiveModel::Dirty are:
|
||||
#
|
||||
# * <tt>include ActiveModel::Dirty</tt> in your object
|
||||
# * Call <tt>define_attribute_methods</tt> passing each method you want to track
|
||||
# * Call <tt>attr_name_will_change!</tt> before each change to the tracked attribute
|
||||
#
|
||||
# If you wish to also track previous changes on save or update, you need to add
|
||||
#
|
||||
# @previously_changed = changes
|
||||
#
|
||||
# inside of your save or update method.
|
||||
#
|
||||
# A minimal implementation could be:
|
||||
#
|
||||
# class Person
|
||||
#
|
||||
# include ActiveModel::Dirty
|
||||
#
|
||||
# define_attribute_methods [:name]
|
||||
#
|
||||
# def name
|
||||
# @name
|
||||
# end
|
||||
#
|
||||
# def name=(val)
|
||||
# name_will_change!
|
||||
# @name = val
|
||||
# end
|
||||
#
|
||||
# def save
|
||||
# @previously_changed = changes
|
||||
# end
|
||||
#
|
||||
# end
|
||||
#
|
||||
# == Examples:
|
||||
#
|
||||
# A newly instantiated object is unchanged:
|
||||
# person = Person.find_by_name('Uncle Bob')
|
||||
|
|
Loading…
Reference in a new issue