notifiably_audited 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +17 -0
  3. data/Appraisals +11 -0
  4. data/CHANGELOG +34 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +234 -0
  8. data/Rakefile +24 -0
  9. data/audited-activerecord.gemspec +21 -0
  10. data/audited-mongo_mapper.gemspec +21 -0
  11. data/audited.gemspec +27 -0
  12. data/gemfiles/rails30.gemfile +7 -0
  13. data/gemfiles/rails31.gemfile +7 -0
  14. data/gemfiles/rails32.gemfile +7 -0
  15. data/lib/audited/audit.rb +115 -0
  16. data/lib/audited/auditor.rb +449 -0
  17. data/lib/audited/rspec_matchers.rb +173 -0
  18. data/lib/audited/sweeper.rb +51 -0
  19. data/lib/audited.rb +15 -0
  20. data/lib/notifiably_audited/version.rb +3 -0
  21. data/lib/notifiably_audited.rb +15 -0
  22. data/notifiably_audited.gemspec +23 -0
  23. data/spec/audited_spec_helpers.rb +31 -0
  24. data/spec/rails_app/config/application.rb +5 -0
  25. data/spec/rails_app/config/database.yml +24 -0
  26. data/spec/rails_app/config/environment.rb +5 -0
  27. data/spec/rails_app/config/environments/development.rb +19 -0
  28. data/spec/rails_app/config/environments/production.rb +33 -0
  29. data/spec/rails_app/config/environments/test.rb +33 -0
  30. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  31. data/spec/rails_app/config/initializers/inflections.rb +2 -0
  32. data/spec/rails_app/config/initializers/secret_token.rb +2 -0
  33. data/spec/rails_app/config/routes.rb +6 -0
  34. data/spec/spec_helper.rb +23 -0
  35. data/spec/support/active_record/models.rb +84 -0
  36. data/spec/support/active_record/schema.rb +54 -0
  37. data/spec/support/mongo_mapper/connection.rb +4 -0
  38. data/spec/support/mongo_mapper/models.rb +210 -0
  39. data/test/db/version_1.rb +17 -0
  40. data/test/db/version_2.rb +18 -0
  41. data/test/db/version_3.rb +19 -0
  42. data/test/db/version_4.rb +20 -0
  43. data/test/db/version_5.rb +18 -0
  44. data/test/install_generator_test.rb +17 -0
  45. data/test/test_helper.rb +19 -0
  46. data/test/upgrade_generator_test.rb +65 -0
  47. metadata +159 -31
  48. data/lib/audited/adapters/active_record/audit.rb +0 -69
  49. data/lib/audited/adapters/active_record.rb +0 -15
  50. data/lib/audited-activerecord.rb +0 -2
  51. data/lib/generators/audited/install_generator.rb +0 -28
  52. data/lib/generators/audited/templates/add_association_to_audits.rb +0 -11
  53. data/lib/generators/audited/templates/add_comment_to_audits.rb +0 -9
  54. data/lib/generators/audited/templates/add_remote_address_to_audits.rb +0 -10
  55. data/lib/generators/audited/templates/install.rb +0 -35
  56. data/lib/generators/audited/templates/rename_association_to_associated.rb +0 -23
  57. data/lib/generators/audited/templates/rename_changes_to_audited_changes.rb +0 -9
  58. data/lib/generators/audited/templates/rename_parent_to_association.rb +0 -11
  59. data/lib/generators/audited/upgrade_generator.rb +0 -63
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NWE2NzIwZTJkZjkwNWI4ZWVjNzZiZjRlNGE2ZWVmNjVjZWJhODk3Mg==
4
+ ZDJmOWNmZjdhNzFhNzJjYzUwNGEyMzBkMjgxNzMxODg2Nzk1ODUwZg==
5
5
  data.tar.gz: !binary |-
