culturecode-track_changes 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/app/helpers/track_changes/diff_helper.rb +3 -2
- data/lib/track_changes/configuration.rb +6 -0
- data/lib/track_changes/engine.rb +1 -0
- data/lib/track_changes/model.rb +32 -6
- data/lib/track_changes/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 740a90693ee8263535857c6037b35d1f005a3ad3e9f6e1d200b8db1fefa82a89
|
4
|
+
data.tar.gz: df7bbc71ddfd165f0cc2cb347284b6fcb4aa334b58699e24e7294a35d8a540e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c06836667b94b7a4a873662a4ac105d4d4ad190b325c91f978147231d4cf107187c3d446f2e9d8250cb4e1a243166e712fed9a3c713c3bfe0db374620092db6d
|
7
|
+
data.tar.gz: 138ed42c7a8549ead7070afdc61fea6a0b00ea56745d2b3e906515eec785d81b73647a99eb0d5d80e608e3d0ebf9be404023caf4220d59b785609fcf1ae54587
|
data/README.md
CHANGED
@@ -40,6 +40,8 @@ By default all model attributes are tracked, except the primary_key, usually ```
|
|
40
40
|
- ```:methods``` accepts a field name or array of field names to track in addition to the default fields
|
41
41
|
- ```:track_timestamps``` accepts a boolean, enabling or disabling tracking of ```created_at``` and ```updated_at```
|
42
42
|
- ```:track_primary_key``` accepts a boolean, enabling or disabling tracking of the model's primary key
|
43
|
+
- ```:track_locking_column``` accepts a boolean, enabling or disabling tracking of the model's locking column
|
44
|
+
|
43
45
|
|
44
46
|
### Attribution
|
45
47
|
Changes can be attributed to a particular source. The source is saved as a string
|
@@ -33,8 +33,9 @@ module TrackChanges
|
|
33
33
|
end
|
34
34
|
|
35
35
|
if reflection
|
36
|
-
|
37
|
-
|
36
|
+
primary_key = reflection.options.fetch(:primary_key, :id)
|
37
|
+
from = reflection.klass.find_by(primary_key => from) || content_tag(:span, 'DELETED', :class => 'deleted', :title => "This #{field_name} has been deleted") if from.present?
|
38
|
+
to = reflection.klass.find_by(primary_key => to) || content_tag(:span, 'DELETED', :class => 'deleted', :title => "This #{field_name} has been deleted") if to.present?
|
38
39
|
end
|
39
40
|
|
40
41
|
if from.blank?
|
data/lib/track_changes/engine.rb
CHANGED
data/lib/track_changes/model.rb
CHANGED
@@ -11,8 +11,10 @@ module TrackChanges
|
|
11
11
|
attr_accessor :track_changes # Faux attribute to allow disabling of change tracking on this record
|
12
12
|
attr_writer :track_changes_by # Faux attribute to store who made the changes so we can save it in the diff
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
# A representation of this record as it was last saved
|
15
|
+
has_one :snapshot, :as => :record, :class_name => 'TrackChanges::Snapshot', :dependent => Configuration.cascade_destroy ? :destroy : nil
|
16
|
+
# A representation of changes made between saves through this record's lifetime
|
17
|
+
has_many :diffs, lambda { reorder('id DESC') }, :as => :record, :class_name => 'TrackChanges::Diff', :dependent => Configuration.cascade_destroy ? :delete_all : nil
|
16
18
|
|
17
19
|
after_save :persist_tracked_changes
|
18
20
|
end
|
@@ -21,11 +23,18 @@ module TrackChanges
|
|
21
23
|
module ClassMethods
|
22
24
|
# Returns the method names to call to fetch the fields tracked for changes
|
23
25
|
def track_changes_fields
|
24
|
-
fields = Array(track_changes_options[:only]).collect(&:to_s).presence ||
|
26
|
+
fields = Array(track_changes_options[:only]).collect(&:to_s).presence || default_track_changes_fields
|
25
27
|
fields -= Array(track_changes_options[:except]).collect(&:to_s)
|
26
28
|
fields += Array(track_changes_options[:methods]).collect(&:to_s)
|
27
29
|
fields -= ['created_at', 'updated_at'] unless track_changes_options[:track_timestamps]
|
28
30
|
fields -= [primary_key] unless track_changes_options[:track_primary_key]
|
31
|
+
fields -= [locking_column] unless track_changes_options[:track_locking_column]
|
32
|
+
|
33
|
+
return fields.uniq
|
34
|
+
end
|
35
|
+
|
36
|
+
def default_track_changes_fields
|
37
|
+
attribute_names - stored_attributes.keys.map(&:to_s) + stored_attributes.values.flatten.map(&:to_s)
|
29
38
|
end
|
30
39
|
|
31
40
|
# Create snapshots for all records so that the next changes made are captured
|
@@ -36,11 +45,18 @@ module TrackChanges
|
|
36
45
|
# Create new snapshots
|
37
46
|
where.not(:id => joins(:snapshot)).find_each(&:create_snapshot)
|
38
47
|
end
|
48
|
+
|
49
|
+
# Record a diff and update the snapshot for all records in the scope
|
50
|
+
# This can be used to record a diff after an `update_all`.
|
51
|
+
def persist_tracked_changes(track_changes_by: nil)
|
52
|
+
find_each do |record|
|
53
|
+
record.track_changes_by = track_changes_by
|
54
|
+
record.persist_tracked_changes
|
55
|
+
end
|
56
|
+
end
|
39
57
|
end
|
40
58
|
|
41
59
|
module InstanceMethods
|
42
|
-
private
|
43
|
-
|
44
60
|
def track_changes_by
|
45
61
|
@track_changes_by || TrackChanges.default_attribution
|
46
62
|
end
|
@@ -49,7 +65,7 @@ module TrackChanges
|
|
49
65
|
def persist_tracked_changes
|
50
66
|
return if track_changes == false
|
51
67
|
|
52
|
-
new_record =
|
68
|
+
new_record = was_new_record_before_save?
|
53
69
|
action = new_record ? 'create' : 'update'
|
54
70
|
changes_by = track_changes_by.is_a?(ActiveRecord::Base) ? track_changes_by.id : track_changes_by
|
55
71
|
|
@@ -64,6 +80,16 @@ module TrackChanges
|
|
64
80
|
snapshot.create_diff(:action => action, :changes_by => changes_by, :from => {})
|
65
81
|
end
|
66
82
|
end
|
83
|
+
|
84
|
+
def was_new_record_before_save?
|
85
|
+
if !respond_to?(:attribute_before_last_save) # Rails < 6
|
86
|
+
id_was.blank?
|
87
|
+
elsif saved_change_to_attribute?(:id) # Rails <=5
|
88
|
+
attribute_before_last_save(:id).blank?
|
89
|
+
else # Allow this method to be used outside of a transaction, e.g. after a bulk update
|
90
|
+
new_record?
|
91
|
+
end
|
92
|
+
end
|
67
93
|
end
|
68
94
|
end
|
69
95
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: culturecode-track_changes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Jakobsen, Ryan Wallace
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/tasks/track_changes_tasks.rake
|
57
57
|
- lib/track_changes.rb
|
58
58
|
- lib/track_changes/attribution.rb
|
59
|
+
- lib/track_changes/configuration.rb
|
59
60
|
- lib/track_changes/controller.rb
|
60
61
|
- lib/track_changes/engine.rb
|
61
62
|
- lib/track_changes/model.rb
|
@@ -79,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
80
|
- !ruby/object:Gem::Version
|
80
81
|
version: '0'
|
81
82
|
requirements: []
|
82
|
-
|
83
|
-
rubygems_version: 2.7.9
|
83
|
+
rubygems_version: 3.3.23
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: Easily track changes to various ActiveRecord models
|