track_history 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module TrackHistory
2
2
 
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
 
5
5
  end
data/lib/track_history.rb CHANGED
@@ -38,9 +38,8 @@ module TrackHistory
38
38
  # figure out the model name
39
39
  model_name ||= "#{base.name}History"
40
40
  @klass_reference = Object.const_set(model_name, Class.new(ActiveRecord::Base))
41
-
42
- # set up a way to record tracks
43
- @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
41
+
42
+ # get the history class in line
44
43
  @klass_reference.send(:extend, HistoryMethods)
45
44
 
46
45
  # infer fields
@@ -52,7 +51,11 @@ module TrackHistory
52
51
  @klass_reference.historical_fields[field_name] = { :before => "#{field_name}_before".to_sym, :after => "#{field_name}_after".to_sym }
53
52
  end
54
53
  end
55
-
54
+
55
+ # figure out the field for tracking action (enum)
56
+ @klass_reference.instance_variable_set(:@historical_action_field, @klass_reference.columns_hash.has_key?('action') ? 'action' : nil)
57
+ @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
58
+
56
59
  # allow other things to be specified
57
60
  @klass_reference.module_eval(&block) if block_given?
58
61
 
@@ -64,12 +67,18 @@ module TrackHistory
64
67
  if track_reference
65
68
  @klass_reference.belongs_to rel
66
69
  @klass_reference.send(:alias_method, :historical_relation, rel)
67
- has_many :histories, :class_name => model_name, :order => 'created_at desc' if track_reference
70
+ has_many :histories, :class_name => model_name, :order => 'created_at desc, id desc' if track_reference
68
71
  end
69
72
 
70
73
  # tell the other class about us
71
74
  # purposely don't define these until after getting historical_fields
72
- before_update :record_historical_changes
75
+ before_update { |c| c.send(:record_historical_changes, 'update') }
76
+
77
+ # track other things (optionally)
78
+ unless @klass_reference.historical_action_field.nil?
79
+ after_create { |c| c.send(:record_historical_changes, 'create') }
80
+ before_destroy { |c| c.send(:record_historical_changes, 'destroy') }
81
+ end
73
82
 
74
83
  end
75
84
 
@@ -77,7 +86,7 @@ module TrackHistory
77
86
 
78
87
  module HistoryMethods
79
88
 
80
- attr_reader :historical_fields
89
+ attr_reader :historical_fields, :historical_action_field
81
90
 
82
91
  def track_historical_reference?
83
92
  @track_historical_reference
@@ -91,7 +100,7 @@ module TrackHistory
91
100
  @historical_tracks ||= {}
92
101
  end
93
102
 
94
- def field(field, options = {}) # TODO rename
103
+ def field(field, options = {})
95
104
  field_s = field.is_a?(String) ? field : field.to_s
96
105
  historical_fields[field_s] = {
97
106
  :before => options[:before] || "#{field}_before".to_sym,
@@ -135,21 +144,26 @@ module TrackHistory
135
144
 
136
145
  private
137
146
 
138
- def record_historical_changes
147
+ def record_historical_changes(action)
139
148
  historical_fields = self.class.historical_class.historical_fields
140
149
  historical_tracks = self.class.historical_class.historical_tracks
141
150
  return if historical_fields.empty? && historical_tracks.empty?
142
151
  # go through each and build the hashes
143
152
  attributes = {}
144
153
  historical_fields.each do |field, field_options|
145
- next unless send(:"#{field}_changed?")
146
- attributes.merge! field_options[:before] => send(:"#{field}_was"), field_options[:after] => send(field.to_sym)
154
+ next if !send(:"#{field}_changed?") && action == 'update'
155
+ after_value = action == 'destroy' ? nil : send(field.to_sym) # special tracking on deletions
156
+ attributes.merge! field_options[:before] => send(:"#{field}_was"), field_options[:after] => after_value
147
157
  end
148
- return if attributes.empty? # nothing changed - skip out
158
+ return if attributes.empty? && action == 'update' # nothing changed - skip out
149
159
  # then go through each track
150
160
  historical_tracks.each do |field, block|
151
161
  attributes[field] = block.is_a?(Symbol) ? send(block) : (block.arity == 1 ? block.call(self) : instance_eval(&block)) # give access to the user object
152
162
  end
163
+ # determine the action_type if needed
164
+ if action_field = self.class.historical_class.historical_action_field
165
+ attributes[action_field] = action
166
+ end
153
167
  # record the change
154
168
  if self.class.historical_class.track_historical_reference?
155
169
  self.histories.create(attributes)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: track_history
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Crepezzi