track_history 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,9 +19,10 @@ module TrackHistory
19
19
  # Takes a hash of options, which can only be :model_name to force a different model name
20
20
  # Default model name is ModelHistory
21
21
  def track_history(options = {})
22
+ options.assert_valid_keys(:model_name, :table_name)
22
23
  @historical_fields = []
23
24
  @historical_tracks = {}
24
- define_historical_model(self, options[:model_name])
25
+ define_historical_model(self, options[:model_name], options[:table_name])
25
26
  yield if block_given?
26
27
  end
27
28
 
@@ -43,13 +44,13 @@ module TrackHistory
43
44
 
44
45
  private
45
46
 
46
- def define_historical_model(base, class_name)
47
-
48
- class_name ||= "#{base.name}History"
49
- klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
47
+ def define_historical_model(base, model_name, table_name)
48
+ model_name ||= "#{base.name}History"
49
+ klass = Object.const_set(model_name, Class.new(ActiveRecord::Base))
50
50
  @klass_reference = klass
51
51
 
52
52
  # infer fields
53
+ klass.send(:table_name=, table_name) unless table_name.nil?
53
54
  klass.columns_hash.each_key do |k|
54
55
  matches = k.match(/(.+?)_before$/)
55
56
  if matches && matches.size == 2 && field_name = matches[1]
@@ -65,7 +66,7 @@ module TrackHistory
65
66
 
66
67
  # tell the other class about us
67
68
  # purposely don't define these until after getting historical_fields
68
- has_many :histories, :class_name => class_name, :order => 'created_at desc', :dependent => :destroy
69
+ has_many :histories, :class_name => model_name, :order => 'created_at desc', :dependent => :destroy
69
70
  before_update :record_historical_changes
70
71
 
71
72
  end
@@ -1,5 +1,5 @@
1
1
  module TrackHistory
2
2
 
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
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: 27
5
- prerelease:
4
+ hash: 25
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Crepezzi
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  requirements: []
74
74
 
75
75
  rubyforge_project: historical
76
- rubygems_version: 1.4.1
76
+ rubygems_version: 1.3.7
77
77
  signing_key:
78
78
  specification_version: 3
79
79
  summary: Smart model auditing