Class: PaperTrail::ModelConfig
- Inherits:
-
Object
- Object
- PaperTrail::ModelConfig
- 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. Use on_destroy(:before) or disable belongs_to_required_by_default. STR
- E_HPT_ABSTRACT_CLASS =
<<~STR.squish.freeze An application model (%s) has been configured to use PaperTrail (via `has_paper_trail`), but the version model it has been told to use (%s) is an `abstract_class`. This could happen when an advanced feature called Custom Version Classes (http://bit.ly/2G4ch0G) is misconfigured. When all version classes are custom, PaperTrail::Version is configured to be an `abstract_class`. This is fine, but all application models must be configured to use concrete (not abstract) version models. STR
- DPR_PASSING_ASSOC_NAME_DIRECTLY_TO_VERSIONS_OPTION =
<<~STR.squish Passing versions association name as `has_paper_trail versions: %{versions_name}` is deprecated. Use `has_paper_trail versions: {name: %{versions_name}}` instead. The hash you pass to `versions:` is now passed directly to `has_many`. STR
- DPR_CLASS_NAME_OPTION =
<<~STR.squish Passing Version class name as `has_paper_trail class_name: %{class_name}` is deprecated. Use `has_paper_trail versions: {class_name: %{class_name}}` instead. The hash you pass to `versions:` is now passed directly to `has_many`. STR
Instance Method Summary collapse
-
#initialize(model_class) ⇒ ModelConfig
constructor
A new instance of ModelConfig.
-
#on_create ⇒ Object
Adds a callback that records a version after a “create” event.
-
#on_destroy(recording_order = "before") ⇒ Object
Adds a callback that records a version before or after a “destroy” event.
-
#on_touch ⇒ Object
Adds a callback that records a version after a “touch” event.
-
#on_update ⇒ Object
Adds a callback that records a version after an “update” event.
-
#setup(options = {}) ⇒ Object
private
Set up ‘@model_class` for PaperTrail.
- #version_class ⇒ Object private
Constructor Details
#initialize(model_class) ⇒ ModelConfig
Returns a new instance of ModelConfig.
32 33 34 |
# File 'lib/paper_trail/model_config.rb', line 32 def initialize(model_class) @model_class = model_class end |
Instance Method Details
#on_create ⇒ Object
Adds a callback that records a version after a “create” event.
39 40 41 42 43 44 |
# File 'lib/paper_trail/model_config.rb', line 39 def on_create @model_class.after_create { |r| r.paper_trail.record_create if r.paper_trail.save_version? } append_option_uniquely(:on, :create) end |
#on_destroy(recording_order = "before") ⇒ Object
Adds a callback that records a version before or after a “destroy” event.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/paper_trail/model_config.rb', line 49 def on_destroy(recording_order = "before") assert_valid_recording_order_for_on_destroy(recording_order) @model_class.send( "#{recording_order}_destroy", lambda do |r| return unless r.paper_trail.save_version? r.paper_trail.record_destroy(recording_order) end ) append_option_uniquely(:on, :destroy) end |
#on_touch ⇒ Object
Adds a callback that records a version after a “touch” event.
Rails < 6.0 (no longer supported by PT) had a bug where dirty-tracking did not occur during a ‘touch`. (github.com/rails/rails/issues/33429) See also: github.com/paper-trail-gem/paper_trail/issues/1121 github.com/paper-trail-gem/paper_trail/issues/1161 github.com/paper-trail-gem/paper_trail/pull/1285
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/paper_trail/model_config.rb', line 93 def on_touch @model_class.after_touch { |r| if r.paper_trail.save_version? r.paper_trail.record_update( force: false, in_after_callback: true, is_touch: true ) end } end |
#on_update ⇒ Object
Adds a callback that records a version after an “update” event.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/paper_trail/model_config.rb', line 64 def on_update @model_class.before_save { |r| r.paper_trail. } @model_class.after_update { |r| if r.paper_trail.save_version? r.paper_trail.record_update( force: false, in_after_callback: true, is_touch: false ) end } @model_class.after_update { |r| r.paper_trail.clear_version_instance } append_option_uniquely(: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.
108 109 110 111 112 113 114 115 116 |
# File 'lib/paper_trail/model_config.rb', line 108 def setup( = {}) [:on] ||= %i[create update destroy touch] [:on] = Array([:on]) # Support single symbol @model_class.send :include, ::PaperTrail::Model::InstanceMethods () setup_associations() @model_class.after_rollback { paper_trail.clear_rolled_back_versions } [:on] end |
#version_class ⇒ 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.
119 120 121 |
# File 'lib/paper_trail/model_config.rb', line 119 def version_class @version_class ||= @model_class.version_class_name.constantize end |