track_history 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9912ac6c6cf2c97cf3bb34b89ceaf51ef304837c8355f9690f86d759fe785443
4
+ data.tar.gz: 9e78921d5a036b6843e821aa2bbc634f3a7674b32e7a89a1df382a14bb6145a8
5
+ SHA512:
6
+ metadata.gz: 96c793635774b9151a94726caa9c0e662559b131cc9f8b9050908a1e85186f9e66670da387e2661c56c0896d2d695d9f00dabedebfd9672b282cfd22b78a2cd5
7
+ data.tar.gz: 4bec89fa316d1573a76aa8611b8c1947c332e5f3e32f8abf78100647cb422ba8a7d594522e9a5e3f4c9ed3982fd8e790501b98b7364b75f910144aa92dbb2fbf
@@ -54,26 +54,26 @@ module TrackHistory
54
54
  $stderr.puts "[TrackHistory] No such table exists: #{@klass_reference.table_name} - #{self.name} history will not be tracked" unless TrackHistory.warnings_disabled?
55
55
  return
56
56
  end
57
-
57
+
58
58
  # get the history class in line
59
59
  @klass_reference.send(:extend, TrackHistory::HistoryMethods)
60
-
60
+
61
61
  # figure out the field for tracking action (enum)
62
62
  @klass_reference.instance_variable_set(:@historical_action_field, @klass_reference.columns_hash.has_key?('action') ? 'action' : nil)
63
- @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
63
+ @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
64
64
 
65
65
  # allow other things to be specified
66
66
  @klass_reference.module_eval(&block) if block_given?
67
67
 
68
68
  # infer fields
69
- @klass_reference.columns_hash.each_key do |k|
69
+ @klass_reference.columns_hash.each_key do |k|
70
70
  matches = k.match(/(.+?)_before$/)
71
71
  if matches && matches.size == 2 && field_name = matches[1]
72
72
  next if @klass_reference.historical_fields.has_key?(field_name) || @klass_reference.historical_tracks.has_key?(field_name) # override inferrences
73
73
  @klass_reference.historical_fields[field_name] = { :before => "#{field_name}_before".to_sym, :after => "#{field_name}_after".to_sym }
74
74
  end
75
75
  end
76
-
76
+
77
77
  # create the history class
78
78
  rel = base.name.singularize.underscore.downcase.to_sym
79
79
  @klass_reference.send(:include, TrackHistory::HistoricalRelationHelpers)
@@ -82,7 +82,7 @@ module TrackHistory
82
82
  if track_reference
83
83
  @klass_reference.belongs_to rel
84
84
  @klass_reference.send(:alias_method, :historical_relation, rel)
85
- has_many :histories, :class_name => model_name, :order => 'id desc' if track_reference
85
+ has_many :histories, -> { order 'id desc' }, :class_name => model_name if track_reference
86
86
  end
87
87
 
88
88
  # tell the other class about us
@@ -114,15 +114,28 @@ module TrackHistory
114
114
  def record_historical_changes(action = 'update')
115
115
  historical_fields = self.class.historical_class.historical_fields
116
116
  historical_tracks = self.class.historical_class.historical_tracks
117
+
117
118
  return if historical_fields.empty? && historical_tracks.empty?
118
119
  # go through each and build the hashes
119
120
  attributes = {}
120
121
  historical_fields.each do |field, field_options|
121
- next if !send(:"#{field}_changed?") && action == 'update'
122
- attributes[field_options[:after]] = (action == 'destroy' ? nil : send(field.to_sym)) if field_options[:after] # special tracking on deletions
123
- attributes[field_options[:before]] = send(:"#{field}_was") if field_options[:before]
122
+
123
+ old, new = case action
124
+ when 'update'
125
+ send("#{field}_change")
126
+ when 'destroy'
127
+ [send(field), nil]
128
+ when 'create'
129
+ [nil, send(field)]
130
+ end
131
+
132
+ next if old == nil && new == nil
133
+
134
+ attributes[field_options[:after]] = new if field_options[:after]
135
+ attributes[field_options[:before]] = old if field_options[:before]
124
136
  end
125
- return if attributes.empty? && action == 'update' # nothing changed - skip out
137
+
138
+ return if attributes.empty? && action == 'update' # nothing changed - skip out
126
139
  # then go through each track
127
140
  historical_tracks.each do |field, block|
128
141
  attributes[field] = block.is_a?(Symbol) ? send(block) : (block.arity == 1 ? block.call(self) : instance_eval(&block)) # give access to the user object
@@ -131,6 +144,7 @@ module TrackHistory
131
144
  if action_field = self.class.historical_class.historical_action_field
132
145
  attributes[action_field] = action
133
146
  end
147
+
134
148
  # record the change
135
149
  if self.class.historical_class.track_historical_reference?
136
150
  self.histories.create(attributes)
@@ -1,5 +1,5 @@
1
1
  module TrackHistory
2
2
 
3
- VERSION = '0.1.0'
3
+ VERSION = '1.0.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,70 +1,88 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: track_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Crepezzi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2011-09-16 00:00:00.000000000Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rspec
16
- requirement: &70166017027660 !ruby/object:Gem::Requirement
17
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
18
30
  requirements:
19
- - - ! '>='
31
+ - - ">="
20
32
  - !ruby/object:Gem::Version
21
33
  version: '0'
22
34
  type: :development
23
35
  prerelease: false
24
- version_requirements: *70166017027660
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
25
41
  - !ruby/object:Gem::Dependency
26
- name: mysql
27
- requirement: &70166017015560 !ruby/object:Gem::Requirement
28
- none: false
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
29
44
  requirements:
30
- - - ! '>='
45
+ - - ">="
31
46
  - !ruby/object:Gem::Version
32
47
  version: '0'
33
48
  type: :development
34
49
  prerelease: false
35
- version_requirements: *70166017015560
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
36
55
  description: Smart, performant model auditing
37
56
  email: john.crepezzi@patch.com
38
57
  executables: []
39
58
  extensions: []
40
59
  extra_rdoc_files: []
41
60
  files:
61
+ - lib/track_history.rb
42
62
  - lib/track_history/historical_relation_helpers.rb
43
63
  - lib/track_history/history_methods.rb
44
64
  - lib/track_history/version.rb
45
- - lib/track_history.rb
46
65
  homepage: http://github.com/seejohnrun/historical
47
66
  licenses: []
67
+ metadata: {}
48
68
  post_install_message:
49
69
  rdoc_options: []
50
70
  require_paths:
51
71
  - lib
52
72
  required_ruby_version: !ruby/object:Gem::Requirement
53
- none: false
54
73
  requirements:
55
- - - ! '>='
74
+ - - ">="
56
75
  - !ruby/object:Gem::Version
57
76
  version: '0'
58
77
  required_rubygems_version: !ruby/object:Gem::Requirement
59
- none: false
60
78
  requirements:
61
- - - ! '>='
79
+ - - ">="
62
80
  - !ruby/object:Gem::Version
63
81
  version: '0'
64
82
  requirements: []
65
83
  rubyforge_project: historical
66
- rubygems_version: 1.8.10
84
+ rubygems_version: 2.7.6
67
85
  signing_key:
68
- specification_version: 3
86
+ specification_version: 4
69
87
  summary: Smart model auditing
70
88
  test_files: []