Skip to content

Commit

Permalink
Merge pull request #48616 from p8/activerecord/document-dirty
Browse files Browse the repository at this point in the history
Document `ActiveRecord::Dirty` module in the API docs
  • Loading branch information
guilleiguaran committed Jun 30, 2023
2 parents e8e8517 + 4b3920a commit b790387
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions activemodel/lib/active_model/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ module ActiveModel
# person.name_change # => ["Bill", "Bill"]
# person.name << 'y'
# person.name_change # => ["Bill", "Billy"]
#
# Methods can be invoked as +name_changed?+ or by passing an argument to the
# generic method <tt>attribute_changed?("name")</tt>.
module Dirty
extend ActiveSupport::Concern
include ActiveModel::AttributeMethods
Expand Down
31 changes: 31 additions & 0 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@
module ActiveRecord
module AttributeMethods
# = Active Record Attribute Methods \Dirty
#
# Provides a way to track changes in your Active Record models. It adds all
# methods from ActiveModel::Dirty and adds database specific methods.
#
# A newly created +Person+ object is unchanged:
#
# class Person < ActiveRecord::Base
# end
#
# person = Person.create(name: "Alisson")
# person.changed? # => false
#
# Change the name:
#
# person.name = 'Alice'
# person.name_in_database # => "Allison"
# person.will_save_change_to_name? # => true
# person.name_change_to_be_saved # => ["Allison", "Alice"]
# person.changes_to_save # => {"name"=>["Allison", "Alice"]}
#
# Save the changes:
#
# person.save
# person.name_in_database # => "Alice"
# person.saved_change_to_name? # => true
# person.saved_change_to_name # => ["Allison", "Alice"]
# person.name_before_last_change # => "Allison"
#
# Similar to ActiveModel::Dirty, methods can be invoked as
# +saved_change_to_name?+ or by passing an argument to the generic method
# <tt>saved_change_to_attribute?("name")</tt>.
module Dirty
extend ActiveSupport::Concern

Expand Down

0 comments on commit b790387

Please sign in to comment.