htanata-acts_as_audited 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ acts_as_audited_plugin.sqlite3.db
2
+ test/debug.log
3
+ coverage/
4
+ pkg
@@ -0,0 +1,25 @@
1
+ acts_as_audited ChangeLog
2
+ -------------------------------------------------------------------------------
3
+ * 2009-01-27 - Store old and new values for updates, and store all attributes on destroy.
4
+ Refactored revisioning methods to work as expected
5
+ * 2008-10-10 - changed to make it work in development mode
6
+ * 2008-04-19 - refactored to make compatible with dirty tracking in edge rails
7
+ and to stop storing both old and new values in a single audit
8
+ * 2008-04-18 - Fix NoMethodError when trying to access the :previous revision
9
+ on a model that doesn't have previous revisions [Alex Soto]
10
+ * 2008-03-21 - added #changed_attributes to get access to the changes before a
11
+ save [Chris Parker]
12
+ * 2007-12-16 - Added #revision_at for retrieving a revision from a specific
13
+ time [Jacob Atzen]
14
+ * 2007-12-16 - Fix error when getting revision from audit with no changes
15
+ [Geoffrey Wiseman]
16
+ * 2007-12-16 - Remove dependency on acts_as_list
17
+ * 2007-06-17 - Added support getting previous revisions
18
+ * 2006-11-17 - Replaced use of singleton User.current_user with cache sweeper
19
+ implementation for auditing the user that made the change
20
+ * 2006-11-17 - added migration generator
21
+ * 2006-08-14 - incorporated changes from Michael Schuerig to write_attribute
22
+ that saves the new value after every change and not just the
23
+ first, and performs proper type-casting before doing comparisons
24
+ * 2006-08-14 - The "changes" are now saved as a serialized hash
25
+ * 2006-07-21 - initial version
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2008 Brandon Keepers - Collective Idea
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,70 @@
1
+ = acts_as_audited
2
+
3
+ acts_as_audited is an ActiveRecord extension that logs all changes to your models in an audits table.
4
+
5
+ The purpose of this fork is to store both the previous values and the changed value, making each audit record selfcontained.
6
+
7
+ == Installation
8
+
9
+ * acts_as_audited can be installed as a gem:
10
+
11
+ # config/environment.rb
12
+ config.gem 'acts_as_audited', :lib => false, :source => 'http://gemcutter.org'
13
+
14
+ or a plugin:
15
+
16
+ script/plugin install git://github.com/collectiveidea/acts_as_audited.git
17
+
18
+ * Generate the migration
19
+ script/generate audited_migration add_audits_table
20
+ rake db:migrate
21
+
22
+ == Usage
23
+
24
+ Declare <tt>acts_as_audited</tt> on your models:
25
+
26
+ class User < ActiveRecord::Base
27
+ acts_as_audited :except => [:password, :mistress]
28
+ end
29
+
30
+ Within a web request, will automatically record the user that made the change if your controller has a <tt>current_user</tt> method.
31
+
32
+ To record a user in the audits outside of a web request, you can use <tt>as_user</tt>:
33
+
34
+ Audit.as_user(user) do
35
+ # Perform changes on audited models
36
+ end
37
+
38
+ == Caveats
39
+
40
+ If your model declares +attr_accessible+ after +acts_as_audited+, you need to set +:protect+ to false. acts_as_audited uses +attr_protected+ internally to prevent malicious users from unassociating your audits, and Rails does not allow both +attr_protected+ and +attr_accessible+. It will default to false if +attr_accessible+ is called before +acts_as_audited+, but needs to be explicitly set if it is called after.
41
+
42
+ class User < ActiveRecord::Base
43
+ acts_as_audited :protect => false
44
+ attr_accessible :name
45
+ end
46
+
47
+ == Compatability
48
+
49
+ acts_as_audited works with Rails 2.1 or later.
50
+
51
+ == Getting Help
52
+
53
+ Join the mailing list for getting help or offering suggestions:
54
+ http://groups.google.com/group/acts_as_audited
55
+
56
+ == Contributing
57
+
58
+ Contributions are always welcome. Checkout the latest code on GitHub:
59
+ http://github.com/collectiveidea/acts_as_audited
60
+
61
+ Please include tests with your patches. There are a few gems required to run the tests:
62
+ $ gem install multi_rails
63
+ $ gem install thoughtbot-shoulda jnunemaker-matchy --source http://gems.github.com
64
+
65
+ Make sure the tests pass against all versions of Rails since 2.1:
66
+
67
+ $ rake test:multi_rails:all
68
+
69
+ Please report bugs or feature suggestions on GitHub:
70
+ http://github.com/collectiveidea/acts_as_audited/issues
@@ -0,0 +1,57 @@
1
+ require 'rake'
2
+ require 'load_multi_rails_rake_tasks'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run tests.'
7
+ task :default => :test
8
+
9
+ begin
10
+ require 'jeweler'
11
+ Jeweler::Tasks.new do |gem|
12
+ gem.name = "htanata-acts_as_audited"
13
+ gem.summary = %Q{ActiveRecord extension that logs all changes to your models in an audits table}
14
+ gem.email = "htanata@gmail.com"
15
+ gem.homepage = "http://github.com/htanata/acts_as_audited"
16
+ gem.authors = ["Brandon Keepers", "Hendy Tanata"]
17
+ gem.add_dependency 'activerecord', '>=2.1'
18
+ gem.add_development_dependency "thoughtbot-shoulda"
19
+ gem.add_development_dependency "jnunemaker-matchy"
20
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
21
+ end
22
+ # Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
+ end
26
+
27
+ desc 'Test the acts_as_audited plugin'
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'lib'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = true
32
+ end
33
+
34
+ task :test => :check_dependencies
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ test.verbose = true
42
+ test.rcov_opts = %w(--exclude test,/usr/lib/ruby,/Library/Ruby,$HOME/.gem --sort coverage)
43
+ end
44
+ rescue LoadError
45
+ task :rcov do
46
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
47
+ end
48
+ end
49
+
50
+ desc 'Generate documentation for the acts_as_audited plugin.'
51
+ Rake::RDocTask.new(:rdoc) do |rdoc|
52
+ rdoc.rdoc_dir = 'doc'
53
+ rdoc.title = 'acts_as_audited'
54
+ rdoc.options << '--line-numbers' << '--inline-source'
55
+ rdoc.rdoc_files.include('README')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.1
@@ -0,0 +1,73 @@
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{acts_as_audited}
8
+ s.version = "1.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brandon Keepers"]
12
+ s.date = %q{2010-04-03}
13
+ s.email = %q{brandon@opensoul.org}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "acts_as_audited.gemspec",
26
+ "generators/audited_migration/USAGE",
27
+ "generators/audited_migration/audited_migration_generator.rb",
28
+ "generators/audited_migration/templates/migration.rb",
29
+ "init.rb",
30
+ "lib/acts_as_audited.rb",
31
+ "lib/acts_as_audited/audit.rb",
32
+ "lib/acts_as_audited/audit_sweeper.rb",
33
+ "rails/init.rb",
34
+ "test/acts_as_audited_test.rb",
35
+ "test/audit_sweeper_test.rb",
36
+ "test/audit_test.rb",
37
+ "test/db/database.yml",
38
+ "test/db/schema.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/collectiveidea/acts_as_audited}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.6}
45
+ s.summary = %q{ActiveRecord extension that logs all changes to your models in an audits table}
46
+ s.test_files = [
47
+ "test/acts_as_audited_test.rb",
48
+ "test/audit_sweeper_test.rb",
49
+ "test/audit_test.rb",
50
+ "test/db/schema.rb",
51
+ "test/test_helper.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.1"])
60
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
61
+ s.add_development_dependency(%q<jnunemaker-matchy>, [">= 0"])
62
+ else
63
+ s.add_dependency(%q<activerecord>, [">= 2.1"])
64
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
65
+ s.add_dependency(%q<jnunemaker-matchy>, [">= 0"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<activerecord>, [">= 2.1"])
69
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
70
+ s.add_dependency(%q<jnunemaker-matchy>, [">= 0"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,7 @@
1
+ Description:
2
+ The audited migration generator creates a migration to add the audits table.
3
+
4
+ Example:
5
+ ./script/generate audited_migration add_audits_table
6
+
7
+ This will create a migration in db/migrate/. Run "rake db:migrate" to update your database.
@@ -0,0 +1,7 @@
1
+ class AuditedMigrationGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ class <%= class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :audits, :force => true do |t|
4
+ t.column :auditable_id, :integer
5
+ t.column :auditable_type, :string
6
+ t.column :user_id, :integer
7
+ t.column :user_type, :string
8
+ t.column :username, :string
9
+ t.column :action, :string
10
+ t.column :changes, :text
11
+ t.column :version, :integer, :default => 0
12
+ t.column :created_at, :datetime
13
+ end
14
+
15
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
16
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
17
+ add_index :audits, :created_at
18
+ end
19
+
20
+ def self.down
21
+ drop_table :audits
22
+ end
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -0,0 +1,242 @@
1
+ # Copyright (c) 2006 Brandon Keepers
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module CollectiveIdea #:nodoc:
23
+ module Acts #:nodoc:
24
+ # Specify this act if you want changes to your model to be saved in an
25
+ # audit table. This assumes there is an audits table ready.
26
+ #
27
+ # class User < ActiveRecord::Base
28
+ # acts_as_audited
29
+ # end
30
+ #
31
+ # See <tt>CollectiveIdea::Acts::Audited::ClassMethods#acts_as_audited</tt>
32
+ # for configuration options
33
+ module Audited #:nodoc:
34
+ CALLBACKS = [:audit_create, :audit_update, :audit_destroy]
35
+
36
+ def self.included(base) # :nodoc:
37
+ base.extend ClassMethods
38
+ end
39
+
40
+ module ClassMethods
41
+ # == Configuration options
42
+ #
43
+ #
44
+ # * +only+ - Only audit the given attributes
45
+ # * +except+ - Excludes fields from being saved in the audit log.
46
+ # By default, acts_as_audited will audit all but these fields:
47
+ #
48
+ # [self.primary_key, inheritance_column, 'lock_version', 'created_at', 'updated_at']
49
+ # You can add to those by passing one or an array of fields to skip.
50
+ #
51
+ # class User < ActiveRecord::Base
52
+ # acts_as_audited :except => :password
53
+ # end
54
+ # * +protect+ - If your model uses +attr_protected+, set this to false to prevent Rails from
55
+ # raising an error. If you declare +attr_accessibe+ before calling +acts_as_audited+, it
56
+ # will automatically default to false. You only need to explicitly set this if you are
57
+ # calling +attr_accessible+ after.
58
+ #
59
+ # class User < ActiveRecord::Base
60
+ # acts_as_audited :protect => false
61
+ # attr_accessible :name
62
+ # end
63
+ #
64
+ def acts_as_audited(options = {})
65
+ # don't allow multiple calls
66
+ return if self.included_modules.include?(CollectiveIdea::Acts::Audited::InstanceMethods)
67
+
68
+ options = {:protect => accessible_attributes.nil?}.merge(options)
69
+
70
+ class_inheritable_reader :non_audited_columns
71
+ class_inheritable_reader :auditing_enabled
72
+
73
+ if options[:only]
74
+ except = self.column_names - options[:only].flatten.map(&:to_s)
75
+ else
76
+ except = [self.primary_key, inheritance_column, 'lock_version',
77
+ 'created_at', 'updated_at', 'created_on', 'updated_on']
78
+ except |= Array(options[:except]).collect(&:to_s) if options[:except]
79
+ end
80
+ write_inheritable_attribute :non_audited_columns, except
81
+
82
+ has_many :audits, :as => :auditable, :order => "#{Audit.quoted_table_name}.version"
83
+ attr_protected :audit_ids if options[:protect]
84
+ Audit.audited_class_names << self.to_s
85
+
86
+ after_create :audit_create if !options[:on] || (options[:on] && options[:on].include?(:create))
87
+ before_update :audit_update if !options[:on] || (options[:on] && options[:on].include?(:update))
88
+ after_destroy :audit_destroy if !options[:on] || (options[:on] && options[:on].include?(:destroy))
89
+
90
+ attr_accessor :version
91
+
92
+ extend CollectiveIdea::Acts::Audited::SingletonMethods
93
+ include CollectiveIdea::Acts::Audited::InstanceMethods
94
+
95
+ write_inheritable_attribute :auditing_enabled, true
96
+ end
97
+ end
98
+
99
+ module InstanceMethods
100
+
101
+ # Temporarily turns off auditing while saving.
102
+ def save_without_auditing
103
+ without_auditing { save }
104
+ end
105
+
106
+ # Executes the block with the auditing callbacks disabled.
107
+ #
108
+ # @foo.without_auditing do
109
+ # @foo.save
110
+ # end
111
+ #
112
+ def without_auditing(&block)
113
+ self.class.without_auditing(&block)
114
+ end
115
+
116
+ # Gets an array of the revisions available
117
+ #
118
+ # user.revisions.each do |revision|
119
+ # user.name
120
+ # user.version
121
+ # end
122
+ #
123
+ def revisions(from_version = 1)
124
+ audits = self.audits.find(:all, :conditions => ['version >= ?', from_version])
125
+ return [] if audits.empty?
126
+ revision = self.audits.find_by_version(from_version).revision
127
+ Audit.reconstruct_attributes(audits) {|attrs| revision.revision_with(attrs) }
128
+ end
129
+
130
+ # Get a specific revision specified by the version number, or +:previous+
131
+ def revision(version)
132
+ revision_with Audit.reconstruct_attributes(audits_to(version))
133
+ end
134
+
135
+ def revision_at(date_or_time)
136
+ audits = self.audits.find(:all, :conditions => ["created_at <= ?", date_or_time])
137
+ revision_with Audit.reconstruct_attributes(audits) unless audits.empty?
138
+ end
139
+
140
+ def audited_attributes
141
+ attributes.except(*non_audited_columns)
142
+ end
143
+
144
+ protected
145
+
146
+ def revision_with(attributes)
147
+ returning self.dup do |revision|
148
+ revision.send :instance_variable_set, '@attributes', self.attributes_before_type_cast
149
+ Audit.assign_revision_attributes(revision, attributes)
150
+
151
+ # Remove any association proxies so that they will be recreated
152
+ # and reference the correct object for this revision. The only way
153
+ # to determine if an instance variable is a proxy object is to
154
+ # see if it responds to certain methods, as it forwards almost
155
+ # everything to its target.
156
+ for ivar in revision.instance_variables
157
+ proxy = revision.instance_variable_get ivar
158
+ if !proxy.nil? and proxy.respond_to? :proxy_respond_to?
159
+ revision.instance_variable_set ivar, nil
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def audited_changes
168
+ changed_attributes.except(*non_audited_columns).inject({}) do |changes,(attr, old_value)|
169
+ changes[attr] = [old_value, self[attr]]
170
+ changes
171
+ end
172
+ end
173
+
174
+ def audits_to(version = nil)
175
+ if version == :previous
176
+ version = if self.version
177
+ self.version - 1
178
+ else
179
+ previous = audits.find(:first, :offset => 1,
180
+ :order => "#{Audit.quoted_table_name}.version DESC")
181
+ previous ? previous.version : 1
182
+ end
183
+ end
184
+ audits.find(:all, :conditions => ['version <= ?', version])
185
+ end
186
+
187
+ def audit_create
188
+ write_audit(:action => 'create', :changes => audited_attributes)
189
+ end
190
+
191
+ def audit_update
192
+ unless (changes = audited_changes).empty?
193
+ write_audit(:action => 'update', :changes => changes)
194
+ end
195
+ end
196
+
197
+ def audit_destroy
198
+ write_audit(:action => 'destroy', :changes => audited_attributes)
199
+ end
200
+
201
+ def write_audit(attrs)
202
+ self.audits.create attrs if auditing_enabled
203
+ end
204
+ end # InstanceMethods
205
+
206
+ module SingletonMethods
207
+ # Returns an array of columns that are audited. See non_audited_columns
208
+ def audited_columns
209
+ self.columns.select { |c| !non_audited_columns.include?(c.name) }
210
+ end
211
+
212
+ # Executes the block with auditing disabled.
213
+ #
214
+ # Foo.without_auditing do
215
+ # @foo.save
216
+ # end
217
+ #
218
+ def without_auditing(&block)
219
+ auditing_was_enabled = auditing_enabled
220
+ disable_auditing
221
+ returning(block.call) { enable_auditing if auditing_was_enabled }
222
+ end
223
+
224
+ def disable_auditing
225
+ write_inheritable_attribute :auditing_enabled, false
226
+ end
227
+
228
+ def enable_auditing
229
+ write_inheritable_attribute :auditing_enabled, true
230
+ end
231
+
232
+ # All audit operations during the block are recorded as being
233
+ # made by +user+. This is not model specific, the method is a
234
+ # convenience wrapper around #Audit.as_user.
235
+ def audit_as( user, &block )
236
+ Audit.as_user( user, &block )
237
+ end
238
+
239
+ end
240
+ end
241
+ end
242
+ end