Module: PaperTrail::Model::ClassMethods

Defined in:
lib/paper_trail/has_paper_trail.rb

Instance Method Summary collapse

Instance Method Details

#has_paper_trail(options = {}) ⇒ Object

Declare this in your model to track every create, update, and destroy. Each version of the model is available in the ‘versions` association.

Options: :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 Version. :ignore an array of attributes for which a new ‘Version` will not be created if only they change. :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied :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`.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/paper_trail/has_paper_trail.rb', line 24

def has_paper_trail(options = {})
  # Lazily include the instance methods so we don't clutter up
  # any more ActiveRecord models than we have to.
  send :include, InstanceMethods

  # The version this instance was reified from.
  attr_accessor :version

  class_attribute :version_class_name
  self.version_class_name = options[:class_name] || 'Version'

  class_attribute :ignore
  self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s

  class_attribute :only
  self.only = ([options[:only]].flatten.compact || []).map &:to_s

  class_attribute :meta
  self.meta = options[:meta] || {}

  class_attribute :track_columns
  self.track_columns = options[:track_columns] || true

  class_attribute :ignored_columns
  self.ignored_columns = options[:ignored_columns] || ['id', 'updated_at', 'created_at']

  class_attribute :paper_trail_enabled_for_model
  self.paper_trail_enabled_for_model = true

  class_attribute :versions_association_name
  self.versions_association_name = options[:versions] || :versions

  has_many self.versions_association_name,
           :class_name => version_class_name,
           :as         => :item,
           :order      => "created_at ASC, #{self.version_class_name.constantize.primary_key} ASC"

  after_create  :record_create  if !options[:on] || options[:on].include?(:create)
  before_update :record_update  if !options[:on] || options[:on].include?(:update)
  after_destroy :record_destroy if !options[:on] || options[:on].include?(:destroy)
end

#paper_trail_offObject

Switches PaperTrail off for this class.



67
68
69
# File 'lib/paper_trail/has_paper_trail.rb', line 67

def paper_trail_off
  self.paper_trail_enabled_for_model = false
end

#paper_trail_onObject

Switches PaperTrail on for this class.



72
73
74
# File 'lib/paper_trail/has_paper_trail.rb', line 72

def paper_trail_on
  self.paper_trail_enabled_for_model = true
end