paper_trail 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -582,6 +582,7 @@ Many thanks to:
582
582
  * [Emmanuel Gomez](https://github.com/emmanuel)
583
583
  * [Matthew MacLeod](https://github.com/mattmacleod)
584
584
  * [benzittlau](https://github.com/benzittlau)
585
+ * [Tom Derks](https://github.com/EgoH)
585
586
 
586
587
 
587
588
  ## Inspirations
@@ -25,9 +25,9 @@ module PaperTrail
25
25
 
26
26
  # The version this instance was reified from.
27
27
  attr_accessor :version
28
-
28
+
29
29
  cattr_accessor :version_class_name
30
- self.version_class_name = options[:class_name] || "Version"
30
+ self.version_class_name = options[:class_name] || 'Version'
31
31
 
32
32
  cattr_accessor :ignore
33
33
  self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
@@ -72,7 +72,7 @@ module PaperTrail
72
72
 
73
73
  # Returns who put the object into its current state.
74
74
  def originator
75
- version_class_name.constantize.with_item_keys(self.class.name, id).last.try :whodunnit
75
+ version_class.with_item_keys(self.class.name, id).last.try :whodunnit
76
76
  end
77
77
 
78
78
  # Returns the object (not a Version) as it was at the given timestamp.
@@ -99,6 +99,10 @@ module PaperTrail
99
99
 
100
100
  private
101
101
 
102
+ def version_class
103
+ version_class_name.constantize
104
+ end
105
+
102
106
  def record_create
103
107
  if switched_on?
104
108
  versions.create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
@@ -115,7 +119,7 @@ module PaperTrail
115
119
 
116
120
  def record_destroy
117
121
  if switched_on? and not new_record?
118
- version_class_name.constantize.create merge_metadata(:item_id => self.id,
122
+ version_class.create merge_metadata(:item_id => self.id,
119
123
  :item_type => self.class.name,
120
124
  :event => 'destroy',
121
125
  :object => object_to_string(item_before_change),
@@ -52,7 +52,8 @@ class Version < ActiveRecord::Base
52
52
  if item
53
53
  model = item
54
54
  else
55
- class_name = attrs['type'].blank? ? item_type : attrs['type']
55
+ inheritance_column_name = item_type.constantize.inheritance_column
56
+ class_name = attrs[inheritance_column_name].blank? ? item_type : attrs[inheritance_column_name]
56
57
  klass = class_name.constantize
57
58
  model = klass.new
58
59
  end
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.1.0'
2
+ VERSION = '2.1.1'
3
3
  end
@@ -0,0 +1,3 @@
1
+ class Animal < ActiveRecord::Base
2
+ has_paper_trail
3
+ end
@@ -0,0 +1,3 @@
1
+ class Cat < Animal
2
+ set_inheritance_column 'species'
3
+ end
@@ -0,0 +1,3 @@
1
+ class Dog < Animal
2
+ set_inheritance_column 'species'
3
+ end
@@ -82,14 +82,20 @@ class SetUpTestTables < ActiveRecord::Migration
82
82
  create_table :songs, :force => true do |t|
83
83
  t.integer :length
84
84
  end
85
-
85
+
86
86
  create_table :posts, :force => true do |t|
87
87
  t.string :title
88
88
  t.string :content
89
89
  end
90
+
91
+ create_table :animals, :force => true do |t|
92
+ t.string :name
93
+ t.string :species # single table inheritance column
94
+ end
90
95
  end
91
96
 
92
97
  def self.down
98
+ drop_table :animals
93
99
  drop_table :posts
94
100
  drop_table :songs
95
101
  drop_table :people
Binary file
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class InheritanceColumnTest < ActiveSupport::TestCase
4
+
5
+ context 'STI models' do
6
+ setup do
7
+ @animal = Animal.create :name => 'Animal'
8
+ @animal.update_attributes :name => 'Animal from the Muppets'
9
+ @animal.update_attributes :name => 'Animal Muppet'
10
+
11
+ @dog = Dog.create :name => 'Snoopy'
12
+ @dog.update_attributes :name => 'Scooby'
13
+ @dog.update_attributes :name => 'Scooby Doo'
14
+
15
+ @cat = Cat.create :name => 'Garfield'
16
+ @cat.update_attributes :name => 'Garfield (I hate Mondays)'
17
+ @cat.update_attributes :name => 'Garfield The Cat'
18
+ end
19
+
20
+ should 'work with custom STI inheritance column' do
21
+ assert_equal 9, Version.count
22
+ assert_equal 3, @animal.versions.count
23
+ assert_equal 3, @dog.versions.count
24
+ assert_equal 3, @cat.versions.count
25
+
26
+ assert_equal 'Animal from the Muppets', @animal.versions.last.reify.name
27
+ assert_equal 'Scooby', @dog.versions.last.reify.name
28
+ assert_equal 'Garfield (I hate Mondays)', @cat.versions.last.reify.name
29
+ end
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 1
9
- - 0
10
- version: 2.1.0
9
+ - 1
10
+ version: 2.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andy Stewart
@@ -108,9 +108,12 @@ files:
108
108
  - test/dummy/app/controllers/test_controller.rb
109
109
  - test/dummy/app/controllers/widgets_controller.rb
110
110
  - test/dummy/app/helpers/application_helper.rb
111
+ - test/dummy/app/models/animal.rb
111
112
  - test/dummy/app/models/article.rb
112
113
  - test/dummy/app/models/authorship.rb
113
114
  - test/dummy/app/models/book.rb
115
+ - test/dummy/app/models/cat.rb
116
+ - test/dummy/app/models/dog.rb
114
117
  - test/dummy/app/models/fluxor.rb
115
118
  - test/dummy/app/models/foo_widget.rb
116
119
  - test/dummy/app/models/person.rb
@@ -157,6 +160,7 @@ files:
157
160
  - test/paper_trail_test.rb
158
161
  - test/support/integration_case.rb
159
162
  - test/test_helper.rb
163
+ - test/unit/inheritance_column_test.rb
160
164
  - test/unit/model_test.rb
161
165
  has_rdoc: true
162
166
  homepage: http://github.com/airblade/paper_trail
@@ -198,9 +202,12 @@ test_files:
198
202
  - test/dummy/app/controllers/test_controller.rb
199
203
  - test/dummy/app/controllers/widgets_controller.rb
200
204
  - test/dummy/app/helpers/application_helper.rb
205
+ - test/dummy/app/models/animal.rb
201
206
  - test/dummy/app/models/article.rb
202
207
  - test/dummy/app/models/authorship.rb
203
208
  - test/dummy/app/models/book.rb
209
+ - test/dummy/app/models/cat.rb
210
+ - test/dummy/app/models/dog.rb
204
211
  - test/dummy/app/models/fluxor.rb
205
212
  - test/dummy/app/models/foo_widget.rb
206
213
  - test/dummy/app/models/person.rb
@@ -247,4 +254,5 @@ test_files:
247
254
  - test/paper_trail_test.rb
248
255
  - test/support/integration_case.rb
249
256
  - test/test_helper.rb
257
+ - test/unit/inheritance_column_test.rb
250
258
  - test/unit/model_test.rb