paper_trail 2.6.0 → 2.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -358,6 +358,16 @@ This allows you to store each model's versions in a separate table, which is use
358
358
 
359
359
  Alternatively you could store certain metadata for one type of version, and other metadata for other versions.
360
360
 
361
+ If you only use custom version classes and don't use PaperTrail's built-in one, on Rails 3.2 you must:
362
+
363
+ - either declare PaperTrail's version class abstract like this (in `config/initializers/paper_trail_patch.rb`):
364
+
365
+ Version.module_eval do
366
+ self.abstract_class = true
367
+ end
368
+
369
+ - or define a `versions` table in the database so Rails can instantiate the version superclass.
370
+
361
371
  You can also specify custom names for the versions and version associations. This is useful if you already have `versions` or/and `version` methods on your model. For example:
362
372
 
363
373
  class Post < ActiveRecord::Base
@@ -705,6 +715,8 @@ Many thanks to:
705
715
  * [Peter Harkins](https://github.com/pushcx)
706
716
  * [Mohd Amree](https://github.com/amree)
707
717
  * [Nikita Cernovs](https://github.com/nikitachernov)
718
+ * [Jason Noble](https://github.com/jasonnoble)
719
+ * [Jared Mehle](https://github.com/jrmehle)
708
720
 
709
721
 
710
722
  ## Inspirations
@@ -32,6 +32,16 @@ module PaperTrail
32
32
  paper_trail_store[:request_enabled_for_controller] = value
33
33
  end
34
34
 
35
+ # Set the field which records when a version was created.
36
+ def self.timestamp_field=(field_name)
37
+ PaperTrail.config.timestamp_field = field_name
38
+ end
39
+
40
+ # Returns the field which records when a version was created.
41
+ def self.timestamp_field
42
+ PaperTrail.config.timestamp_field
43
+ end
44
+
35
45
  # Returns who is reponsible for any changes that occur.
36
46
  def self.whodunnit
37
47
  paper_trail_store[:whodunnit]
@@ -1,11 +1,12 @@
1
1
  module PaperTrail
2
2
  class Config
3
3
  include Singleton
4
- attr_accessor :enabled
4
+ attr_accessor :enabled, :timestamp_field
5
5
 
6
6
  def initialize
7
7
  # Indicates whether PaperTrail is on or off.
8
- @enabled = true
8
+ @enabled = true
9
+ @timestamp_field = :created_at
9
10
  end
10
11
  end
11
12
  end
@@ -68,7 +68,7 @@ module PaperTrail
68
68
  has_many self.versions_association_name,
69
69
  :class_name => version_class_name,
70
70
  :as => :item,
71
- :order => "created_at ASC, #{self.version_class_name.constantize.primary_key} ASC"
71
+ :order => "#{PaperTrail.timestamp_field} ASC, #{self.version_class_name.constantize.primary_key} ASC"
72
72
 
73
73
  after_create :record_create, :if => :save_version? if !options[:on] || options[:on].include?(:create)
74
74
  before_update :record_update, :if => :save_version? if !options[:on] || options[:on].include?(:update)
@@ -8,16 +8,16 @@ class Version < ActiveRecord::Base
8
8
  end
9
9
 
10
10
  scope :subsequent, lambda { |version|
11
- where(["#{self.primary_key} > ?", version.is_a?(self) ? version.id : version]).order("#{self.primary_key} ASC")
11
+ where(["#{self.primary_key} > ?", version.is_a?(self) ? version.send(self.primary_key) : version]).order("#{self.primary_key} ASC")
12
12
  }
13
13
 
14
14
  scope :preceding, lambda { |version|
15
- where(["#{self.primary_key} < ?", version.is_a?(self) ? version.id : version]).order("#{self.primary_key} DESC")
15
+ where(["#{self.primary_key} < ?", version.is_a?(self) ? version.send(self.primary_key) : version]).order("#{self.primary_key} DESC")
16
16
  }
17
17
 
18
18
  scope :following, lambda { |timestamp|
19
19
  # TODO: is this :order necessary, considering its presence on the has_many :versions association?
20
- where(['created_at > ?', timestamp]).order("created_at ASC, #{self.primary_key} ASC")
20
+ where(["#{PaperTrail.timestamp_field} > ?", timestamp]).order("#{PaperTrail.timestamp_field} ASC, #{self.primary_key} ASC")
21
21
  }
22
22
 
23
23
  # Restore the item from this version.
@@ -116,7 +116,8 @@ class Version < ActiveRecord::Base
116
116
  end
117
117
 
118
118
  def index
119
- sibling_versions.select(:id).order("id ASC").map(&:id).index(self.id)
119
+ id_column = self.class.primary_key.to_sym
120
+ sibling_versions.select(id_column).order("#{id_column} ASC").map(&id_column).index(self.send(id_column))
120
121
  end
121
122
 
122
123
  private
@@ -145,7 +146,7 @@ class Version < ActiveRecord::Base
145
146
  # but until PaperTrail knows which updates are "together" (e.g. parent and child being
146
147
  # updated on the same form), it's impossible to tell when the overall update started;
147
148
  # and therefore impossible to know when "just before" was.
148
- if (child_as_it_was = child.version_at(created_at - lookback.seconds))
149
+ if (child_as_it_was = child.version_at(send(PaperTrail.timestamp_field) - lookback.seconds))
149
150
  child_as_it_was.attributes.each do |k,v|
150
151
  model.send(assoc.name).send :write_attribute, k.to_sym, v rescue nil
151
152
  end
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.6.0'
2
+ VERSION = '2.6.1'
3
3
  end
@@ -20,5 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency 'rake'
21
21
  s.add_development_dependency 'shoulda', '2.10.3'
22
22
  s.add_development_dependency 'sqlite3-ruby', '~> 1.2'
23
- s.add_development_dependency 'capybara', '>= 0.4.0'
23
+ s.add_development_dependency 'capybara', '~> 1.0.0'
24
24
  end
@@ -1,4 +1,4 @@
1
1
  class Animal < ActiveRecord::Base
2
2
  has_paper_trail
3
- set_inheritance_column 'species'
3
+ self.inheritance_column = 'species'
4
4
  end
@@ -1,3 +1,3 @@
1
1
  class PostVersion < Version
2
- set_table_name :post_versions
2
+ self.table_name = 'post_versions'
3
3
  end
@@ -1,5 +1,5 @@
1
1
  # Define a bare test case to use with Capybara
2
2
  class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
3
- include Capybara
3
+ include Capybara::DSL
4
4
  include Rails.application.routes.url_helpers
5
5
  end
@@ -38,6 +38,7 @@ def change_schema
38
38
  ActiveRecord::Migration.verbose = false
39
39
  ActiveRecord::Schema.define do
40
40
  remove_column :widgets, :sacrificial_column
41
+ add_column :versions, :custom_created_at, :datetime
41
42
  end
42
43
  ActiveRecord::Migration.verbose = true
43
44
  end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class TimestampTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ PaperTrail.config.timestamp_field = :custom_created_at
7
+ change_schema
8
+ Version.reset_column_information
9
+
10
+ Fluxor.instance_eval <<-END
11
+ has_paper_trail
12
+ END
13
+
14
+ @fluxor = Fluxor.create :name => 'Some text.'
15
+ @fluxor.update_attributes :name => 'Some more text.'
16
+ @fluxor.update_attributes :name => 'Even more text.'
17
+ end
18
+
19
+ test 'versions works with custom timestamp field' do
20
+ # Normal behaviour
21
+ assert_equal 3, @fluxor.versions.length
22
+ assert_nil @fluxor.versions[0].reify
23
+ assert_equal 'Some text.', @fluxor.versions[1].reify.name
24
+ assert_equal 'Some more text.', @fluxor.versions[2].reify.name
25
+
26
+ # Tinker with custom timestamps.
27
+ now = Time.now.utc
28
+ @fluxor.versions.reverse.each_with_index do |version, index|
29
+ version.update_attribute :custom_created_at, (now + index.seconds)
30
+ end
31
+
32
+ # Test we are ordering by custom timestamps.
33
+ @fluxor.versions true # reload association
34
+ assert_nil @fluxor.versions[2].reify
35
+ assert_equal 'Some text.', @fluxor.versions[1].reify.name
36
+ assert_equal 'Some more text.', @fluxor.versions[0].reify.name
37
+ end
38
+
39
+ 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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 6
9
- - 0
10
- version: 2.6.0
9
+ - 1
10
+ version: 2.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andy Stewart
@@ -15,13 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-18 00:00:00 +01:00
18
+ date: 2012-03-12 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: rails
23
23
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ~>
@@ -31,11 +31,11 @@ dependencies:
31
31
  - 3
32
32
  version: "3"
33
33
  type: :runtime
34
- version_requirements: *id001
34
+ requirement: *id001
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake
37
37
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ">="
@@ -45,11 +45,11 @@ dependencies:
45
45
  - 0
46
46
  version: "0"
47
47
  type: :development
48
- version_requirements: *id002
48
+ requirement: *id002
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: shoulda
51
51
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - "="
@@ -61,11 +61,11 @@ dependencies:
61
61
  - 3
62
62
  version: 2.10.3
63
63
  type: :development
64
- version_requirements: *id003
64
+ requirement: *id003
65
65
  - !ruby/object:Gem::Dependency
66
66
  name: sqlite3-ruby
67
67
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
71
  - - ~>
@@ -76,23 +76,23 @@ dependencies:
76
76
  - 2
77
77
  version: "1.2"
78
78
  type: :development
79
- version_requirements: *id004
79
+ requirement: *id004
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: capybara
82
82
  prerelease: false
83
- requirement: &id005 !ruby/object:Gem::Requirement
83
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
- - - ">="
86
+ - - ~>
87
87
  - !ruby/object:Gem::Version
88
- hash: 15
88
+ hash: 23
89
89
  segments:
90
+ - 1
90
91
  - 0
91
- - 4
92
92
  - 0
93
- version: 0.4.0
93
+ version: 1.0.0
94
94
  type: :development
95
- version_requirements: *id005
95
+ requirement: *id005
96
96
  description: Track changes to your models' data. Good for auditing or versioning.
97
97
  email: boss@airbladesoftware.com
98
98
  executables: []
@@ -180,6 +180,7 @@ files:
180
180
  - test/test_helper.rb
181
181
  - test/unit/inheritance_column_test.rb
182
182
  - test/unit/model_test.rb
183
+ - test/unit/timestamp_test.rb
183
184
  has_rdoc: true
184
185
  homepage: http://github.com/airblade/paper_trail
185
186
  licenses: []
@@ -276,3 +277,4 @@ test_files:
276
277
  - test/test_helper.rb
277
278
  - test/unit/inheritance_column_test.rb
278
279
  - test/unit/model_test.rb
280
+ - test/unit/timestamp_test.rb