track_history 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ module HistoricalRelationHelpers
2
+
3
+ # Get a list of the modifications in a given history
4
+ def modifications
5
+ self.class.historical_fields.reject do |field, options|
6
+ send(options[:before]) == send(options[:after])
7
+ end.keys
8
+ end
9
+
10
+ def to_s
11
+ return 'modified nothing' if modifications.empty?
12
+ str = 'modified ' + modifications.sort.join(', ')
13
+ str += " on #{historical_relation}" if self.class.instance_variable_get(:@track_historical_reference)
14
+ str
15
+ end
16
+
17
+ end
@@ -0,0 +1,38 @@
1
+ module HistoryMethods
2
+
3
+ attr_reader :historical_fields, :historical_action_field
4
+
5
+ def track_historical_reference?
6
+ @track_historical_reference
7
+ end
8
+
9
+ def historical_fields
10
+ @historical_fields ||= {}
11
+ end
12
+
13
+ def historical_tracks
14
+ @historical_tracks ||= {}
15
+ end
16
+
17
+ def field(field, options = {})
18
+ options.assert_valid_keys(:before, :after)
19
+ field_s = field.is_a?(String) ? field : field.to_s
20
+ historical_fields[field_s] = {
21
+ :before => options[:before] || nil,
22
+ :after => options[:after] || nil
23
+ }
24
+ nil
25
+ end
26
+
27
+ def annotate(field, options = {}, &block) # haha
28
+ options.assert_valid_keys(:as)
29
+ save_as = options.has_key?(:as) ? options[:as] : field
30
+
31
+ unless columns_hash.has_key?(save_as.to_s)
32
+ raise ActiveRecord::StatementInvalid.new("No such attribute '#{save_as}' on #{self.name}")
33
+ end
34
+
35
+ historical_tracks[save_as] = block.nil? ? field : block
36
+ end
37
+
38
+ end
@@ -1,5 +1,5 @@
1
1
  module TrackHistory
2
2
 
3
- VERSION = '0.0.9'
3
+ VERSION = '0.0.10'
4
4
 
5
5
  end
data/lib/track_history.rb CHANGED
@@ -1,13 +1,24 @@
1
1
  module TrackHistory
2
2
 
3
- autoload :VERSION, File.join(File.dirname(__FILE__), 'track_history', 'version')
4
3
  require 'rubygems'
5
4
  require 'active_record'
6
5
 
6
+ autoload :VERSION, 'track_history/version'
7
+ autoload :HistoryMethods, 'track_history/history_methods'
8
+ autoload :HistoricalRelationHelpers, 'track_history/historical_relation_helpers'
9
+
7
10
  def self.install
8
11
  ActiveRecord::Base.send(:include, self)
9
12
  end
10
13
 
14
+ def self.warnings_disabled?
15
+ @warnings_disabled ||= false
16
+ end
17
+
18
+ def self.disable_warnings
19
+ @warnings_disabled = true
20
+ end
21
+
11
22
  def self.included(base)
12
23
  base.extend ActsAsMethods
13
24
  base.send(:include, InstanceMethods)
@@ -37,28 +48,28 @@ module TrackHistory
37
48
  @klass_reference.send(:table_name=, table_name) unless table_name.nil?
38
49
 
39
50
  unless @klass_reference.table_exists?
40
- STDERR.puts "[TrackHistory] No such table exists: #{@klass_reference.table_name}"
51
+ $stderr.puts "[TrackHistory] No such table exists: #{@klass_reference.table_name} - #{self.name} history will not be tracked" unless TrackHistory.warnings_disabled?
41
52
  return
42
53
  end
43
54
 
44
55
  # get the history class in line
45
56
  @klass_reference.send(:extend, HistoryMethods)
57
+
58
+ # figure out the field for tracking action (enum)
59
+ @klass_reference.instance_variable_set(:@historical_action_field, @klass_reference.columns_hash.has_key?('action') ? 'action' : nil)
60
+ @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
61
+
62
+ # allow other things to be specified
63
+ @klass_reference.module_eval(&block) if block_given?
46
64
 
47
65
  # infer fields
48
66
  @klass_reference.columns_hash.each_key do |k|
