track_history 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/track_history.rb CHANGED
@@ -53,7 +53,7 @@ module TrackHistory
53
53
  end
54
54
 
55
55
  # get the history class in line
56
- @klass_reference.send(:extend, HistoryMethods)
56
+ @klass_reference.send(:extend, TrackHistory::HistoryMethods)
57
57
 
58
58
  # figure out the field for tracking action (enum)
59
59
  @klass_reference.instance_variable_set(:@historical_action_field, @klass_reference.columns_hash.has_key?('action') ? 'action' : nil)
@@ -73,23 +73,23 @@ module TrackHistory
73
73
 
74
74
  # create the history class
75
75
  rel = base.name.singularize.underscore.downcase.to_sym
76
- @klass_reference.send(:include, HistoricalRelationHelpers)
76
+ @klass_reference.send(:include, TrackHistory::HistoricalRelationHelpers)
77
77
 
78
78
  # create a backward reference
79
79
  if track_reference
80
80
  @klass_reference.belongs_to rel
81
81
  @klass_reference.send(:alias_method, :historical_relation, rel)
82
- has_many :histories, :class_name => model_name, :order => 'created_at desc, id desc' if track_reference
82
+ has_many :histories, :class_name => model_name, :order => 'id desc' if track_reference
83
83
  end
84
84
 
85
85
  # tell the other class about us
86
86
  # purposely don't define these until after getting historical_fields
87
- before_update { |c| c.send(:record_historical_changes, 'update') }
87
+ before_update :record_historical_changes
88
88
 
89
89
  # track other things (optionally)
90
90
  unless @klass_reference.historical_action_field.nil?
91
- after_create { |c| c.send(:record_historical_changes, 'create') }
92
- before_destroy { |c| c.send(:record_historical_changes, 'destroy') }
91
+ after_create :record_historical_changes_on_create
92
+ before_destroy :record_historical_changes_on_destroy
93
93
  end
94
94
 
95
95
  end
@@ -100,7 +100,15 @@ module TrackHistory
100
100
 
101
101
  private
102
102
 
103
- def record_historical_changes(action)
103
+ def record_historical_changes_on_destroy
104
+ record_historical_changes('destroy')
105
+ end
106
+
107
+ def record_historical_changes_on_create
108
+ record_historical_changes('create')
109
+ end
110
+
111
+ def record_historical_changes(action = 'update')
104
112
  historical_fields = self.class.historical_class.historical_fields
105
113
  historical_tracks = self.class.historical_class.historical_tracks
106
114
  return if historical_fields.empty? && historical_tracks.empty?
@@ -1,17 +1,21 @@
1
- module HistoricalRelationHelpers
1
+ module TrackHistory
2
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
3
+ module HistoricalRelationHelpers
4
+
5
+ # Get a list of the modifications in a given history
6
+ def modifications
7
+ self.class.historical_fields.reject do |field, options|
8
+ send(options[:before]) == send(options[:after])
9
+ end.keys
10
+ end
11
+
12
+ def to_s
13
+ return 'modified nothing' if modifications.empty?
14
+ str = 'modified ' + modifications.sort.join(', ')
15
+ str += " on #{historical_relation}" if self.class.instance_variable_get(:@track_historical_reference)
16
+ str
17
+ end
9
18
 
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
19
  end
16
20
 
17
21
  end
@@ -1,38 +1,42 @@
1
- module HistoryMethods
1
+ module TrackHistory
2
2
 
3
- attr_reader :historical_fields, :historical_action_field
3
+ module HistoryMethods
4
4
 
5
- def track_historical_reference?
6
- @track_historical_reference
7
- end
5
+ attr_reader :historical_fields, :historical_action_field
8
6
 
9
- def historical_fields
10
- @historical_fields ||= {}
11
- end
7
+ def track_historical_reference?
8
+ @track_historical_reference
9
+ end
12
10
 
13
- def historical_tracks
14
- @historical_tracks ||= {}
15
- end
11
+ def historical_fields
12
+ @historical_fields ||= {}
13
+ end
16
14
 
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
15
+ def historical_tracks
16
+ @historical_tracks ||= {}
17
+ end
18
+
19
+ def field(field, options = {})
20
+ options.assert_valid_keys(:before, :after)
21
+ field_s = field.is_a?(String) ? field : field.to_s
22
+ historical_fields[field_s] = {
23
+ :before => options[:before] || nil,
24
+ :after => options[:after] || nil
25
+ }
26
+ nil
27
+ end
28
+
29
+ def annotate(field, options = {}, &block) # haha
30
+ options.assert_valid_keys(:as)
31
+ save_as = options.has_key?(:as) ? options[:as] : field
26
32
 
27
- def annotate(field, options = {}, &block) # haha
28
- options.assert_valid_keys(:as)
29
- save_as = options.has_key?(:as) ? options[:as] : field
33
+ unless columns_hash.has_key?(save_as.to_s)
34
+ raise ActiveRecord::StatementInvalid.new("No such attribute '#{save_as}' on #{self.name}")
35
+ end
30
36
 
31
- unless columns_hash.has_key?(save_as.to_s)
32
- raise ActiveRecord::StatementInvalid.new("No such attribute '#{save_as}' on #{self.name}")
37
+ historical_tracks[save_as] = block.nil? ? field : block
33
38
  end
34
39
 
35
- historical_tracks[save_as] = block.nil? ? field : block
36
40
  end
37
41
 
38
42
  end
@@ -1,5 +1,5 @@
1
1
  module TrackHistory
2
2
 
3
- VERSION = '0.0.10'
3
+ VERSION = '0.0.11'
4
4
 
5
5
  end
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 10
10
- version: 0.0.10
9
+ - 11
10
+ version: 0.0.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Crepezzi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-11 00:00:00 -05:00
18
+ date: 2011-01-16 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency