Permalink
Browse files

add acts_as_audited plugin

  • Loading branch information...
1 parent 7f5a322 commit 63e68693e493a6879a3f95e05ebcc0ff960a223e @timriley committed Jul 6, 2008
@@ -0,0 +1,23 @@
+class AddAuditsTable < ActiveRecord::Migration
+ def self.up
+ create_table :audits, :force => true do |t|
+ t.column :auditable_id, :integer
+ t.column :auditable_type, :string
+ t.column :user_id, :integer
+ t.column :user_type, :string
+ t.column :username, :string
+ t.column :action, :string
+ t.column :changes, :text
+ t.column :version, :integer, :default => 0
+ t.column :created_at, :datetime
+ end
+
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
+ add_index :audits, :created_at
+ end
+
+ def self.down
+ drop_table :audits
+ end
+end
View
@@ -9,7 +9,23 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 2) do
+ActiveRecord::Schema.define(:version => 20080706055700) do
+
+ create_table "audits", :force => true do |t|
+ t.integer "auditable_id", :limit => 11
+ t.string "auditable_type"
+ t.integer "user_id", :limit => 11
+ t.string "user_type"
+ t.string "username"
+ t.string "action"
+ t.text "changes"
+ t.integer "version", :limit => 11, :default => 0
+ t.datetime "created_at"
+ end
+
+ add_index "audits", ["auditable_id", "auditable_type"], :name => "auditable_index"
+ add_index "audits", ["user_id", "user_type"], :name => "user_index"
+ add_index "audits", ["created_at"], :name => "index_audits_on_created_at"
create_table "bj_config", :primary_key => "bj_config_id", :force => true do |t|
t.text "hostname"
@@ -0,0 +1,2 @@
+acts_as_audited_plugin.sqlite3.db
+test/debug.log
@@ -0,0 +1,22 @@
+ acts_as_audited ChangeLog
+-------------------------------------------------------------------------------
+* 2008-04-19 - refactored to make compatible with dirty tracking in edge rails
+ and to stop storing both old and new values in a single audit
+* 2008-04-18 - Fix NoMethodError when trying to access the :previous revision
+ on a model that doesn't have previous revisions [Alex Soto]
+* 2008-03-21 - added #changed_attributes to get access to the changes before a
+ save [Chris Parker]
+* 2007-12-16 - Added #revision_at for retrieving a revision from a specific
+ time [Jacob Atzen]
+* 2007-12-16 - Fix error when getting revision from audit with no changes
+ [Geoffrey Wiseman]
+* 2007-12-16 - Remove dependency on acts_as_list
+* 2007-06-17 - Added support getting previous revisions
+* 2006-11-17 - Replaced use of singleton User.current_user with cache sweeper
+ implementation for auditing the user that made the change
+* 2006-11-17 - added migration generator
+* 2006-08-14 - incorporated changes from Michael Schuerig to write_attribute
+ that saves the new value after every change and not just the
+ first, and performs proper type-casting before doing comparisons
+* 2006-08-14 - The "changes" are now saved as a serialized hash
+* 2006-07-21 - initial version
@@ -0,0 +1,67 @@
+= acts_as_audited
+
+acts_as_audited is an ActiveRecord extension that logs all changes to your models in an audits table.
+
+== Installation
+
+* Install the plugin into your rails app
+ If you are using Rails 2.1:
+
+ script/plugin install git://github.com/collectiveidea/acts_as_audited.git
+
+ For versions prior to 2.1:
+
+ git clone git://github.com/collectiveidea/acts_as_audited.git vendor/plugins/acts_as_audited
+
+* Generate the migration
+ script/generate audited_migration add_audits_table
+ rake db:migrate
+
+== Auditing in Rails
+
+If you're using acts_as_audited within Rails, you can simply declare which models should be audited. acts_as_audited can also automatically record the user that made the change if your controller has a <tt>current_user</tt> method.
+
+ class ApplicationController < ActionController::Base
+ audit User, List, Item
+ protected
+ def current_user
+ @user ||= User.find(session[:user])
+ end
+ end
+
+== Customizing
+
+To get auditing outside of Rails, or to customize which fields are audited within Rails, you can explicitly declare <tt>acts_as_audited</tt> on your models:
+
+ class User < ActiveRecord::Base
+ acts_as_audited :except => [:password, :mistress]
+ end
+
+See http://opensoul.org/2006/07/21/acts_as_audited for more information.
+
+== Caveats
+
+Auditing with user support depends on Rails' caching mechanisms, therefore auditing isn't enabled during development mode. To test that auditing is working, start up your app in production mode, or change the following options in config/environments/environment.rb:
+
+ config.cache_classes = true
+ config.action_controller.perform_caching = true
+
+=== ActiveScaffold
+
+Many users have also reported problems with acts_as_audited and ActiveScaffold, which appears to be caused by a limitation in ActiveScaffold not supporting polymorphic associations. To get it to work with ActiveScaffold:
+
+ class ApplicationController < ActionController::Base
+ audit MyModel, :only => [:create, :update, :destroy]
+ end
+
+== Upgrading
+
+To upgrade from an older version, add a migration with:
+
+ # to version 0.3
+ add_column :audits, :user_type, :string
+ add_column :audits, :username, :string
+
+ # to version 0.4
+ add_column :audits, :version, :integer, :default => 0
+
@@ -0,0 +1,28 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the acts_as_audited plugin'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the acts_as_audited plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'doc'
+ rdoc.title = 'acts_as_audited'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+desc "Publish the rdocs"
+task :publish => [:rdoc] do
+ `ssh host.collectiveidea.com "mkdir -p /var/www/vhosts/source.collectiveidea.com/public/dist/api/acts_as_audited"`
+ Rake::SshDirPublisher.new("host.collectiveidea.com", "/var/www/vhosts/source.collectiveidea.com/public/dist/api/acts_as_audited", "doc").upload
+end
@@ -0,0 +1,7 @@
+Description:
+ The audited migration generator creates a migration to add the audits table.
+
+Example:
+ ./script/generate audited_migration add_audits_table
+
+ This will create a migration in db/migrate/. Run "rake db:migrate" to update your database.
@@ -0,0 +1,7 @@
+class AuditedMigrationGenerator < Rails::Generator::NamedBase
+ def manifest
+ record do |m|
+ m.migration_template 'migration.rb', 'db/migrate'
+ end
+ end
+end
@@ -0,0 +1,23 @@
+class <%= class_name %> < ActiveRecord::Migration
+ def self.up
+ create_table :audits, :force => true do |t|
+ t.column :auditable_id, :integer
+ t.column :auditable_type, :string
+ t.column :user_id, :integer
+ t.column :user_type, :string
+ t.column :username, :string
+ t.column :action, :string
+ t.column :changes, :text
+ t.column :version, :integer, :default => 0
+ t.column :created_at, :datetime
+ end
+
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
+ add_index :audits, :created_at
+ end
+
+ def self.down
+ drop_table :audits
+ end
+end
@@ -0,0 +1,9 @@
+require 'acts_as_audited/audit'
+require 'acts_as_audited'
+
+ActiveRecord::Base.send :include, CollectiveIdea::Acts::Audited
+
+if defined?(ActionController) and defined?(ActionController::Base)
+ require 'acts_as_audited/audit_sweeper'
+ ActionController::Base.send :include, CollectiveIdea::ActionController::Audited
+end
Oops, something went wrong.

0 comments on commit 63e6869

Please sign in to comment.