6
- YzYzODE2Mzg1ZWM1YjVkZjJjODc5OTE4MzgzYzdmMzU1NTE5ODk3OA==
6
+ ODNlMzk1N2Q4YTI2NWU2NjQ4NGIxZDUwYjllNGQ2Y2JjMTI4MjA2Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzVkNzBlZmU5MzU0MGNiMTY5MWEyZjNmNTM5ZTY5YTUzMWE4NTg2MWYxNDE5
10
- ZGRiYjAwZGIwODk5YWE5Y2NlYWU0Y2Y5ZjFlNWM3NjU2NjFiNjgzZGRkMDAw
11
- NGVjYzY1N2RlNGFlNGYzOWE5NDU3ZWY4NGUzZGJkMjdmMzI3M2Q=
9
+ MjRiNWI0ZjIxYzI2ODVjYWI5MGQyNTVjYmNhNTk3YTEzMmM4NGIzNTA4Nzgy
10
+ NGEyNTgwOGMxZmRhNzkwYzUxODRiNmQ2MWVjMGNjY2VhMWE4YTJiMmMzZTM3
11
+ MDUyYTA1ZDUzNWFlZWRiYmMxZjYzZGFmY2U0OTRkOTVjYzUxMDE=
12
12
  data.tar.gz: !binary |-