49
67
  matches = k.match(/(.+?)_before$/)
50
68
  if matches && matches.size == 2 && field_name = matches[1]
51
- next if @klass_reference.historical_fields.has_key?(field_name) # override inferrences
69
+ next if @klass_reference.historical_fields.has_key?(field_name) || @klass_reference.historical_tracks.has_key?(field_name) # override inferrences
52
70
  @klass_reference.historical_fields[field_name] = { :before => "#{field_name}_before".to_sym, :after => "#{field_name}_after".to_sym }
53
71
  end
54
72
  end
55
-
56
- # figure out the field for tracking action (enum)
57
- @klass_reference.instance_variable_set(:@historical_action_field, @klass_reference.columns_hash.has_key?('action') ? 'action' : nil)
58
- @klass_reference.instance_variable_set(:@track_historical_reference, track_reference)
59
-
60
- # allow other things to be specified
61
- @klass_reference.module_eval(&block) if block_given?
62
73
 
63
74
  # create the history class
64
75
  rel = base.name.singularize.underscore.downcase.to_sym
@@ -85,62 +96,6 @@ module TrackHistory
85
96
 
86
97
  end
87
98
 
88
- module HistoryMethods
89
-
90
- attr_reader :historical_fields, :historical_action_field
91
-
92
- def track_historical_reference?
93
- @track_historical_reference
94
- end
95
-
96
- def historical_fields
97
- @historical_fields ||= {}
98
- end
99
-
100
- def historical_tracks
101
- @historical_tracks ||= {}
102
- end
103
-
104
- def field(field, options = {})
105
- field_s = field.is_a?(String) ? field : field.to_s
106
- historical_fields[field_s] = {
107
- :before => options[:before] || "#{field}_before".to_sym,
108
- :after => options[:after] || "#{field}_after".to_sym
109
- }
110
- nil
111
- end
112
-
113
- def annotate(field, options = {}, &block) # haha
114
- options.assert_valid_keys(:as)
115
- save_as = options.has_key?(:as) ? options[:as] : field
116
-
117
- unless columns_hash.has_key?(save_as.to_s)
118
- raise ActiveRecord::StatementInvalid.new("No such attribute '#{field}' on #{@klass_reference.name}")
119
- end
120
-
121
- historical_tracks[save_as] = block.nil? ? field : block
122
- end
123
-
124
- end
125
-
126
- module HistoricalRelationHelpers
127
-
128
- # Get a list of the modifications in a given history
129
- def modifications
130
- self.class.historical_fields.reject do |field, options|
131
- send(options[:before]) == send(options[:after])
132
- end.keys
133
- end
134
-
135
- def to_s
136
- return 'modified nothing' if modifications.empty?
137
- str = 'modified ' + modifications.sort.join(', ')
138
- str += " on #{historical_relation}" if self.class.instance_variable_get(:@track_historical_reference)
139
- str
140
- end
141
-
142
- end
143
-
144
99
  module InstanceMethods
145
100
 
146
101
  private
@@ -153,8 +108,8 @@ module TrackHistory
153
108
  attributes = {}
154
109
  historical_fields.each do |field, field_options|
155
110
  next if !send(:"#{field}_changed?") && action == 'update'
156
- after_value = action == 'destroy' ? nil : send(field.to_sym) # special tracking on deletions
157
- attributes.merge! field_options[:before] => send(:"#{field}_was"), field_options[:after] => after_value
111
+ attributes[field_options[:after]] = (action == 'destroy' ? nil : send(field.to_sym)) if field_options[:after] # special tracking on deletions
112
+ attributes[field_options[:before]] = send(:"#{field}_was") if field_options[:before]
158
113
  end
159
114
  return if attributes.empty? && action == 'update' # nothing changed - skip out
160
115
  # then go through each track
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: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 9
10
- version: 0.0.9
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Crepezzi
@@ -55,6 +55,8 @@ extensions: []
55
55
  extra_rdoc_files: []
56
56
 
57
57
  files:
58
+ - lib/track_history/historical_relation_helpers.rb
59
+ - lib/track_history/history_methods.rb
58
60
  - lib/track_history/version.rb
59
61
  - lib/track_history.rb
60
62
  has_rdoc: true