paper_trail 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- paper_trail (2.0.0)
4
+ paper_trail (2.0.1)
5
5
  rails (~> 3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -7,7 +7,7 @@ PaperTrail lets you track changes to your models' data. It's good for auditing
7
7
 
8
8
  * Stores every create, update and destroy.
9
9
  * Does not store updates which don't change anything.
10
- * Does not store updates which only change attributes you are ignoring.
10
+ * Allows you to specify attributes (by inclusion or exclusion) which must change for a Version to be stored.
11
11
  * Allows you to get at every version, including the original, even once destroyed.
12
12
  * Allows you to get at every version even if the schema has since changed.
13
13
  * Allows you to get at the version as of a particular time.
@@ -26,7 +26,7 @@ PaperTrail lets you track changes to your models' data. It's good for auditing
26
26
 
27
27
  ## Rails Version
28
28
 
29
- Works on Rails 3 and Rails 2.3. The Rails 3 code is on the master branch and tagged v2.x. The Rails 2.3 code is on the Rails 2 branch and tagged 1.x.
29
+ Works on Rails 3 and Rails 2.3. The Rails 3 code is on the `master` branch and tagged `v2.x`. The Rails 2.3 code is on the `rails2` branch and tagged `v1.x`.
30
30
 
31
31
 
32
32
  ## API Summary
@@ -164,7 +164,7 @@ Here's a helpful table showing what PaperTrail stores:
164
164
  PaperTrail stores the values in the Model Before column. Most other auditing/versioning plugins store the After column.
165
165
 
166
166
 
167
- ## Ignoring changes to certain attributes
167
+ ## Choosing Attributes To Monitor
168
168
 
169
169
  You can ignore changes to certain attributes like this:
170
170
 
@@ -182,6 +182,23 @@ This means that changes to just the `title` or `rating` will not store another v
182
182
  >> a.versions.length # 2
183
183
  >> a.versions.last.reify.title # 'My Title'
184
184
 
185
+ Or, you can specify a list of all attributes you care about:
186
+
187
+ class Article < ActiveRecord::Base
188
+ has_paper_trail :only => [:title]
189
+ end
190
+
191
+ This means that only changes to the `title` will save a version of the article:
192
+
193
+ >> a = Article.create
194
+ >> a.versions.length # 1
195
+ >> a.update_attributes :title => 'My Title'
196
+ >> a.versions.length # 2
197
+ >> a.update_attributes :content => 'Hello'
198
+ >> a.versions.length # 2
199
+
200
+ Passing both `:ignore` and `:only` options will result in the article being saved if a changed attribute is included in `:only` but not in `:ignore`.
201
+
185
202
 
186
203
  ## Reverting And Undeleting A Model
187
204
 
@@ -494,29 +511,17 @@ Over time your `versions` table will grow to an unwieldy size. Because each ver
494
511
 
495
512
  2. Generate a migration which will add a `versions` table to your database.
496
513
 
497
- `rails generate paper_trail`
514
+ `bundle exec rails generate paper_trail`
498
515
 
499
516
  3. Run the migration.
500
517
 
501
- `rake db:migrate`
518
+ `bundle exec rake db:migrate`
502
519
 
503
520
  4. Add `has_paper_trail` to the models you want to track.
504
521
 
505
522
  ### Rails 2
506
523
 
507
- 1. Install PaperTrail as a gem via your `config/environment.rb`:
508
-
509
- `config.gem 'paper_trail', :version => '~> 1'`
510
-
511
- 2. Generate a migration which will add a `versions` table to your database.
512
-
513
- `script/generate paper_trail`
514
-
515
- 3. Run the migration.
516
-
517
- `rake db:migrate`
518
-
519
- 4. Add `has_paper_trail` to the models you want to track.
524
+ Please see the `rails2` branch.
520
525
 
521
526
 
522
527
  ## Testing
@@ -547,7 +552,8 @@ Many thanks to:
547
552
  * Danny Trelogan
548
553
  * [Mikl Kurkov](http://github.com/mkurkov)
549
554
  * [Franco Catena](https://github.com/francocatena)
550
- * [Emmanuel](https://github.com/emmanuel)
555
+ * [Emmanuel Gomez](https://github.com/emmanuel)
556
+ * [Matthew MacLeod](https://github.com/mattmacleod)
551
557
 
552
558
 
553
559
  ## Inspirations
@@ -558,5 +564,5 @@ Many thanks to:
558
564
 
559
565
  ## Intellectual Property
560
566
 
561
- Copyright (c) 2009 Andy Stewart (boss@airbladesoftware.com).
567
+ Copyright (c) 2011 Andy Stewart (boss@airbladesoftware.com).
562
568
  Released under the MIT licence.
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ module PaperTrail
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ extend ActiveRecord::Generators::Migration
9
+
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ desc 'Generates (but does not run) a migration to add a versions table.'
13
+
14
+ def create_migration_file
15
+ migration_template 'create_versions.rb', 'db/migrate/create_versions.rb'
16
+ end
17
+ end
18
+ end
@@ -12,6 +12,7 @@ module PaperTrail
12
12
  #
13
13
  # Options:
14
14
  # :ignore an array of attributes for which a new `Version` will not be created if only they change.
15
+ # :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
15
16
  # :meta a hash of extra data to store. You must add a column to the `versions` table for each key.
16
17
  # Values are objects or procs (which are called with `self`, i.e. the model with the paper
17
18
  # trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from
@@ -25,8 +26,11 @@ module PaperTrail
25
26
  attr_accessor :version
26
27
 
27
28
  cattr_accessor :ignore
28
- self.ignore = (options[:ignore] || []).map &:to_s
29
+ self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
29
30
 
31
+ cattr_accessor :only
32
+ self.only = ([options[:only]].flatten.compact || []).map &:to_s
33
+
30
34
  cattr_accessor :meta
31
35
  self.meta = options[:meta] || {}
32
36
 
@@ -68,11 +72,11 @@ module PaperTrail
68
72
  end
69
73
 
70
74
  # Returns the object (not a Version) as it was at the given timestamp.
71
- def version_at(timestamp)
75
+ def version_at(timestamp, reify_options={})
72
76
  # Because a version stores how its object looked *before* the change,
73
77
  # we need to look for the first version created *after* the timestamp.
74
78
  version = versions.after(timestamp).first
75
- version ? version.reify : self
79
+ version ? version.reify(reify_options) : self
76
80
  end
77
81
 
78
82
  # Returns the object (not a Version) as it was most recently.
@@ -147,6 +151,10 @@ module PaperTrail
147
151
  end
148
152
 
149
153
  def notably_changed
154
+ self.class.only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & self.class.only)
155
+ end
156
+
157
+ def changed_and_not_ignored
150
158
  changed - self.class.ignore
151
159
  end
152
160
 
@@ -99,7 +99,7 @@ class Version < ActiveRecord::Base
99
99
  end
100
100
 
101
101
  def index
102
- sibling_versions.all(:select => :id, :order => "id ASC").map(&:id).index(self.id)
102
+ sibling_versions.select(:id).order("id ASC").map(&:id).index(self.id)
103
103
  end
104
104
 
105
105
  private
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -1,5 +1,6 @@
1
1
  class Article < ActiveRecord::Base
2
- has_paper_trail :ignore => [:title],
2
+ has_paper_trail :ignore => :title,
3
+ :only => [:content],
3
4
  :meta => {:answer => 42,
4
5
  :action => :action_data_provider_method,
5
6
  :question => Proc.new { "31 + 11 = #{31 + 11}" },
@@ -49,6 +49,7 @@ class SetUpTestTables < ActiveRecord::Migration
49
49
  create_table :articles, :force => true do |t|
50
50
  t.string :title
51
51
  t.string :content
52
+ t.string :abstract
52
53
  end
53
54
 
54
55
  create_table :books, :force => true do |t|
@@ -15,6 +15,7 @@ ActiveRecord::Schema.define(:version => 20110208155312) do
15
15
  create_table "articles", :force => true do |t|
16
16
  t.string "title"
17
17
  t.string "content"
18
+ t.string "abstract"
18
19
  end
19
20
 
20
21
  create_table "authorships", :force => true do |t|
Binary file
@@ -10,10 +10,21 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
10
10
  should_not_change('the number of versions') { Version.count }
11
11
  end
12
12
 
13
- context 'which updates an ignored column and a non-ignored column' do
13
+ context 'which updates an ignored column and a selected column' do
14
14
  setup { @article.update_attributes :title => 'My first title', :content => 'Some text here.' }
15
15
  should_change('the number of versions', :by => 1) { Version.count }
16
16
  end
17
+
18
+ context 'which updates a selected column' do
19
+ setup { @article.update_attributes :content => 'Some text here.' }
20
+ should_change('the number of versions', :by => 1) { Version.count }
21
+ end
22
+
23
+ context 'which updates a non-ignored and non-selected column' do
24
+ setup { @article.update_attributes :abstract => 'Other abstract'}
25
+ should_not_change('the number of versions') { Version.count }
26
+ end
27
+
17
28
  end
18
29
 
19
30
 
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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 0
10
- version: 2.0.0
9
+ - 1
10
+ version: 2.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andy Stewart
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-08 00:00:00 +00:00
18
+ date: 2011-02-25 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -95,7 +95,7 @@ files:
95
95
  - README.md
96
96
  - Rakefile
97
97
  - lib/generators/paper_trail/USAGE
98
- - lib/generators/paper_trail/paper_trail_generator.rb
98
+ - lib/generators/paper_trail/install_generator.rb
99
99
  - lib/generators/paper_trail/templates/create_versions.rb
100
100
  - lib/paper_trail.rb
101
101
  - lib/paper_trail/config.rb
@@ -1,24 +0,0 @@
1
- require 'rails/generators/named_base'
2
- require 'rails/generators/migration'
3
-
4
- class PaperTrailGenerator < Rails::Generators::NamedBase
5
- include Rails::Generators::Migration
6
-
7
- desc "Generates (but does not run) a migration to add a versions table."
8
- source_root File.expand_path('../templates', __FILE__)
9
- argument :name, :type => :string, :default => "create_versions"
10
-
11
- # Implement the required interface for Rails::Generators::Migration.
12
- # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
13
- def self.next_migration_number(dirname)
14
- if ActiveRecord::Base.timestamped_migrations
15
- Time.now.utc.strftime("%Y%m%d%H%M%S")
16
- else
17
- "%.3d" % (current_migration_number(dirname) + 1)
18
- end
19
- end
20
-
21
- def create_migration_file
22
- migration_template 'create_versions.rb', 'db/migrate/create_versions.rb'
23
- end
24
- end