notifiably_audited 0.0.5 → 0.0.6

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/lib/audited/adapters/active_record/audit.rb +69 -0
  3. data/lib/audited/adapters/active_record.rb +15 -0
  4. data/lib/audited-activerecord.rb +2 -0
  5. data/lib/generators/audited/install_generator.rb +28 -0
  6. data/lib/generators/audited/templates/add_association_to_audits.rb +11 -0
  7. data/lib/generators/audited/templates/add_comment_to_audits.rb +9 -0
  8. data/lib/generators/audited/templates/add_remote_address_to_audits.rb +10 -0
  9. data/lib/generators/audited/templates/install.rb +35 -0
  10. data/lib/generators/audited/templates/rename_association_to_associated.rb +23 -0
  11. data/lib/generators/audited/templates/rename_changes_to_audited_changes.rb +9 -0
  12. data/lib/generators/audited/templates/rename_parent_to_association.rb +11 -0
  13. data/lib/generators/audited/upgrade_generator.rb +63 -0
  14. metadata +24 -157
  15. data/.gitignore +0 -17
  16. data/Appraisals +0 -11
  17. data/CHANGELOG +0 -34
  18. data/Gemfile +0 -3
  19. data/LICENSE.txt +0 -22
  20. data/README.md +0 -234
  21. data/Rakefile +0 -24
  22. data/audited-activerecord.gemspec +0 -21
  23. data/audited-mongo_mapper.gemspec +0 -21
  24. data/audited.gemspec +0 -27
  25. data/gemfiles/rails30.gemfile +0 -7
  26. data/gemfiles/rails31.gemfile +0 -7
  27. data/gemfiles/rails32.gemfile +0 -7
  28. data/lib/audited/audit.rb +0 -115
  29. data/lib/audited/auditor.rb +0 -449
  30. data/lib/audited/rspec_matchers.rb +0 -173
  31. data/lib/audited/sweeper.rb +0 -51
  32. data/lib/audited.rb +0 -15
  33. data/lib/notifiably_audited/version.rb +0 -3
  34. data/lib/notifiably_audited.rb +0 -15
  35. data/notifiably_audited.gemspec +0 -23
  36. data/spec/audited_spec_helpers.rb +0 -31
  37. data/spec/rails_app/config/application.rb +0 -5
  38. data/spec/rails_app/config/database.yml +0 -24
  39. data/spec/rails_app/config/environment.rb +0 -5
  40. data/spec/rails_app/config/environments/development.rb +0 -19
  41. data/spec/rails_app/config/environments/production.rb +0 -33
  42. data/spec/rails_app/config/environments/test.rb +0 -33
  43. data/spec/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  44. data/spec/rails_app/config/initializers/inflections.rb +0 -2
  45. data/spec/rails_app/config/initializers/secret_token.rb +0 -2
  46. data/spec/rails_app/config/routes.rb +0 -6
  47. data/spec/spec_helper.rb +0 -23
  48. data/spec/support/active_record/models.rb +0 -84
  49. data/spec/support/active_record/schema.rb +0 -54
  50. data/spec/support/mongo_mapper/connection.rb +0 -4
  51. data/spec/support/mongo_mapper/models.rb +0 -210
  52. data/test/db/version_1.rb +0 -17
  53. data/test/db/version_2.rb +0 -18
  54. data/test/db/version_3.rb +0 -19
  55. data/test/db/version_4.rb +0 -20
  56. data/test/db/version_5.rb +0 -18
  57. data/test/install_generator_test.rb +0 -17
  58. data/test/test_helper.rb +0 -19
  59. data/test/upgrade_generator_test.rb +0 -65
data/README.md DELETED
@@ -1,234 +0,0 @@
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 DELETED
@@ -1,24 +0,0 @@
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
@@ -1,21 +0,0 @@
1
- # encoding: utf-8
2
-
3
- Gem::Specification.new do |gem|
4
- gem.name = 'audited-activerecord'
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 ActiveRecord 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 '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
-
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,27 +0,0 @@
1
- # encoding: utf-8
2
-
3
- Gem::Specification.new do |gem|
4
- gem.name = 'notifiably_audited'
5
- gem.version = '0.0.5'
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 models'
10
- gem.summary = gem.description
11
- gem.homepage = 'https://github.com/collectiveidea/audited'
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
-
@@ -1,7 +0,0 @@
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=>"../"
@@ -1,7 +0,0 @@
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=>"../"
@@ -1,7 +0,0 @@
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=>"../"
data/lib/audited/audit.rb DELETED
@@ -1,115 +0,0 @@
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