paper_trail 1.4.0 → 17.0.0

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.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/lib/generators/paper_trail/install/USAGE +31 -0
  3. data/lib/generators/paper_trail/install/install_generator.rb +101 -0
  4. data/lib/generators/paper_trail/install/templates/add_object_changes_to_versions.rb.erb +12 -0
  5. data/lib/generators/paper_trail/install/templates/create_versions.rb.erb +41 -0
  6. data/lib/generators/paper_trail/migration_generator.rb +65 -0
  7. data/lib/generators/paper_trail/update_item_subtype/USAGE +4 -0
  8. data/lib/generators/paper_trail/update_item_subtype/templates/update_versions_for_item_subtype.rb.erb +86 -0
  9. data/lib/generators/paper_trail/update_item_subtype/update_item_subtype_generator.rb +40 -0
  10. data/lib/paper_trail/attribute_serializers/README.md +10 -0
  11. data/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb +41 -0
  12. data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +51 -0
  13. data/lib/paper_trail/attribute_serializers/object_attribute.rb +48 -0
  14. data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +51 -0
  15. data/lib/paper_trail/cleaner.rb +60 -0
  16. data/lib/paper_trail/compatibility.rb +51 -0
  17. data/lib/paper_trail/config.rb +41 -0
  18. data/lib/paper_trail/errors.rb +33 -0
  19. data/lib/paper_trail/events/base.rb +343 -0
  20. data/lib/paper_trail/events/create.rb +32 -0
  21. data/lib/paper_trail/events/destroy.rb +42 -0
  22. data/lib/paper_trail/events/update.rb +76 -0
  23. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +16 -0
  24. data/lib/paper_trail/frameworks/active_record.rb +12 -0
  25. data/lib/paper_trail/frameworks/cucumber.rb +33 -0
  26. data/lib/paper_trail/frameworks/rails/controller.rb +103 -0
  27. data/lib/paper_trail/frameworks/rails/railtie.rb +34 -0
  28. data/lib/paper_trail/frameworks/rails.rb +3 -0
  29. data/lib/paper_trail/frameworks/rspec/helpers.rb +29 -0
  30. data/lib/paper_trail/frameworks/rspec.rb +42 -0
  31. data/lib/paper_trail/has_paper_trail.rb +79 -82
  32. data/lib/paper_trail/model_config.rb +257 -0
  33. data/lib/paper_trail/queries/versions/where_attribute_changes.rb +50 -0
  34. data/lib/paper_trail/queries/versions/where_object.rb +65 -0
  35. data/lib/paper_trail/queries/versions/where_object_changes.rb +70 -0
  36. data/lib/paper_trail/queries/versions/where_object_changes_from.rb +57 -0
  37. data/lib/paper_trail/queries/versions/where_object_changes_to.rb +57 -0
  38. data/lib/paper_trail/record_history.rb +51 -0
  39. data/lib/paper_trail/record_trail.rb +342 -0
  40. data/lib/paper_trail/reifier.rb +147 -0
  41. data/lib/paper_trail/request.rb +163 -0
  42. data/lib/paper_trail/serializers/json.rb +36 -0
  43. data/lib/paper_trail/serializers/yaml.rb +68 -0
  44. data/lib/paper_trail/type_serializers/postgres_array_serializer.rb +35 -0
  45. data/lib/paper_trail/version_concern.rb +406 -0
  46. data/lib/paper_trail/version_number.rb +23 -0
  47. data/lib/paper_trail.rb +128 -19
  48. metadata +444 -70
  49. data/.gitignore +0 -3
  50. data/README.md +0 -225
  51. data/Rakefile +0 -50
  52. data/VERSION +0 -1
  53. data/generators/paper_trail/USAGE +0 -2
  54. data/generators/paper_trail/paper_trail_generator.rb +0 -9
  55. data/generators/paper_trail/templates/create_versions.rb +0 -18
  56. data/init.rb +0 -1
  57. data/install.rb +0 -1
  58. data/lib/paper_trail/version.rb +0 -59
  59. data/paper_trail.gemspec +0 -67
  60. data/rails/init.rb +0 -1
  61. data/tasks/paper_trail_tasks.rake +0 -0
  62. data/test/database.yml +0 -18
  63. data/test/paper_trail_controller_test.rb +0 -70
  64. data/test/paper_trail_model_test.rb +0 -448
  65. data/test/paper_trail_schema_test.rb +0 -15
  66. data/test/schema.rb +0 -48
  67. data/test/schema_change.rb +0 -3
  68. data/test/test_helper.rb +0 -43
  69. data/uninstall.rb +0 -1
  70. /data/{MIT-LICENSE → LICENSE} +0 -0
