Class: PaperTrail::ModelConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail/model_config.rb

Overview

Configures an ActiveRecord model, mostly at application boot time, but also sometimes mid-request, with methods like enable/disable.

Constant Summary collapse

E_CANNOT_RECORD_AFTER_DESTROY =
<<-STR.strip_heredoc.freeze
  paper_trail.on_destroy(:after) is incompatible with ActiveRecord's
  belongs_to_required_by_default and has no effect. Please use :before
  or disable belongs_to_required_by_default.
STR

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ ModelConfig



13
14
15
# File 'lib/paper_trail/model_config.rb', line 13

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#disableObject

Switches PaperTrail off for this class.



18
19
20
# File 'lib/paper_trail/model_config.rb', line 18

def disable
  ::PaperTrail.enabled_for_model(@model_class, false)
end

#enableObject

Switches PaperTrail on for this class.



23
24
25
# File 'lib/paper_trail/model_config.rb', line 23

def enable
  ::PaperTrail.enabled_for_model(@model_class, true)
end

#enabled?Boolean



27
28
29
30
# File 'lib/paper_trail/model_config.rb', line 27

def enabled?
  return false unless @model_class.include?(::PaperTrail::Model::InstanceMethods)
  ::PaperTrail.enabled_for_model?(@model_class)
end

#on_createObject

Adds a callback that records a version after a “create” event.



33
34
35
36
37
# File 'lib/paper_trail/model_config.rb', line 33

def on_create
  @model_class.after_create :record_create, if: ->(m) { m.paper_trail.save_version? }
  return if @model_class.paper_trail_options[:on].include?(:create)
  @model_class.paper_trail_options[:on] << :create
end

#on_destroy(recording_order = "before") ⇒ Object

Adds a callback that records a version before or after a “destroy” event.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paper_trail/model_config.rb', line 40

def on_destroy(recording_order = "before")
  unless %w(after before).include?(recording_order.to_s)
    raise ArgumentError, 'recording order can only be "after" or "before"'
  end

  if recording_order.to_s == "after" && cannot_record_after_destroy?
    ::ActiveSupport::Deprecation.warn(E_CANNOT_RECORD_AFTER_DESTROY)
  end

  @model_class.send "#{recording_order}_destroy", :record_destroy,
    if: ->(m) { m.paper_trail.save_version? }

  return if @model_class.paper_trail_options[:on].include?(:destroy)
  @model_class.paper_trail_options[:on] << :destroy
end

#on_updateObject

Adds a callback that records a version after an “update” event.



57
58
59
60
61
62
63
# File 'lib/paper_trail/model_config.rb', line 57

def on_update
  @model_class.before_save :reset_timestamp_attrs_for_update_if_needed!, on: :update
  @model_class.after_update :record_update, if: ->(m) { m.paper_trail.save_version? }
  @model_class.after_update :clear_version_instance!
  return if @model_class.paper_trail_options[:on].include?(:update)
  @model_class.paper_trail_options[:on] << :update
end

#setup(options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set up ‘@model_class` for PaperTrail. Installs callbacks, associations, “class attributes”, instance methods, and more.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/paper_trail/model_config.rb', line 68

def setup(options = {})
  options[:on] ||= [:create, :update, :destroy]
  options[:on] = Array(options[:on]) # Support single symbol
  @model_class.send :include, ::PaperTrail::Model::InstanceMethods
  if ::ActiveRecord::VERSION::STRING < "4.2"
    @model_class.send :extend, AttributeSerializers::LegacyActiveRecordShim
  end
  setup_options(options)
  setup_associations(options)
  setup_transaction_callbacks
  setup_callbacks_from_options options[:on]
  setup_callbacks_for_habtm options[:join_tables]
end

#version_classObject



82
83
84
# File 'lib/paper_trail/model_config.rb', line 82

def version_class
  @_version_class ||= @model_class.version_class_name.constantize
end