13
- OTMyZTdkNTgzZTBjNDIwYzE4Yjk0NTMzYzY3ZWM4YTkzYWJmZDNmZGZiMjcx
14
- ZmMxN2MwNDVlNWIzZmRiYzNiNjRlYzdjOTM5YWQ1YzBlYTg5ZDUyMTM5YTAx
15
- YWZmOTg4ZWZlNDI5NjEyNGUyMjA3ZDQzYmNhOWQzZDY2MTcyZmQ=
13
+ MTA4MDUzOGYwM2YxZWM1ZDM5MjViMjFhMWY3NzYwNDkwMTAzYzQyMGE5NWJk
14
+ NTE2YTBjNmJkMDllY2NlMzM4ZTIwMDU0YzIwMzdkMGI2Y2Q2NGUyZTk1MDA5
15
+ M2UwMmJiOGQ1MTdlNDNhZWE5YjI2Mjg3M2Q0NzYyZjYwMjI4ZDg=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise 'rails30' do
2
+ gem 'rails', '~> 3.0.0'
3
+ end
4
+
5
+ appraise 'rails31' do
6
+ gem 'rails', '~> 3.1.0'
7
+ end
8
+
9
+ appraise 'rails32' do
10
+ gem 'rails', '~> 3.2.0'
11
+ end
data/CHANGELOG ADDED
@@ -0,0 +1,34 @@
1
+ Audited ChangeLog
2
+ -------------------------------------------------------------------------------
3
+ * 2012-04-10 - Add Audit scopes for creates, updates and destroys [chriswfx]
4
+ * 2011-10-25 - Made ignored_attributes configurable [senny]
5
+ * 2011-09-09 - Rails 3.x support
6
+ Support for associated audits
7
+ Support for remote IP address storage
8
+ Plenty of bug fixes and refactoring
9
+ [kennethkalmer, ineu, PatrickMa, jrozner, dwarburton, bsiggelkow, dgm]
10
+ * 2009-01-27 - Store old and new values for updates, and store all attributes on destroy.
11
+ Refactored revisioning methods to work as expected
12
+ * 2008-10-10 - changed to make it work in development mode
13
+ * 2008-09-24 - Add ability to record parent record of the record being audited
14
+ [Kenneth Kalmer]
15
+ * 2008-04-19 - refactored to make compatible with dirty tracking in edge rails
16
+ and to stop storing both old and new values in a single audit
17
+ * 2008-04-18 - Fix NoMethodError when trying to access the :previous revision
18
+ on a model that doesn't have previous revisions [Alex Soto]
19
+ * 2008-03-21 - added #changed_attributes to get access to the changes before a
20
+ save [Chris Parker]
21
+ * 2007-12-16 - Added #revision_at for retrieving a revision from a specific
22
+ time [Jacob Atzen]
23
+ * 2007-12-16 - Fix error when getting revision from audit with no changes
24
+ [Geoffrey Wiseman]
25
+ * 2007-12-16 - Remove dependency on acts_as_list
26
+ * 2007-06-17 - Added support getting previous revisions
27
+ * 2006-11-17 - Replaced use of singleton User.current_user with cache sweeper
28
+ implementation for auditing the user that made the change
29
+ * 2006-11-17 - added migration generator
30
+ * 2006-08-14 - incorporated changes from Michael Schuerig to write_attribute
31
+ that saves the new value after every change and not just the
32
+ first, and performs proper type-casting before doing comparisons
33
+ * 2006-08-14 - The "changes" are now saved as a serialized hash
34
+ * 2006-07-21 - initial version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :name => 'audited'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,234 @@
1
+ Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.png)](http://travis-ci.org/collectiveidea/audited) [![Dependency Status](https://gemnasium.com/collectiveidea/audited.png)](https://gemnasium.com/collectiveidea/audited)
2
+ =======
3
+
4
+ **Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited also allows you to record who made those changes, save comments and associate models related to the changes. Audited works with Rails 3.
5
+
6
+ ## Supported Rubies
7
+
8
+ Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions:
9
+
10
+ * 1.8.7
11
+ * 1.9.2
12
+ * 1.9.3
13
+ * Head
14
+
15
+ Audited may work just fine with a Ruby version not listed above, but we can't guarantee that it will. If you'd like to maintain a Ruby that isn't listed, please let us know with a [pull request](https://github.com/collectiveidea/audited/pulls).
16
+
17
+ ## Supported ORMs
18
+
19
+ In a previous life, Audited was ActiveRecord-only. Audited will now audit models for the following backends:
20
+
21
+ * ActiveRecord
22
+ * MongoMapper
23
+
24
+ ## Installation
25
+
26
+ The installation process depends on what ORM your app is using.
27
+
28
+ ### ActiveRecord
29
+
30
+ Add the appropriate gem to your Gemfile:
31
+
32
+ ```ruby
33
+ gem "audited-activerecord", "~> 3.0"
34
+ ```
35
+
36
+ Then, from your Rails app directory, create the `audits` table:
37
+
38
+ ```bash
39
+ $ rails generate audited:install
40
+ $ rake db:migrate
41
+ ```
42
+
43
+ #### Upgrading
44
+
45
+ If you're already using Audited (or acts_as_audited), your `audits` table may require additional columns. After every upgrade, please run:
46
+
47
+ ```bash
48
+ $ rails generate audited:upgrade
49
+ $ rake db:migrate
50
+ ```
51
+
52
+ Upgrading will only make changes if changes are needed.
53
+
54
+ ### MongoMapper
55
+
56
+ ```ruby
57
+ gem "audited-mongo_mapper", "~> 3.0"
58
+ ```
59
+
60
+ ## Usage
61
+
62
+ Simply call `audited` on your models:
63
+
64
+ ```ruby
65
+ class User < ActiveRecord::Base
66
+ audited
67
+ end
68
+ ```
69
+
70
+ By default, whenever a user is created, updated or destroyed, a new audit is created.
71
+
72
+ ```ruby
73
+ user = User.create!(:name => "Steve")
74
+ user.audits.count # => 1
75
+ user.update_attributes!(:name => "Ryan")
76
+ user.audits.count # => 2
77
+ user.destroy
78
+ user.audits.count # => 3
79
+ ```
80
+
81
+ Audits contain information regarding what action was taken on the model and what changes were made.
82
+
83
+ ```ruby
84
+ user.update_attributes!(:name => "Ryan")
85
+ audit = user.audits.last
86
+ audit.action # => "update"
87
+ audit.audited_changes # => {"name"=>["Steve", "Ryan"]}
88
+ ```
89
+
90
+ ### Specifying columns
91
+
92
+ By default, a new audit is created for any attribute changes. You can, however, limit the columns to be considered.
93
+
94
+ ```ruby
95
+ class User < ActiveRecord::Base
96
+ # All fields
97
+ # audited
98
+
99
+ # Single field
100
+ # audited only: :name
101
+
102
+ # Multiple fields
103
+ # audited only: [:name, :address]
104
+
105
+ # All except certain fields
106
+ # audited except: :password
107
+ end
108
+ ```
109
+
110
+ ### Comments
111
+
112
+ You can attach comments to each audit using an `audit_comment` attribute on your model.
113
+
114
+ ```ruby
115
+ user.update_attributes!(:name => "Ryan", :audit_comment => "Changing name, just because")
116
+ user.audits.last.comment # => "Changing name, just because"
117
+ ```
118
+
119
+ You can optionally add the `:comment_required` option to your `audited` call to require comments for all audits.
120
+
121
+ ```ruby
122
+ class User < ActiveRecord::Base
123
+ audited :comment_required => true
124
+ end
125
+ ```
126
+
127
+ ### Current User Tracking
128
+
129
+ If you're using Audited in a Rails application, all audited changes made within a request will automatically be attributed to the current user. By default, Audited uses the `current_user` method in your controller.
130
+
131
+ ```
132
+ class PostsController < ApplicationController
133
+ def create
134
+ current_user # => #<User name: "Steve">
135
+ @post = Post.create(params[:post])
136
+ @post.audits.last.user # => #<User name: "Steve">
137
+ end
138
+ end
139
+ ```
140
+
141
+ To use a method other than `current_user`, put the following in an intializer:
142
+
143
+ ```ruby
144
+ Audited.current_user_method = :authenticated_user
145
+ ```
146
+
147
+ Outside of a request, Audited can still record the user with the `as_user` method:
148
+
149
+ ```ruby
150
+ Audit.as_user(User.find(1)) do
151
+ post.update_attribute!(:title => "Hello, world!")
152
+ end
153
+ post.audits.last.user # => #<User id: 1>
154
+ ```
155
+
156
+ ### Associated Audits
157
+
158
+ Sometimes it's useful to associate an audit with a model other than the one being changed. For instance, given the following models:
159
+
160
+ ```ruby
161
+ class User < ActiveRecord::Base
162
+ belongs_to :company
163
+ audited
164
+ end
165
+
166
+ class Company < ActiveRecord::Base
167
+ has_many :users
168
+ end
169
+ ```
170
+
171
+ Every change to a user is audited, but what if you want to grab all of the audits of users belonging to a particular company? You can add the `:associated_with` option to your `audited` call:
172
+
173
+ ```ruby
174
+ class User < ActiveRecord::Base
175
+ belongs_to :company
176
+ audited :associated_with => :company
177
+ end
178
+
179
+ class Company < ActiveRecord::Base
180
+ has_many :users
181
+ has_associated_audits
182
+ end
183
+ ```
184
+
185
+ Now, when a audit is created for a user, that user's company is also saved alongside the audit. This makes it much easier (and faster) to access audits indirectly related to a company.
186
+
187
+ ```ruby
188
+ company = Company.create!(:name => "Collective Idea")
189
+ user = company.users.create!(:name => "Steve")
190
+ user.update_attribute!(:name => "Steve Richert")
191
+ user.audits.last.associated # => #<Company name: "Collective Idea">
192
+ company.associated_audits.last.auditable # => #<User name: "Steve Richert">
193
+ ```
194
+
195
+ ## Gotchas
196
+
197
+ ### Accessible Attributes
198
+
199
+ Audited assumes you are using `attr_accessible`, however, if you are using `attr_protected` or just going at it unprotected you will have to set the `:allow_mass_assignment => true` option.
200
+
201
+ If using `attr_protected` be sure to add `audit_ids` to the list of protected attributes to prevent data loss.
202
+
203
+ ```ruby
204
+ class User < ActiveRecord::Base
205
+ audited :allow_mass_assignment => true
206
+ end
207
+ ```
208
+
209
+ ```ruby
210
+ class User < ActiveRecord::Base
211
+ audited :allow_mass_assignment => true
212
+ attr_protected :logins, :audit_ids
213
+ end
214
+ ```
215
+
216
+ ### MongoMapper Embedded Documents
217
+
218
+ Currently, Audited does not track changes on embedded documents. Audited works by tracking a model's [dirty changes](http://api.rubyonrails.org/classes/ActiveModel/Dirty.html) but changes to embedded documents don't appear in dirty tracking.
219
+
220
+ ## Support
221
+
222
+ You can find documentation at: http://rdoc.info/github/collectiveidea/audited
223
+
224
+ Or join the [mailing list](http://groups.google.com/group/audited) to get help or offer suggestions.
225
+
226
+ ## Contributing
227
+
228
+ In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project. Here are a few ways _you_ can pitch in:
229
+
230
+ * Use prerelease versions of Audited.
231
+ * [Report bugs](https://github.com/collectiveidea/audited/issues).
232
+ * Fix bugs and submit [pull requests](http://github.com/collectiveidea/audited/pulls).
233
+ * Write, clarify or fix documentation.
234
+ * Refactor code.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_helper'
4
+ require 'rspec/core/rake_task'
5
+ require 'appraisal'
6
+
7
+ Bundler::GemHelper.install_tasks(:name => 'audited')
8
+ Bundler::GemHelper.install_tasks(:name => 'audited-activerecord')
9
+ Bundler::GemHelper.install_tasks(:name => 'audited-mongo_mapper')
10
+
11
+ ADAPTERS = %w(active_record mongo_mapper)
12
+
13
+ ADAPTERS.each do |adapter|
14
+ desc "Run RSpec code examples for #{adapter} adapter"
15
+ RSpec::Core::RakeTask.new(adapter) do |t|
16
+ t.pattern = "spec/audited/adapters/#{adapter}/**/*_spec.rb"
17
+ end
18
+ end
19
+
20
+ RSpec::Core::RakeTask.new(:spec => ADAPTERS) do |t|
21
+ t.pattern = 'spec/audited/*_spec.rb'
22
+ end
23
+
24
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'notifiably_audited-activerecord'
5
+ gem.version = '0.0.6'
6
+
7
+ gem.authors = ['senthil kumar']
8
+ gem.email = 'senthilkumar.hce@gmail.com'
9
+ gem.description = 'Log all changes to your ActiveRecord models'
10
+ gem.summary = ''
11
+ gem.homepage = ''
12
+ gem.license = 'MIT'
13
+
14
+ gem.add_dependency 'notifiably_audited', gem.version
15
+ gem.add_dependency 'activerecord', '~> 3.0'
16
+
17
+ gem.files = `git ls-files lib`.split($\).grep(/(active_?record|generators)/)
18
+ gem.files << 'LICENSE'
19
+ gem.require_paths = ['lib']
20
+ end
21
+
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'audited-mongo_mapper'
5
+ gem.version = '3.0.0'
6
+
7
+ gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
8
+ gem.email = 'info@collectiveidea.com'
9
+ gem.description = 'Log all changes to your MongoMapper models'
10
+ gem.summary = gem.description
11
+ gem.homepage = 'https://github.com/collectiveidea/audited'
12
+ gem.license = 'MIT'
13
+
14
+ gem.add_dependency 'audited', gem.version
15
+ gem.add_dependency 'mongo_mapper', '~> 0.11'
16
+
17
+ gem.files = `git ls-files lib`.split($\).grep(/mongo_mapper/)
18
+ gem.files << 'LICENSE'
19
+ gem.require_paths = ['lib']
20
+ end
21
+
data/audited.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'notifiably_audited'
5
+ gem.version = '0.0.7'
6
+
7
+ gem.authors = ['senthil kumar']
8
+ gem.email = 'senthilkumar.hce@gmail.com'
9
+ gem.description = 'Log all changes to your ActiveRecord models'
10
+ gem.summary = ''
11
+ gem.homepage = ''
12
+ gem.license = 'MIT'
13
+
14
+ gem.add_development_dependency 'activerecord', '~> 3.0'
15
+ gem.add_development_dependency 'appraisal', '~> 0.4'
16
+ gem.add_development_dependency 'bson_ext', '~> 1.6'
17
+ gem.add_development_dependency 'mongo_mapper', '~> 0.11'
18
+ gem.add_development_dependency 'rails', '~> 3.0'
19
+ gem.add_development_dependency 'rspec-rails', '~> 2.0'
20
+ gem.add_development_dependency 'sqlite3', '~> 1.0'
21
+ gem.add_development_dependency 'private_pub'
22
+
23
+ gem.files = `git ls-files`.split($\).reject{|f| f =~ /(lib\/audited\-|adapters|generators)/ }
24
+ gem.test_files = gem.files.grep(/^spec\//)
25
+ gem.require_paths = ['lib']
26
+ end
27
+
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.0.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.1.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.0"
6
+
7
+ gemspec :name=>"audited", :path=>"../"
@@ -0,0 +1,115 @@
1
+ module Audited
2
+ module Audit
3
+ def self.included(klass)
4
+ klass.extend(ClassMethods)
5
+ klass.setup_audit
6
+ end
7
+
8
+ module ClassMethods
9
+ def setup_audit
10
+ belongs_to :auditable, :polymorphic => true
11
+ belongs_to :user, :polymorphic => true
12
+ belongs_to :associated, :polymorphic => true
13
+
14
+ before_create :set_version_number, :set_audit_user
15
+
16
+ after_save :notify
17
+
18
+ cattr_accessor :audited_class_names
19
+ self.audited_class_names = Set.new
20
+
21
+ attr_accessible :action, :audited_changes, :comment,
22
+ :associated, :receiver_id, :checked, :meta,:title
23
+ end
24
+
25
+ # Returns the list of classes that are being audited
26
+ def audited_classes
27
+ audited_class_names.map(&:constantize)
28
+ end
29
+
30
+ # All audits made during the block called will be recorded as made
31
+ # by +user+. This method is hopefully threadsafe, making it ideal
32
+ # for background operations that require audit information.
33
+ def as_user(user, &block)
34
+ Thread.current[:audited_user] = user
35
+ yield
36
+ ensure
37
+ Thread.current[:audited_user] = nil
38
+ end
39
+
40
+ # @private
41
+ def reconstruct_attributes(audits)
42
+ attributes = {}
43
+ result = audits.collect do |audit|
44
+ attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
45
+ yield attributes if block_given?
46
+ end
47
+ block_given? ? result : attributes
48
+ end
49
+
50
+ # @private
51
+ def assign_revision_attributes(record, attributes)
52
+ attributes.each do |attr, val|
53
+ record = record.dup if record.frozen?
54
+
55
+ if record.respond_to?("#{attr}=")
56
+ record.attributes.has_key?(attr.to_s) ?
57
+ record[attr] = val :
58
+ record.send("#{attr}=", val)
59
+ end
60
+ end
61
+ record
62
+ end
63
+ end
64
+
65
+ # Return an instance of what the object looked like at this revision. If
66
+ # the object has been destroyed, this will be a new record.
67
+ def revision
68
+ clazz = auditable_type.constantize
69
+ (clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
70
+ self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({ :version => version }))
71
+ end
72
+ end
73
+
74
+ # Returns a hash of the changed attributes with the new values
75
+ def new_attributes
76
+ (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
77
+ attrs[attr] = values.is_a?(Array) ? values.last : values
78
+ attrs
79
+ end
80
+ end
81
+
82
+ # Returns a hash of the changed attributes with the old values
83
+ def old_attributes
84
+ (audited_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
85
+ attrs[attr] = Array(values).first
86
+
87
+ attrs
88
+ end
89
+ end
90
+
91
+ private
92
+ def set_version_number
93
+ max = self.class.where(
94
+ :auditable_id => auditable_id,
95
+ :auditable_type => auditable_type
96
+ ).order(:version.desc).first.try(:version) || 0
97
+ self.version = max + 1
98
+ end
99
+
100
+ def set_audit_user
101
+ self.user = Thread.current[:audited_user] if Thread.current[:audited_user]
102
+ p self.user
103
+ nil # prevent stopping callback chains
104
+ end
105
+ #====== beck added for notifiable ====
106
+ def notify
107
+ PrivatePub.publish_to("/notifiably_audited/" + self.receiver_id.to_s,
108
+ title: self.title,
109
+ comment: self.comment,
110
+ id: self.id)
111
+ end
112
+ #========================================
113
+
114
+ end
115
+ end