mongo_trails 10.3.1 → 14.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +53 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +19 -0
- data/.ruby-version +1 -0
- data/Appraisals +10 -4
- data/Gemfile +8 -1
- data/README.md +27 -6
- data/Rakefile +17 -0
- data/gemfiles/rails_7.gemfile +15 -0
- data/gemfiles/rails_7.gemfile.lock +212 -0
- data/gemfiles/rails_8.gemfile +15 -0
- data/gemfiles/rails_8.gemfile.lock +213 -0
- data/gemfiles/rails_8_1.gemfile +15 -0
- data/gemfiles/rails_8_1.gemfile.lock +143 -0
- data/lib/mongo_trails/callback_propagation.rb +77 -0
- data/lib/mongo_trails/config.rb +7 -24
- data/lib/mongo_trails/events/base.rb +5 -308
- data/lib/mongo_trails/model_config.rb +1 -232
- data/lib/mongo_trails/mongo_support/config.rb +7 -1
- data/lib/mongo_trails/mongo_support/criteria.rb +7 -0
- data/lib/mongo_trails/mongo_support/version.rb +242 -19
- data/lib/mongo_trails/mongo_support/version_commit_wrap.rb +37 -0
- data/lib/mongo_trails/mongo_support/write_version_worker.rb +11 -0
- data/lib/mongo_trails/record_trail.rb +21 -262
- data/lib/mongo_trails/version.rb +5 -0
- data/lib/mongo_trails/version_concern.rb +4 -293
- data/lib/mongo_trails.rb +13 -149
- data/mongo_trails.gemspec +19 -20
- metadata +50 -85
- data/.travis.yml +0 -13
- data/Gemfile.lock +0 -62
- data/gemfiles/rails_5.gemfile +0 -9
- data/gemfiles/rails_5.gemfile.lock +0 -63
- data/gemfiles/rails_6.gemfile +0 -9
- data/gemfiles/rails_6.gemfile.lock +0 -63
- data/lib/mongo_trails/attribute_serializers/README.md +0 -10
- data/lib/mongo_trails/attribute_serializers/attribute_serializer_factory.rb +0 -27
- data/lib/mongo_trails/attribute_serializers/cast_attribute_serializer.rb +0 -51
- data/lib/mongo_trails/attribute_serializers/object_attribute.rb +0 -41
- data/lib/mongo_trails/attribute_serializers/object_changes_attribute.rb +0 -44
- data/lib/mongo_trails/cleaner.rb +0 -60
- data/lib/mongo_trails/compatibility.rb +0 -51
- data/lib/mongo_trails/events/create.rb +0 -32
- data/lib/mongo_trails/events/destroy.rb +0 -42
- data/lib/mongo_trails/events/update.rb +0 -60
- data/lib/mongo_trails/frameworks/cucumber.rb +0 -33
- data/lib/mongo_trails/frameworks/rails/controller.rb +0 -109
- data/lib/mongo_trails/frameworks/rails/engine.rb +0 -43
- data/lib/mongo_trails/frameworks/rails.rb +0 -4
- data/lib/mongo_trails/frameworks/rspec/helpers.rb +0 -29
- data/lib/mongo_trails/frameworks/rspec.rb +0 -43
- data/lib/mongo_trails/has_paper_trail.rb +0 -86
- data/lib/mongo_trails/queries/versions/where_object.rb +0 -65
- data/lib/mongo_trails/queries/versions/where_object_changes.rb +0 -75
- data/lib/mongo_trails/record_history.rb +0 -51
- data/lib/mongo_trails/reifier.rb +0 -130
- data/lib/mongo_trails/request.rb +0 -166
- data/lib/mongo_trails/serializers/json.rb +0 -46
- data/lib/mongo_trails/serializers/yaml.rb +0 -43
- data/lib/mongo_trails/type_serializers/postgres_array_serializer.rb +0 -48
- data/lib/mongo_trails/version_number.rb +0 -23
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/type_serializers/postgres_array_serializer"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module AttributeSerializers
|
|
7
|
-
# Values returned by some Active Record serializers are
|
|
8
|
-
# not suited for writing JSON to a text column. This factory
|
|
9
|
-
# replaces certain default Active Record serializers
|
|
10
|
-
# with custom PaperTrail ones.
|
|
11
|
-
module AttributeSerializerFactory
|
|
12
|
-
AR_PG_ARRAY_CLASS = "ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array"
|
|
13
|
-
|
|
14
|
-
def self.for(klass, attr)
|
|
15
|
-
active_record_serializer = klass.type_for_attribute(attr)
|
|
16
|
-
if active_record_serializer.class.name == AR_PG_ARRAY_CLASS
|
|
17
|
-
TypeSerializers::PostgresArraySerializer.new(
|
|
18
|
-
active_record_serializer.subtype,
|
|
19
|
-
active_record_serializer.delimiter
|
|
20
|
-
)
|
|
21
|
-
else
|
|
22
|
-
active_record_serializer
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/attribute_serializers/attribute_serializer_factory"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
# :nodoc:
|
|
7
|
-
module AttributeSerializers
|
|
8
|
-
# The `CastAttributeSerializer` (de)serializes model attribute values. For
|
|
9
|
-
# example, the string "1.99" serializes into the integer `1` when assigned
|
|
10
|
-
# to an attribute of type `ActiveRecord::Type::Integer`.
|
|
11
|
-
#
|
|
12
|
-
# This implementation depends on the `type_for_attribute` method, which was
|
|
13
|
-
# introduced in rails 4.2. As of PT 8, we no longer support rails < 4.2.
|
|
14
|
-
class CastAttributeSerializer
|
|
15
|
-
def initialize(klass)
|
|
16
|
-
@klass = klass
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
private
|
|
20
|
-
|
|
21
|
-
# Returns a hash mapping attributes to hashes that map strings to
|
|
22
|
-
# integers. Example:
|
|
23
|
-
#
|
|
24
|
-
# ```
|
|
25
|
-
# { "status" => { "draft"=>0, "published"=>1, "archived"=>2 } }
|
|
26
|
-
# ```
|
|
27
|
-
#
|
|
28
|
-
# ActiveRecord::Enum was added in AR 4.1
|
|
29
|
-
# http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums
|
|
30
|
-
def defined_enums
|
|
31
|
-
@defined_enums ||= (@klass.respond_to?(:defined_enums) ? @klass.defined_enums : {})
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Uses AR 5's `serialize` and `deserialize`.
|
|
36
|
-
class CastAttributeSerializer
|
|
37
|
-
def serialize(attr, val)
|
|
38
|
-
AttributeSerializerFactory.for(@klass, attr).serialize(val)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def deserialize(attr, val)
|
|
42
|
-
if defined_enums[attr] && val.is_a?(::String)
|
|
43
|
-
# Because PT 4 used to save the string version of enums to `object_changes`
|
|
44
|
-
val
|
|
45
|
-
else
|
|
46
|
-
AttributeSerializerFactory.for(@klass, attr).deserialize(val)
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/attribute_serializers/cast_attribute_serializer"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module AttributeSerializers
|
|
7
|
-
# Serialize or deserialize the `version.object` column.
|
|
8
|
-
class ObjectAttribute
|
|
9
|
-
def initialize(model_class)
|
|
10
|
-
@model_class = model_class
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def serialize(attributes)
|
|
14
|
-
alter(attributes, :serialize)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def deserialize(attributes)
|
|
18
|
-
alter(attributes, :deserialize)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
# Modifies `attributes` in place.
|
|
24
|
-
# TODO: Return a new hash instead.
|
|
25
|
-
def alter(attributes, serialization_method)
|
|
26
|
-
# Don't serialize before values before inserting into columns of type
|
|
27
|
-
# `JSON` on `PostgreSQL` databases.
|
|
28
|
-
return attributes if object_col_is_json?
|
|
29
|
-
|
|
30
|
-
serializer = CastAttributeSerializer.new(@model_class)
|
|
31
|
-
attributes.each do |key, value|
|
|
32
|
-
attributes[key] = serializer.send(serialization_method, key, value)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def object_col_is_json?
|
|
37
|
-
@model_class.paper_trail.version_class.object_col_is_json?
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/attribute_serializers/cast_attribute_serializer"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module AttributeSerializers
|
|
7
|
-
# Serialize or deserialize the `version.object_changes` column.
|
|
8
|
-
class ObjectChangesAttribute
|
|
9
|
-
def initialize(item_class)
|
|
10
|
-
@item_class = item_class
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def serialize(changes)
|
|
14
|
-
alter(changes, :serialize)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def deserialize(changes)
|
|
18
|
-
alter(changes, :deserialize)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
# Modifies `changes` in place.
|
|
24
|
-
# TODO: Return a new hash instead.
|
|
25
|
-
def alter(changes, serialization_method)
|
|
26
|
-
# Don't serialize before values before inserting into columns of type
|
|
27
|
-
# `JSON` on `PostgreSQL` databases.
|
|
28
|
-
return changes if object_changes_col_is_json?
|
|
29
|
-
|
|
30
|
-
serializer = CastAttributeSerializer.new(@item_class)
|
|
31
|
-
changes.clone.each do |key, change|
|
|
32
|
-
# `change` is an Array with two elements, representing before and after.
|
|
33
|
-
changes[key] = Array(change).map do |value|
|
|
34
|
-
serializer.send(serialization_method, key, value)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def object_changes_col_is_json?
|
|
40
|
-
@item_class.paper_trail.version_class.object_changes_col_is_json?
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
data/lib/mongo_trails/cleaner.rb
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module PaperTrail
|
|
4
|
-
# Utilities for deleting version records.
|
|
5
|
-
module Cleaner
|
|
6
|
-
# Destroys all but the most recent version(s) for items on a given date
|
|
7
|
-
# (or on all dates). Useful for deleting drafts.
|
|
8
|
-
#
|
|
9
|
-
# Options:
|
|
10
|
-
#
|
|
11
|
-
# - :keeping - An `integer` indicating the number of versions to be kept for
|
|
12
|
-
# each item per date. Defaults to `1`. The most recent matching versions
|
|
13
|
-
# are kept.
|
|
14
|
-
# - :date - Should either be a `Date` object specifying which date to
|
|
15
|
-
# destroy versions for or `:all`, which will specify that all dates
|
|
16
|
-
# should be cleaned. Defaults to `:all`.
|
|
17
|
-
# - :item_id - The `id` for the item to be cleaned on, or `nil`, which
|
|
18
|
-
# causes all items to be cleaned. Defaults to `nil`.
|
|
19
|
-
#
|
|
20
|
-
def clean_versions!(options = {})
|
|
21
|
-
options = { keeping: 1, date: :all }.merge(options)
|
|
22
|
-
gather_versions(options[:item_id], options[:date]).each do |_item_id, item_versions|
|
|
23
|
-
group_versions_by_date(item_versions).each do |_date, date_versions|
|
|
24
|
-
# Remove the number of versions we wish to keep from the collection
|
|
25
|
-
# of versions prior to destruction.
|
|
26
|
-
date_versions.pop(options[:keeping])
|
|
27
|
-
date_versions.map(&:destroy)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
# Returns a hash of versions grouped by the `item_id` attribute formatted
|
|
35
|
-
# like this: {:item_id => PaperTrail::Version}. If `item_id` or `date` is
|
|
36
|
-
# set, versions will be narrowed to those pointing at items with those ids
|
|
37
|
-
# that were created on specified date. Versions are returned in
|
|
38
|
-
# chronological order.
|
|
39
|
-
def gather_versions(item_id = nil, date = :all)
|
|
40
|
-
unless date == :all || date.respond_to?(:to_date)
|
|
41
|
-
raise ArgumentError, "Expected date to be a Timestamp or :all"
|
|
42
|
-
end
|
|
43
|
-
versions = item_id ? PaperTrail::Version.where(item_id: item_id) : PaperTrail::Version
|
|
44
|
-
versions = versions.order(PaperTrail::Version.timestamp_sort_order)
|
|
45
|
-
versions = versions.between(date.to_date, date.to_date + 1.day) unless date == :all
|
|
46
|
-
|
|
47
|
-
# If `versions` has not been converted to an ActiveRecord::Relation yet,
|
|
48
|
-
# do so now.
|
|
49
|
-
versions = PaperTrail::Version.all if versions == PaperTrail::Version
|
|
50
|
-
versions.group_by(&:item_id)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# Given an array of versions, returns a hash mapping dates to arrays of
|
|
54
|
-
# versions.
|
|
55
|
-
# @api private
|
|
56
|
-
def group_versions_by_date(versions)
|
|
57
|
-
versions.group_by { |v| v.created_at.to_date }
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module PaperTrail
|
|
4
|
-
# Rails does not follow SemVer, makes breaking changes in minor versions.
|
|
5
|
-
# Breaking changes are expected, and are generally good for the rails
|
|
6
|
-
# ecosystem. However, they often require dozens of hours to fix, even with the
|
|
7
|
-
# [help of experts](https://github.com/paper-trail-gem/mongo_trails/pull/899).
|
|
8
|
-
#
|
|
9
|
-
# It is not safe to assume that a new version of rails will be compatible with
|
|
10
|
-
# PaperTrail. PT is only compatible with the versions of rails that it is
|
|
11
|
-
# tested against. See `.travis.yml`.
|
|
12
|
-
#
|
|
13
|
-
# However, as of
|
|
14
|
-
# [#1213](https://github.com/paper-trail-gem/mongo_trails/pull/1213) our
|
|
15
|
-
# gemspec allows installation with newer, incompatible rails versions. We hope
|
|
16
|
-
# this will make it easier for contributors to work on compatibility with
|
|
17
|
-
# newer rails versions. Most PT users should avoid incompatible rails
|
|
18
|
-
# versions.
|
|
19
|
-
module Compatibility
|
|
20
|
-
ACTIVERECORD_GTE = ">= 5.2" # enforced in gemspec
|
|
21
|
-
ACTIVERECORD_LT = "< 6.1" # not enforced in gemspec
|
|
22
|
-
|
|
23
|
-
E_INCOMPATIBLE_AR = <<-EOS
|
|
24
|
-
PaperTrail %s is not compatible with ActiveRecord %s. We allow PT
|
|
25
|
-
contributors to install incompatible versions of ActiveRecord, and this
|
|
26
|
-
warning can be silenced with an environment variable, but this is a bad
|
|
27
|
-
idea for normal use. Please install a compatible version of ActiveRecord
|
|
28
|
-
instead (%s). Please see the discussion in mongo_trails/compatibility.rb
|
|
29
|
-
for details.
|
|
30
|
-
EOS
|
|
31
|
-
|
|
32
|
-
# Normal users need a warning if they accidentally install an incompatible
|
|
33
|
-
# version of ActiveRecord. Contributors can silence this warning with an
|
|
34
|
-
# environment variable.
|
|
35
|
-
def self.check_activerecord(ar_version)
|
|
36
|
-
raise ::TypeError unless ar_version.instance_of?(::Gem::Version)
|
|
37
|
-
return if ::ENV["PT_SILENCE_AR_COMPAT_WARNING"].present?
|
|
38
|
-
req = ::Gem::Requirement.new([ACTIVERECORD_GTE, ACTIVERECORD_LT])
|
|
39
|
-
unless req.satisfied_by?(ar_version)
|
|
40
|
-
::Kernel.warn(
|
|
41
|
-
format(
|
|
42
|
-
E_INCOMPATIBLE_AR,
|
|
43
|
-
::PaperTrail.gem_version,
|
|
44
|
-
ar_version,
|
|
45
|
-
req
|
|
46
|
-
)
|
|
47
|
-
)
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/events/base"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module Events
|
|
7
|
-
# See docs in `Base`.
|
|
8
|
-
#
|
|
9
|
-
# @api private
|
|
10
|
-
class Create < Base
|
|
11
|
-
# Return attributes of nascent `Version` record.
|
|
12
|
-
#
|
|
13
|
-
# @api private
|
|
14
|
-
def data
|
|
15
|
-
data = {
|
|
16
|
-
item: @record,
|
|
17
|
-
event: @record.paper_trail_event || "create",
|
|
18
|
-
whodunnit: PaperTrail.request.whodunnit
|
|
19
|
-
}
|
|
20
|
-
if @record.respond_to?(:updated_at)
|
|
21
|
-
data[:created_at] = @record.updated_at
|
|
22
|
-
end
|
|
23
|
-
if record_object_changes? && changed_notably?
|
|
24
|
-
changes = notable_changes
|
|
25
|
-
data[:object_changes] = prepare_object_changes(changes)
|
|
26
|
-
end
|
|
27
|
-
merge_item_subtype_into(data)
|
|
28
|
-
merge_metadata_into(data)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/events/base"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module Events
|
|
7
|
-
# See docs in `Base`.
|
|
8
|
-
#
|
|
9
|
-
# @api private
|
|
10
|
-
class Destroy < Base
|
|
11
|
-
# Return attributes of nascent `Version` record.
|
|
12
|
-
#
|
|
13
|
-
# @api private
|
|
14
|
-
def data
|
|
15
|
-
data = {
|
|
16
|
-
item_id: @record.id,
|
|
17
|
-
item_type: @record.class.base_class.name,
|
|
18
|
-
event: @record.paper_trail_event || "destroy",
|
|
19
|
-
whodunnit: PaperTrail.request.whodunnit
|
|
20
|
-
}
|
|
21
|
-
if record_object?
|
|
22
|
-
data[:object] = recordable_object(false)
|
|
23
|
-
end
|
|
24
|
-
if record_object_changes?
|
|
25
|
-
data[:object_changes] = prepare_object_changes(notable_changes)
|
|
26
|
-
end
|
|
27
|
-
merge_item_subtype_into(data)
|
|
28
|
-
merge_metadata_into(data)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
private
|
|
32
|
-
|
|
33
|
-
# Rails' implementation (eg. `@record.saved_changes`) returns nothing on
|
|
34
|
-
# destroy, so we have to build the hash we want.
|
|
35
|
-
#
|
|
36
|
-
# @override
|
|
37
|
-
def changes_in_latest_version
|
|
38
|
-
@record.attributes.map { |attr, value| [attr, [value, nil]] }.to_h
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "mongo_trails/events/base"
|
|
4
|
-
|
|
5
|
-
module PaperTrail
|
|
6
|
-
module Events
|
|
7
|
-
# See docs in `Base`.
|
|
8
|
-
#
|
|
9
|
-
# @api private
|
|
10
|
-
class Update < Base
|
|
11
|
-
# - is_touch - [boolean] - Used in the two situations that are touch-like:
|
|
12
|
-
# - `after_touch` we call `RecordTrail#record_update`
|
|
13
|
-
# - force_changes - [Hash] - Only used by `RecordTrail#update_columns`,
|
|
14
|
-
# because there dirty-tracking is off, so it has to track its own changes.
|
|
15
|
-
#
|
|
16
|
-
# @api private
|
|
17
|
-
def initialize(record, in_after_callback, is_touch, force_changes)
|
|
18
|
-
super(record, in_after_callback)
|
|
19
|
-
@is_touch = is_touch
|
|
20
|
-
@force_changes = force_changes
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Return attributes of nascent `Version` record.
|
|
24
|
-
#
|
|
25
|
-
# @api private
|
|
26
|
-
def data
|
|
27
|
-
data = {
|
|
28
|
-
item: @record,
|
|
29
|
-
event: @record.paper_trail_event || "update",
|
|
30
|
-
whodunnit: PaperTrail.request.whodunnit
|
|
31
|
-
}
|
|
32
|
-
if @record.respond_to?(:updated_at)
|
|
33
|
-
data[:created_at] = @record.updated_at
|
|
34
|
-
end
|
|
35
|
-
if record_object?
|
|
36
|
-
data[:object] = recordable_object(@is_touch)
|
|
37
|
-
end
|
|
38
|
-
if record_object_changes?
|
|
39
|
-
changes = @force_changes.nil? ? notable_changes : @force_changes
|
|
40
|
-
data[:object_changes] = prepare_object_changes(changes)
|
|
41
|
-
end
|
|
42
|
-
merge_item_subtype_into(data)
|
|
43
|
-
merge_metadata_into(data)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
private
|
|
47
|
-
|
|
48
|
-
# `touch` cannot record `object_changes` because rails' `touch` does not
|
|
49
|
-
# perform dirty-tracking. Specifically, methods from `Dirty`, like
|
|
50
|
-
# `saved_changes`, return the same values before and after `touch`.
|
|
51
|
-
#
|
|
52
|
-
# See https://github.com/rails/rails/issues/33429
|
|
53
|
-
#
|
|
54
|
-
# @api private
|
|
55
|
-
def record_object_changes?
|
|
56
|
-
!@is_touch && super
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# before hook for Cucumber
|
|
4
|
-
Before do
|
|
5
|
-
PaperTrail.enabled = false
|
|
6
|
-
PaperTrail.request.enabled = true
|
|
7
|
-
PaperTrail.request.whodunnit = nil
|
|
8
|
-
PaperTrail.request.controller_info = {} if defined?(::Rails)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
module PaperTrail
|
|
12
|
-
module Cucumber
|
|
13
|
-
# Helper method for enabling PT in Cucumber features.
|
|
14
|
-
module Extensions
|
|
15
|
-
# :call-seq:
|
|
16
|
-
# with_versioning
|
|
17
|
-
#
|
|
18
|
-
# enable versioning for specific blocks
|
|
19
|
-
|
|
20
|
-
def with_versioning
|
|
21
|
-
was_enabled = ::PaperTrail.enabled?
|
|
22
|
-
::PaperTrail.enabled = true
|
|
23
|
-
begin
|
|
24
|
-
yield
|
|
25
|
-
ensure
|
|
26
|
-
::PaperTrail.enabled = was_enabled
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
World PaperTrail::Cucumber::Extensions
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module PaperTrail
|
|
4
|
-
module Rails
|
|
5
|
-
# Extensions to rails controllers. Provides convenient ways to pass certain
|
|
6
|
-
# information to the model layer, with `controller_info` and `whodunnit`.
|
|
7
|
-
# Also includes a convenient on/off switch,
|
|
8
|
-
# `paper_trail_enabled_for_controller`.
|
|
9
|
-
module Controller
|
|
10
|
-
def self.included(controller)
|
|
11
|
-
controller.before_action(
|
|
12
|
-
:set_paper_trail_enabled_for_controller,
|
|
13
|
-
:set_paper_trail_controller_info
|
|
14
|
-
)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
protected
|
|
18
|
-
|
|
19
|
-
# Returns the user who is responsible for any changes that occur.
|
|
20
|
-
# By default this calls `current_user` and returns the result.
|
|
21
|
-
#
|
|
22
|
-
# Override this method in your controller to call a different
|
|
23
|
-
# method, e.g. `current_person`, or anything you like.
|
|
24
|
-
#
|
|
25
|
-
# @api public
|
|
26
|
-
def user_for_paper_trail
|
|
27
|
-
return unless defined?(current_user)
|
|
28
|
-
current_user.try(:id) || current_user
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Returns any information about the controller or request that you
|
|
32
|
-
# want PaperTrail to store alongside any changes that occur. By
|
|
33
|
-
# default this returns an empty hash.
|
|
34
|
-
#
|
|
35
|
-
# Override this method in your controller to return a hash of any
|
|
36
|
-
# information you need. The hash's keys must correspond to columns
|
|
37
|
-
# in your `versions` table, so don't forget to add any new columns
|
|
38
|
-
# you need.
|
|
39
|
-
#
|
|
40
|
-
# For example:
|
|
41
|
-
#
|
|
42
|
-
# {:ip => request.remote_ip, :user_agent => request.user_agent}
|
|
43
|
-
#
|
|
44
|
-
# The columns `ip` and `user_agent` must exist in your `versions` # table.
|
|
45
|
-
#
|
|
46
|
-
# Use the `:meta` option to
|
|
47
|
-
# `PaperTrail::Model::ClassMethods.has_paper_trail` to store any extra
|
|
48
|
-
# model-level data you need.
|
|
49
|
-
#
|
|
50
|
-
# @api public
|
|
51
|
-
def info_for_paper_trail
|
|
52
|
-
{}
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Returns `true` (default) or `false` depending on whether PaperTrail
|
|
56
|
-
# should be active for the current request.
|
|
57
|
-
#
|
|
58
|
-
# Override this method in your controller to specify when PaperTrail
|
|
59
|
-
# should be off.
|
|
60
|
-
#
|
|
61
|
-
# ```
|
|
62
|
-
# def paper_trail_enabled_for_controller
|
|
63
|
-
# # Don't omit `super` without a good reason.
|
|
64
|
-
# super && request.user_agent != 'Disable User-Agent'
|
|
65
|
-
# end
|
|
66
|
-
# ```
|
|
67
|
-
#
|
|
68
|
-
# @api public
|
|
69
|
-
def paper_trail_enabled_for_controller
|
|
70
|
-
::PaperTrail.enabled?
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
private
|
|
74
|
-
|
|
75
|
-
# Tells PaperTrail whether versions should be saved in the current
|
|
76
|
-
# request.
|
|
77
|
-
#
|
|
78
|
-
# @api public
|
|
79
|
-
def set_paper_trail_enabled_for_controller
|
|
80
|
-
::PaperTrail.request.enabled = paper_trail_enabled_for_controller
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Tells PaperTrail who is responsible for any changes that occur.
|
|
84
|
-
#
|
|
85
|
-
# @api public
|
|
86
|
-
def set_paper_trail_whodunnit
|
|
87
|
-
if ::PaperTrail.request.enabled?
|
|
88
|
-
::PaperTrail.request.whodunnit = user_for_paper_trail
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# Tells PaperTrail any information from the controller you want to store
|
|
93
|
-
# alongside any changes that occur.
|
|
94
|
-
#
|
|
95
|
-
# @api public
|
|
96
|
-
def set_paper_trail_controller_info
|
|
97
|
-
if ::PaperTrail.request.enabled?
|
|
98
|
-
::PaperTrail.request.controller_info = info_for_paper_trail
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
if defined?(::ActionController)
|
|
106
|
-
::ActiveSupport.on_load(:action_controller) do
|
|
107
|
-
include ::PaperTrail::Rails::Controller
|
|
108
|
-
end
|
|
109
|
-
end
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module PaperTrail
|
|
4
|
-
module Rails
|
|
5
|
-
# See http://guides.rubyonrails.org/engines.html
|
|
6
|
-
class Engine < ::Rails::Engine
|
|
7
|
-
DPR_CONFIG_ENABLED = <<~EOS.squish.freeze
|
|
8
|
-
The rails configuration option config.paper_trail.enabled is deprecated.
|
|
9
|
-
Please use PaperTrail.enabled= instead. People were getting confused
|
|
10
|
-
that PT has both, specifically regarding *when* each was happening. If
|
|
11
|
-
you'd like to keep config.paper_trail, join the discussion at
|
|
12
|
-
https://github.com/paper-trail-gem/mongo_trails/pull/1176
|
|
13
|
-
EOS
|
|
14
|
-
private_constant :DPR_CONFIG_ENABLED
|
|
15
|
-
DPR_RUDELY_ENABLING = <<~EOS.squish.freeze
|
|
16
|
-
At some point early in the rails boot process, you have set
|
|
17
|
-
PaperTrail.enabled = false. PT's rails engine is now overriding your
|
|
18
|
-
setting, and setting it to true. We're not sure why, but this is how PT
|
|
19
|
-
has worked since 5.0, when the config.paper_trail.enabled option was
|
|
20
|
-
introduced. This is now deprecated. In the future, PT will not override
|
|
21
|
-
your setting. See
|
|
22
|
-
https://github.com/paper-trail-gem/mongo_trails/pull/1176 for discussion.
|
|
23
|
-
EOS
|
|
24
|
-
private_constant :DPR_RUDELY_ENABLING
|
|
25
|
-
|
|
26
|
-
# --- Begin deprecated section ---
|
|
27
|
-
config.paper_trail = ActiveSupport::OrderedOptions.new
|
|
28
|
-
initializer "paper_trail.initialisation" do |app|
|
|
29
|
-
enable = app.config.paper_trail[:enabled]
|
|
30
|
-
if enable.nil?
|
|
31
|
-
unless PaperTrail.enabled?
|
|
32
|
-
::ActiveSupport::Deprecation.warn(DPR_RUDELY_ENABLING)
|
|
33
|
-
PaperTrail.enabled = true
|
|
34
|
-
end
|
|
35
|
-
else
|
|
36
|
-
::ActiveSupport::Deprecation.warn(DPR_CONFIG_ENABLED)
|
|
37
|
-
PaperTrail.enabled = enable
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
# --- End deprecated section ---
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module PaperTrail
|
|
4
|
-
module RSpec
|
|
5
|
-
module Helpers
|
|
6
|
-
# Included in the RSpec configuration in `frameworks/rspec.rb`
|
|
7
|
-
module InstanceMethods
|
|
8
|
-
# enable versioning for specific blocks (at instance-level)
|
|
9
|
-
def with_versioning
|
|
10
|
-
was_enabled = ::PaperTrail.enabled?
|
|
11
|
-
::PaperTrail.enabled = true
|
|
12
|
-
yield
|
|
13
|
-
ensure
|
|
14
|
-
::PaperTrail.enabled = was_enabled
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# Extended by the RSpec configuration in `frameworks/rspec.rb`
|
|
19
|
-
module ClassMethods
|
|
20
|
-
# enable versioning for specific blocks (at class-level)
|
|
21
|
-
def with_versioning(&block)
|
|
22
|
-
context "with versioning", versioning: true do
|
|
23
|
-
class_exec(&block)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "rspec/core"
|
|
4
|
-
require "rspec/matchers"
|
|
5
|
-
require "mongo_trails/frameworks/rspec/helpers"
|
|
6
|
-
|
|
7
|
-
RSpec.configure do |config|
|
|
8
|
-
config.include ::PaperTrail::RSpec::Helpers::InstanceMethods
|
|
9
|
-
config.extend ::PaperTrail::RSpec::Helpers::ClassMethods
|
|
10
|
-
|
|
11
|
-
config.before(:each) do
|
|
12
|
-
::Mongoid.purge!
|
|
13
|
-
::PaperTrail.enabled = false
|
|
14
|
-
::PaperTrail.request.enabled = true
|
|
15
|
-
::PaperTrail.request.whodunnit = nil
|
|
16
|
-
::PaperTrail.request.controller_info = {} if defined?(::Rails) && defined?(::RSpec::Rails)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
config.before(:each, versioning: true) do
|
|
20
|
-
::PaperTrail.enabled = true
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
RSpec::Matchers.define :be_versioned do
|
|
25
|
-
# check to see if the model has `has_paper_trail` declared on it
|
|
26
|
-
match { |actual| actual.is_a?(::PaperTrail::Model::InstanceMethods) }
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
RSpec::Matchers.define :have_a_version_with do |attributes|
|
|
30
|
-
# check if the model has a version with the specified attributes
|
|
31
|
-
match do |actual|
|
|
32
|
-
versions_association = actual.class.versions_association_name
|
|
33
|
-
actual.send(versions_association).where_object(attributes).any?
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
RSpec::Matchers.define :have_a_version_with_changes do |attributes|
|
|
38
|
-
# check if the model has a version changes with the specified attributes
|
|
39
|
-
match do |actual|
|
|
40
|
-
versions_association = actual.class.versions_association_name
|
|
41
|
-
actual.send(versions_association).where_object_changes(attributes).any?
|
|
42
|
-
end
|
|
43
|
-
end
|