2017-12-11 04:05:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-18 04:32:24 +00:00
|
|
|
require "paper_trail/attribute_serializers/object_attribute"
|
|
|
|
require "paper_trail/attribute_serializers/object_changes_attribute"
|
2016-02-23 22:59:14 +00:00
|
|
|
require "paper_trail/model_config"
|
|
|
|
require "paper_trail/record_trail"
|
2015-05-08 17:28:47 +00:00
|
|
|
|
2009-05-27 15:21:20 +00:00
|
|
|
module PaperTrail
|
2016-04-09 05:08:34 +00:00
|
|
|
# Extensions to `ActiveRecord::Base`. See `frameworks/active_record.rb`.
|
2016-02-23 22:59:14 +00:00
|
|
|
# It is our goal to have the smallest possible footprint here, because
|
|
|
|
# `ActiveRecord::Base` is a very crowded namespace! That is why we introduced
|
|
|
|
# `.paper_trail` and `#paper_trail`.
|
2010-03-19 17:21:16 +00:00
|
|
|
module Model
|
|
|
|
def self.included(base)
|
|
|
|
base.send :extend, ClassMethods
|
|
|
|
end
|
2009-05-27 15:21:20 +00:00
|
|
|
|
2016-04-09 05:08:34 +00:00
|
|
|
# :nodoc:
|
2010-03-19 17:21:16 +00:00
|
|
|
module ClassMethods
|
2015-08-03 20:45:42 +00:00
|
|
|
# Declare this in your model to track every create, update, and destroy.
|
|
|
|
# Each version of the model is available in the `versions` association.
|
2010-03-19 18:53:49 +00:00
|
|
|
#
|
2010-03-19 17:21:16 +00:00
|
|
|
# Options:
|
2015-08-03 20:45:42 +00:00
|
|
|
#
|
|
|
|
# - :on - The events to track (optional; defaults to all of them). Set
|
|
|
|
# to an array of `:create`, `:update`, `:destroy` as desired.
|
|
|
|
# - :class_name - The name of a custom Version class. This class should
|
|
|
|
# inherit from `PaperTrail::Version`.
|
|
|
|
# - :ignore - An array of attributes for which a new `Version` will not be
|
|
|
|
# created if only they change. It can also aceept a Hash as an
|
|
|
|
# argument where the key is the attribute to ignore (a `String` or
|
|
|
|
# `Symbol`), which will only be ignored if the value is a `Proc` which
|
|
|
|
# returns truthily.
|
|
|
|
# - :if, :unless - Procs that allow to specify conditions when to save
|
|
|
|
# versions for an object.
|
|
|
|
# - :only - Inverse of `ignore`. A new `Version` will be created only
|
|
|
|
# for these attributes if supplied it can also aceept a Hash as an
|
|
|
|
# argument where the key is the attribute to track (a `String` or
|
|
|
|
# `Symbol`), which will only be counted if the value is a `Proc` which
|
|
|
|
# returns truthily.
|
|
|
|
# - :skip - Fields to ignore completely. As with `ignore`, updates to
|
|
|
|
# these fields will not create a new `Version`. In addition, these
|
|
|
|
# fields will not be included in the serialized versions of the object
|
|
|
|
# whenever a new `Version` is created.
|
|
|
|
# - :meta - A hash of extra data to store. You must add a column to the
|
|
|
|
# `versions` table for each key. Values are objects or procs (which
|
|
|
|
# are called with `self`, i.e. the model with the paper trail). See
|
|
|
|
# `PaperTrail::Controller.info_for_paper_trail` for how to store data
|
|
|
|
# from the controller.
|
|
|
|
# - :versions - The name to use for the versions association. Default
|
|
|
|
# is `:versions`.
|
|
|
|
# - :version - The name to use for the method which returns the version
|
|
|
|
# the instance was reified from. Default is `:version`.
|
2018-09-04 18:34:31 +00:00
|
|
|
#
|
|
|
|
# If you are using the experimental `paper_trail-association_tracking` gem
|
|
|
|
# it may accept additional options.
|
2015-03-23 12:10:35 +00:00
|
|
|
#
|
2016-04-21 05:16:28 +00:00
|
|
|
# @api public
|
2010-03-19 17:21:16 +00:00
|
|
|
def has_paper_trail(options = {})
|
2016-02-23 22:59:14 +00:00
|
|
|
paper_trail.setup(options)
|
|
|
|
end
|
2011-07-18 15:07:46 +00:00
|
|
|
|
2016-02-23 22:59:14 +00:00
|
|
|
# @api public
|
|
|
|
def paper_trail
|
|
|
|
::PaperTrail::ModelConfig.new(self)
|
|
|
|
end
|
2009-05-27 15:21:20 +00:00
|
|
|
end
|
|
|
|
|
2010-04-06 16:27:15 +00:00
|
|
|
# Wrap the following methods in a module so we can include them only in the
|
|
|
|
# ActiveRecord models that declare `has_paper_trail`.
|
2010-03-19 17:21:16 +00:00
|
|
|
module InstanceMethods
|
2016-09-08 22:21:18 +00:00
|
|
|
# @api public
|
2016-02-23 22:59:14 +00:00
|
|
|
def paper_trail
|
|
|
|
::PaperTrail::RecordTrail.new(self)
|
|
|
|
end
|
2012-01-16 16:17:57 +00:00
|
|
|
end
|
2010-03-19 17:21:16 +00:00
|
|
|
end
|
2009-05-27 15:21:20 +00:00
|
|
|
end
|