culturecode-track_changes 0.1.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cfddea8f7f37242ef70cad9d17b2bbbea5d610a6
4
- data.tar.gz: 250ab77746cd7991f5eabe04b71dffec39be896a
2
+ SHA256:
3
+ metadata.gz: 740a90693ee8263535857c6037b35d1f005a3ad3e9f6e1d200b8db1fefa82a89
4
+ data.tar.gz: df7bbc71ddfd165f0cc2cb347284b6fcb4aa334b58699e24e7294a35d8a540e2
5
5
  SHA512:
6
- metadata.gz: 8f29bda1a82ad609e32868a3611be8ab996c52f496ff4b3150e64a46e6fbd0d6fc03d129b1bd4faae386b01a2e028154ac1e9922f1aa505c1c7ba83f1215d75c
7
- data.tar.gz: 9da20ea1201d384476e2ab94d07cc577f1f419ec9dcd9a0190cf1074a861f168121f60c8e7a057a27f167f601e7fa01f5dc115848a19b6b1350911ba4d139cc3
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
@@ -48,7 +50,8 @@ the record's id will be used.
48
50
 
49
51
  ```ruby
50
52
  # Model-level attribution
51
- person.save(:changes_by => 'Joe Changems')
53
+ person.track_changes_by = 'Joe Changems'
54
+ person.save
52
55
 
53
56
  # Block-level attribution
54
57
  TrackChanges.with_changes_attributed_to 'Joe Changems' do
@@ -33,8 +33,9 @@ module TrackChanges
33
33
  end
34
34
 
35
35
  if reflection
36
- from = reflection.klass.find_by_id(from) || content_tag(:span, 'DELETED', :class => 'deleted', :title => "This #{field_name} has been deleted") if from.present?
37
- to = reflection.klass.find_by_id(to) || content_tag(:span, 'DELETED', :class => 'deleted', :title => "This #{field_name} has been deleted") if to.present?
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?
@@ -7,6 +7,11 @@ module TrackChanges
7
7
  serialize :from, Hash
8
8
  serialize :to, Hash
9
9
 
10
+ # Returns changes but only those where the string representation of the value has changed
11
+ def visible_changes
12
+ changes.select {|key, (from, to)| (from.present? || to.present?) && (from.to_s != to.to_s) }
13
+ end
14
+
10
15
  # Returns a hash of changes where the key is the field name
11
16
  # and the value is an array of the from value and the to value
12
17
  def changes
@@ -0,0 +1,6 @@
1
+ module TrackChanges
2
+ module Configuration
3
+ mattr_accessor :cascade_destroy # Destroy tracked changes when record is destroyed?
4
+ self.cascade_destroy = true
5
+ end
6
+ end
@@ -1,3 +1,4 @@
1
+ require 'track_changes/configuration'
1
2
  require 'track_changes/model'
2
3
  require 'track_changes/controller'
3
4
  require 'track_changes/attribution'
@@ -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
- has_one :snapshot, :as => :record, :class_name => 'TrackChanges::Snapshot' # A representation of this record as it was last saved
15
- has_many :diffs, lambda { reorder('id DESC') }, :as => :record, :class_name => 'TrackChanges::Diff' # A representation of changes made between saves through this record's lifetime
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 || self.attribute_names
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 = id_was.blank?
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
@@ -1,3 +1,3 @@
1
1
  module TrackChanges
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: culturecode-track_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.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: 2015-09-29 00:00:00.000000000 Z
11
+ date: 2023-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.1
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.1
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -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
- rubyforge_project:
83
- rubygems_version: 2.4.6
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