Add has_paper_trail_defaults (#1166)
This commit is contained in:
parent
2c2a531773
commit
21cc89a696
|
@ -18,6 +18,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- [#1166](https://github.com/paper-trail-gem/paper_trail/pull/1166) -
|
||||||
|
New global option `has_paper_trail_defaults`, defaults for `has_paper_trail`
|
||||||
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Add the
|
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Add the
|
||||||
ability to pass options, such as `scope` or `extend:` to the `has_many
|
ability to pass options, such as `scope` or `extend:` to the `has_many
|
||||||
:versions` association macro.
|
:versions` association macro.
|
||||||
|
|
32
README.md
32
README.md
|
@ -268,6 +268,33 @@ A common place to put these settings is in a Rails initializer file
|
||||||
such as `config/initializers/paper_trail.rb` or in an environment-specific
|
such as `config/initializers/paper_trail.rb` or in an environment-specific
|
||||||
configuration file such as `config/environments/test.rb`.
|
configuration file such as `config/environments/test.rb`.
|
||||||
|
|
||||||
|
#### 1.e.1 Global
|
||||||
|
|
||||||
|
Global configuration options affect all threads.
|
||||||
|
|
||||||
|
- association_reify_error_behaviour
|
||||||
|
- enabled
|
||||||
|
- has_paper_trail_defaults
|
||||||
|
- object_changes_adapter
|
||||||
|
- serializer
|
||||||
|
- version_limit
|
||||||
|
|
||||||
|
Syntax example: (options described in detail later)
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# config/initializers/paper_trail.rb
|
||||||
|
PaperTrail.config.enabled = true
|
||||||
|
PaperTrail.config.has_paper_trail_defaults = {
|
||||||
|
on: %i[create update destroy]
|
||||||
|
}
|
||||||
|
PaperTrail.config.version_limit = 3
|
||||||
|
````
|
||||||
|
|
||||||
|
These options are intended to be set only once, during app initialization (eg.
|
||||||
|
in `config/initializers`). It is unsafe to change them while the app is running.
|
||||||
|
In contrast, `PaperTrail.request` has various options that only apply to a
|
||||||
|
single HTTP request and thus are safe to use while the app is running.
|
||||||
|
|
||||||
## 2. Limiting What is Versioned, and When
|
## 2. Limiting What is Versioned, and When
|
||||||
|
|
||||||
### 2.a. Choosing Lifecycle Events To Monitor
|
### 2.a. Choosing Lifecycle Events To Monitor
|
||||||
|
@ -617,8 +644,8 @@ previous and next versions.
|
||||||
```ruby
|
```ruby
|
||||||
widget = Widget.find 42
|
widget = Widget.find 42
|
||||||
version = widget.versions[-2] # assuming widget has several versions
|
version = widget.versions[-2] # assuming widget has several versions
|
||||||
previous = version.previous
|
previous_version = version.previous
|
||||||
next = version.next
|
next_version = version.next
|
||||||
```
|
```
|
||||||
|
|
||||||
You can find out which of an item's versions yours is:
|
You can find out which of an item's versions yours is:
|
||||||
|
@ -1000,7 +1027,6 @@ end
|
||||||
Overriding (instead of configuring) the `versions` method is not supported.
|
Overriding (instead of configuring) the `versions` method is not supported.
|
||||||
Overriding associations is not recommended in general.
|
Overriding associations is not recommended in general.
|
||||||
|
|
||||||
|
|
||||||
### 5.c. Generators
|
### 5.c. Generators
|
||||||
|
|
||||||
PaperTrail has one generator, `paper_trail:install`. It writes, but does not
|
PaperTrail has one generator, `paper_trail:install`. It writes, but does not
|
||||||
|
|
|
@ -21,7 +21,8 @@ module PaperTrail
|
||||||
:association_reify_error_behaviour,
|
:association_reify_error_behaviour,
|
||||||
:object_changes_adapter,
|
:object_changes_adapter,
|
||||||
:serializer,
|
:serializer,
|
||||||
:version_limit
|
:version_limit,
|
||||||
|
:has_paper_trail_defaults
|
||||||
)
|
)
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -31,6 +32,7 @@ module PaperTrail
|
||||||
|
|
||||||
# Variables which affect all threads, whose access is *not* synchronized.
|
# Variables which affect all threads, whose access is *not* synchronized.
|
||||||
@serializer = PaperTrail::Serializers::YAML
|
@serializer = PaperTrail::Serializers::YAML
|
||||||
|
@has_paper_trail_defaults = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates whether PaperTrail is on or off. Default: true.
|
# Indicates whether PaperTrail is on or off. Default: true.
|
||||||
|
|
|
@ -23,18 +23,18 @@ module PaperTrail
|
||||||
# Options:
|
# Options:
|
||||||
#
|
#
|
||||||
# - :on - The events to track (optional; defaults to all of them). Set
|
# - :on - The events to track (optional; defaults to all of them). Set
|
||||||
# to an array of `:create`, `:update`, `:destroy` as desired.
|
# to an array of `:create`, `:update`, `:destroy` and `:touch` as desired.
|
||||||
# - :class_name (deprecated) - The name of a custom Version class that
|
# - :class_name (deprecated) - The name of a custom Version class that
|
||||||
# includes `PaperTrail::VersionConcern`.
|
# includes `PaperTrail::VersionConcern`.
|
||||||
# - :ignore - An array of attributes for which a new `Version` will not be
|
# - :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
|
# created if only they change. It can also accept a Hash as an
|
||||||
# argument where the key is the attribute to ignore (a `String` or
|
# 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
|
# `Symbol`), which will only be ignored if the value is a `Proc` which
|
||||||
# returns truthily.
|
# returns truthily.
|
||||||
# - :if, :unless - Procs that allow to specify conditions when to save
|
# - :if, :unless - Procs that allow to specify conditions when to save
|
||||||
# versions for an object.
|
# versions for an object.
|
||||||
# - :only - Inverse of `ignore`. A new `Version` will be created only
|
# - :only - Inverse of `ignore`. A new `Version` will be created only
|
||||||
# for these attributes if supplied it can also aceept a Hash as an
|
# for these attributes if supplied it can also accept a Hash as an
|
||||||
# argument where the key is the attribute to track (a `String` or
|
# 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
|
# `Symbol`), which will only be counted if the value is a `Proc` which
|
||||||
# returns truthily.
|
# returns truthily.
|
||||||
|
@ -57,9 +57,15 @@ module PaperTrail
|
||||||
# Plugins like the experimental `paper_trail-association_tracking` gem
|
# Plugins like the experimental `paper_trail-association_tracking` gem
|
||||||
# may accept additional options.
|
# may accept additional options.
|
||||||
#
|
#
|
||||||
|
# You can define a default set of options via the configurable
|
||||||
|
# `PaperTrail.config.has_paper_trail_defaults` hash in your applications
|
||||||
|
# initializer. The hash can contain any of the following options and will
|
||||||
|
# provide an overridable default for all models.
|
||||||
|
#
|
||||||
# @api public
|
# @api public
|
||||||
def has_paper_trail(options = {})
|
def has_paper_trail(options = {})
|
||||||
paper_trail.setup(options)
|
defaults = PaperTrail.config.has_paper_trail_defaults
|
||||||
|
paper_trail.setup(defaults.merge(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api public
|
# @api public
|
||||||
|
|
Loading…
Reference in New Issue