data/README.md DELETED
@@ -1,225 +0,0 @@
1
- # PaperTrail
2
-
3
- PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed.
4
-
5
-
6
- ## Features
7
-
8
- * Stores every create, update and destroy.
9
- * Does not store updates which don't change anything.
10
- * Does not store updates which only change attributes you are ignoring.
11
- * Allows you to get at every version, including the original, even once destroyed.
12
- * Allows you to get at every version even if the schema has since changed.
13
- * Automatically records who was responsible if your controller has a `current_user` method.
14
- * Allows you to set who is responsible at model-level (useful for migrations).
15
- * Allows you to store arbitrary metadata with each version (useful for filtering versions).
16
- * Can be turned off/on (useful for migrations).
17
- * No configuration necessary.
18
- * Stores everything in a single database table (generates migration for you).
19
- * Thoroughly tested.
20
-
21
-
22
- ## Rails Version
23
-
24
- Known to work on Rails 2.3. Probably works on Rails 2.2 and 2.1.
25
-
26
-
27
- ## Basic Usage
28
-
29
- PaperTrail is simple to use. Just add 15 characters to a model to get a paper trail of every `create`, `update`, and `destroy`.
30
-
31
- class Widget < ActiveRecord::Base
32
- has_paper_trail
33
- end
34
-
35
- This gives you a `versions` method which returns the paper trail of changes to your model.
36
-
37
- >> widget = Widget.find 42
38
- >> widget.versions # [<Version>, <Version>, ...]
39
-
40
- Once you have a version, you can find out what happened:
41
-
42
- >> v = widget.versions.last
43
- >> v.event # 'update' (or 'create' or 'destroy')
44
- >> v.whodunnit # '153' (if the update was via a controller and
45
- # the controller has a current_user method,
46
- # here returning the id of the current user)
47
- >> v.created_at # when the update occurred
48
- >> widget = v.reify # the widget as it was before the update;
49
- # would be nil for a create event
50
-
51
- PaperTrail stores the pre-change version of the model, unlike some other auditing/versioning plugins, so you can retrieve the original version. This is useful when you start keeping a paper trail for models that already have records in the database.
52
-
53
- >> widget = Widget.find 153
54
- >> widget.name # 'Doobly'
55
-
56
- # Add has_paper_trail to Widget model.
57
-
58
- >> widget.versions # []
59
- >> widget.update_attributes :name => 'Wotsit'
60
- >> widget.versions.first.reify.name # 'Doobly'
61
- >> widget.versions.first.event # 'update'
62
-
63
- This also means that PaperTrail does not waste space storing a version of the object as it currently stands. The `versions` method gives you previous versions; to get the current one just call a finder on your `Widget` model as usual.
64
-
65
- Here's a helpful table showing what PaperTrail stores:
66
-
67
- <table>
68
- <tr>
69
- <th>Event</th>
70
- <th>Model Before</th>
71
- <th>Model After</th>
72
- </tr>
73
- <tr>
74
- <td>create</td>
75
- <td>nil</td>
76
- <td>widget</td>
77
- </tr>
78
- <tr>
79
- <td>update</td>
80
- <td>widget</td>
81
- <td>widget'</td>
82
- <tr>
83
- <td>destroy</td>
84
- <td>widget</td>
85
- <td>nil</td>
86
- </tr>
87
- </table>
88
-
89
- PaperTrail stores the values in the Model Before column. Most other auditing/versioning plugins store the After column.
90
-
91
-
92
- ## Ignoring changes to certain attributes
93
-
94
- You can ignore changes to certain attributes like this:
95
-
96
- class Article < ActiveRecord::Base
97
- has_paper_trail :ignore => [:title, :rating]
98
- end
99
-
100
- This means that changes to just the `title` or `rating` will not store another version of the article. It does not mean that the `title` and `rating` attributes will be ignored if some other change causes a new `Version` to be crated. For example:
101
-
102
- >> a = Article.create
103
- >> a.versions.length # 1
104
- >> a.update_attributes :title => 'My Title', :rating => 3
105
- >> a.versions.length # 1
106
- >> a.update_attributes :content => 'Hello'
107
- >> a.versions.length # 2
108
- >> a.versions.last.reify.title # 'My Title'
109
-
110
-
111
- ## Reverting And Undeleting A Model
112
-
113
- PaperTrail makes reverting to a previous version easy:
114
-
115
- >> widget = Widget.find 42
116
- >> widget.update_attributes :name => 'Blah blah'
117
- # Time passes....
118
- >> widget = widget.versions.last.reify # the widget as it was before the update
119
- >> widget.save # reverted
120
-
121
- Undeleting is just as simple:
122
-
123
- >> widget = Widget.find 42
124
- >> widget.destroy
125
- # Time passes....
126
- >> widget = Version.find(153).reify # the widget as it was before it was destroyed
127
- >> widget.save # the widget lives!
128
-
129
- In fact you could use PaperTrail to implement an undo system, though I haven't had the opportunity yet to do it myself.
130
-
131
-
132
- ## Finding Out Who Was Responsible For A Change
133
-
134
- If your `ApplicationController` has a `current_user` method, PaperTrail will store the value it returns in the `version`'s `whodunnit` column. Note that this column is a string so you will have to convert it to an integer if it's an id and you want to look up the user later on:
135
-
136
- >> last_change = Widget.versions.last
137
- >> user_who_made_the_change = User.find last_change.whodunnit.to_i
138
-
139
- In a migration or in `script/console` you can set who is responsible like this:
140
-
141
- >> PaperTrail.whodunnit = 'Andy Stewart'
142
- >> widget.update_attributes :name => 'Wibble'
143
- >> widget.versions.last.whodunnit # Andy Stewart
144
-
145
-
146
- ## Storing metadata
147
-
148
- You can store arbitrary metadata alongside each version like this:
149
-
150
- class Article < ActiveRecord::Base
151
- belongs_to :author
152
- has_paper_trail :meta => { :author_id => Proc.new { |article| article.author_id },
153
- :answer => 42 }
154
- end
155
-
156
- PaperTrail will call your proc with the current article and store the result in the `author_id` column of the `versions` table. (Remember to add your metadata columns to the table.)
157
-
158
- Why would you do this? In this example, `author_id` is an attribute of `Article` and PaperTrail will store it anyway in serialized (YAML) form in the `object` column of the `version` record. But let's say you wanted to pull out all versions for a particular author; without the metadata you would have to deserialize (reify) each `version` object to see if belonged to the author in question. Clearly this is inefficient. Using the metadata you can find just those versions you want:
159
-
160
- Version.all(:conditions => ['author_id = ?', author_id])
161
-
162
-
163
- ## Turning PaperTrail Off/On
164
-
165
- Sometimes you don't want to store changes. Perhaps you are only interested in changes made
166
- by your users and don't need to store changes you make yourself in, say, a migration.
167
-
168
- If you are about change some widgets and you don't want a paper trail of your changes, you can
169
- turn PaperTrail off like this:
170
-
171
- >> Widget.paper_trail_off
172
-
173
- And on again like this:
174
-
175
- >> Widget.paper_trail_on
176
-
177
-
178
- ## Installation
179
-
180
- 1. Install PaperTrail either as a gem (from Gemcutter; the ones on GitHub are obsolete) or as a plugin:
181
-
182
- `config.gem 'paper_trail', :source => 'http://gemcutter.org'`
183
-
184
- or:
185
-
186
- `script/plugin install git://github.com/airblade/paper_trail.git`
187
-
188
- 2. Generate a migration which will add a `versions` table to your database.
189
-
190
- `script/generate paper_trail`
191
-
192
- 3. Run the migration.
193
-
194
- `rake db:migrate`
195
-
196
- 4. Add `has_paper_trail` to the models you want to track.
197
-
198
-
199
- ## Testing
200
-
201
- PaperTrail has a thorough suite of tests. Thanks to [Zachery Hostens](http://github.com/zacheryph) for making them able to run standalone, i.e. without needing PaperTrail to be sitting in a Rails app.
202
-
203
-
204
- ## Problems
205
-
206
- Please use GitHub's [issue tracker](http://github.com/airblade/paper_trail/issues).
207
-
208
-
209
- ## Contributors
210
-
211
- Many thanks to:
212
-
213
- * [Zachery Hostens](http://github.com/zacheryph)
214
-
215
-
216
- ## Inspirations
217
-
218
- * [Simply Versioned](http://github.com/github/simply_versioned)
219
- * [Acts As Audited](http://github.com/collectiveidea/acts_as_audited)
220
-
221
-
222
- ## Intellectual Property
223
-
224
- Copyright (c) 2009 Andy Stewart (boss@airbladesoftware.com).
225
- Released under the MIT licence.
data/Rakefile DELETED
@@ -1,50 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- begin
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gemspec|
8
- gemspec.name = 'paper_trail'
9
- gemspec.summary = "Track changes to your models' data. Good for auditing or versioning."
10
- gemspec.email = 'boss@airbladesoftware.com'
11
- gemspec.homepage = 'http://github.com/airblade/paper_trail'
12
- gemspec.authors = ['Andy Stewart']
13
- end
14
- Jeweler::GemcutterTasks.new
15
- rescue LoadError
16
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
- end
18
-
19
- desc 'Test the paper_trail plugin.'
20
- Rake::TestTask.new(:test) do |t|
21
- t.libs << 'lib'
22
- t.libs << 'test'
23
- t.pattern = 'test/**/*_test.rb'
24
- t.verbose = true
25
- end
26
-
27
- begin
28
- require 'rcov/rcovtask'
29
- Rcov::RcovTask.new do |test|
30
- test.libs << 'test'
31
- test.pattern = 'test/**/*_test.rb'
32
- test.verbose = true
33
- end
34
- rescue LoadError
35
- task :rcov do
36
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
- end
38
- end
39
-
40
- desc 'Generate documentation for the paper_trail plugin.'
41
- Rake::RDocTask.new(:rdoc) do |rdoc|
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = 'PaperTrail'
44
- rdoc.options << '--line-numbers' << '--inline-source'
45
- rdoc.rdoc_files.include('README')
46
- rdoc.rdoc_files.include('lib/**/*.rb')
47
- end
48
-
49
- desc 'Default: run unit tests.'
50
- task :default => :test
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.4.0
@@ -1,2 +0,0 @@
1
- Description:
2
- Generates (but does not run) a migration to add a versions table.
@@ -1,9 +0,0 @@
1
- class PaperTrailGenerator < Rails::Generator::Base
2
-
3
- def manifest
4
- record do |m|
5
- m.migration_template 'create_versions.rb', 'db/migrate', :migration_file_name => 'create_versions'
6
- end
7
- end
8
-
9
- end
@@ -1,18 +0,0 @@
1
- class CreateVersions < ActiveRecord::Migration
2
- def self.up
3
- create_table :versions do |t|
4
- t.string :item_type, :null => false
5
- t.integer :item_id, :null => false
6
- t.string :event, :null => false
7
- t.string :whodunnit
8
- t.text :object
9
- t.datetime :created_at
10
- end
11
- add_index :versions, [:item_type, :item_id]
12
- end
13
-
14
- def self.down
15
- remove_index :versions, [:item_type, :item_id]
16
- drop_table :versions
17
- end
18
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- # Include hook code here
data/install.rb DELETED
@@ -1 +0,0 @@
1
- # Install hook code here
@@ -1,59 +0,0 @@
1
- class Version < ActiveRecord::Base
2
- belongs_to :item, :polymorphic => true
3
- validates_presence_of :event
4
-
5
- def reify
6
- unless object.nil?
7
- # Attributes
8
-
9
- attrs = YAML::load object
10
-
11
- # Normally a polymorphic belongs_to relationship allows us
12
- # to get the object we belong to by calling, in this case,
13
- # +item+. However this returns nil if +item+ has been
14
- # destroyed, and we need to be able to retrieve destroyed
15
- # objects.
16
- #
17
- # In this situation we constantize the +item_type+ to get hold of
18
- # the class...except when the stored object's attributes
19
- # include a +type+ key. If this is the case, the object
20
- # we belong to is using single table inheritance and the
21
- # +item_type+ will be the base class, not the actual subclass.
22
- # If +type+ is present but empty, the class is the base class.
23
-
24
- if item
25
- model = item
26
- else
27
- class_name = attrs['type'].blank? ? item_type : attrs['type']
28
- klass = class_name.constantize
29
- model = klass.new
30
- end
31
-
32
- attrs.each do |k, v|
33
- begin
34
- model.send "#{k}=", v
35
- rescue NoMethodError
36
- logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
37
- end
38
- end
39
-
40
- model
41
- end
42
- end
43
-
44
- def next
45
- Version.first :conditions => ["id > ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
46
- :order => 'id ASC'
47
- end
48
-
49
- def previous
50
- Version.first :conditions => ["id < ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
51
- :order => 'id DESC'
52
- end
53
-
54
- def index
55
- Version.all(:conditions => ["item_type = ? AND item_id = ?", item_type, item_id],
56
- :order => 'id ASC').index(self)
57
- end
58
-
59
- end
data/paper_trail.gemspec DELETED
@@ -1,67 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{paper_trail}
8
- s.version = "1.4.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Andy Stewart"]
12
- s.date = %q{2010-01-06}
13
- s.email = %q{boss@airbladesoftware.com}
14
- s.extra_rdoc_files = [
15
- "README.md"
16
- ]
17
- s.files = [
18
- ".gitignore",
19
- "MIT-LICENSE",
20
- "README.md",
21
- "Rakefile",
22
- "VERSION",
23
- "generators/paper_trail/USAGE",
24
- "generators/paper_trail/paper_trail_generator.rb",
25
- "generators/paper_trail/templates/create_versions.rb",
26
- "init.rb",
27
- "install.rb",
28
- "lib/paper_trail.rb",
29
- "lib/paper_trail/has_paper_trail.rb",
30
- "lib/paper_trail/version.rb",
31
- "paper_trail.gemspec",
32
- "rails/init.rb",
33
- "tasks/paper_trail_tasks.rake",
34
- "test/database.yml",
35
- "test/paper_trail_controller_test.rb",
36
- "test/paper_trail_model_test.rb",
37
- "test/paper_trail_schema_test.rb",
38
- "test/schema.rb",
39
- "test/schema_change.rb",
40
- "test/test_helper.rb",
41
- "uninstall.rb"
42
- ]
43
- s.homepage = %q{http://github.com/airblade/paper_trail}
44
- s.rdoc_options = ["--charset=UTF-8"]
45
- s.require_paths = ["lib"]
46
- s.rubygems_version = %q{1.3.5}
47
- s.summary = %q{Track changes to your models' data. Good for auditing or versioning.}
48
- s.test_files = [
49
- "test/paper_trail_controller_test.rb",
50
- "test/paper_trail_model_test.rb",
51
- "test/paper_trail_schema_test.rb",
52
- "test/schema.rb",
53
- "test/schema_change.rb",
54
- "test/test_helper.rb"
55
- ]
56
-
57
- if s.respond_to? :specification_version then
58
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
- s.specification_version = 3
60
-
61
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
- else
63
- end
64
- else
65
- end
66
- end
67
-
data/rails/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'paper_trail'
File without changes
data/test/database.yml DELETED
@@ -1,18 +0,0 @@
1
- sqlite3:
2
- adapter: sqlite3
3
- database: ":memory:"
4
-
5
- postgresql:
6
- adapter: postgresql
7
- username: postgres
8
- password: postgres
9
- database: paper_trail_plugin_test
10
- min_messages: ERROR
11
-
12
- mysql:
13
- adapter: mysql
14
- host: localhost
15
- username: andy
16
- password:
17
- database: paper_trail_plugin_test
18
- socket: /tmp/mysql.sock
@@ -1,70 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class ApplicationController < ActionController::Base
4
- def rescue_action(e)
5
- raise e
6
- end
7
-
8
- # Returns id of hypothetical current user
9
- def current_user
10
- 153
11
- end
12
- end
13
-
14
- class WidgetsController < ApplicationController
15
- def create
16
- @widget = Widget.create params[:widget]
17
- head :ok
18
- end
19
-
20
- def update
21
- @widget = Widget.find params[:id]
22
- @widget.update_attributes params[:widget]
23
- head :ok
24
- end
25
-
26
- def destroy
27
- @widget = Widget.find params[:id]
28
- @widget.destroy
29
- head :ok
30
- end
31
- end
32
-
33
-
34
- class PaperTrailControllerTest < ActionController::TestCase #Test::Unit::TestCase
35
- def setup
36
- @controller = WidgetsController.new
37
- @request = ActionController::TestRequest.new
38
- @response = ActionController::TestResponse.new
39
-
40
- ActionController::Routing::Routes.draw do |map|
41
- map.resources :widgets
42
- end
43
- end
44
-
45
- test 'create' do
46
- post :create, :widget => { :name => 'Flugel' }
47
- widget = assigns(:widget)
48
- assert_equal 1, widget.versions.length
49
- assert_equal 153, widget.versions.last.whodunnit.to_i
50
- end
51
-
52
- test 'update' do
53
- w = Widget.create :name => 'Duvel'
54
- assert_equal 1, w.versions.length
55
- put :update, :id => w.id, :widget => { :name => 'Bugle' }
56
- widget = assigns(:widget)
57
- assert_equal 2, widget.versions.length
58
- assert_equal 153, widget.versions.last.whodunnit.to_i
59
- end
60
-
61
- test 'destroy' do
62
- w = Widget.create :name => 'Roundel'
63
- assert_equal 1, w.versions.length
64
- delete :destroy, :id => w.id
65
- widget = assigns(:widget)
66
- assert_equal 2, widget.versions.length
67
- assert_equal 153, widget.versions.last.whodunnit.to_i
68
- end
69
- end
70
-