Skip to content

Commit

Permalink
Add support for tracking the remote address of the client causing the…
Browse files Browse the repository at this point in the history
… audit (closes #10)
  • Loading branch information
kennethkalmer committed Nov 8, 2010
1 parent ef233ad commit 814b925
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/acts_as_audited/audit_sweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class AuditSweeper < ActionController::Caching::Sweeper

def before_create(audit)
audit.user ||= current_user
audit.remote_address = controller.try(:request).try(:remote_ip)
end

def current_user
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def self.up
add_column :audits, :remote_address, :string
end

def self.down
remove_column :audits, :remote_address
end
end

1 change: 1 addition & 0 deletions lib/generators/acts_as_audited/templates/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.up
t.column :audited_changes, :text
t.column :version, :integer, :default => 0
t.column :comment, :string
t.column :remote_address, :string
t.column :created_at, :datetime
end

Expand Down
4 changes: 4 additions & 0 deletions lib/generators/acts_as_audited/upgrade_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def migrations_to_be_applied
if columns.include?( 'changes' )
yield :rename_changes_to_audited_changes
end

unless columns.include?( 'remote_address' )
yield :add_remote_address_to_audits
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/audit_sweeper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def update_user

assigns(:company).audits.last.user.should == user
end

it "should record the remote address responsible for the change" do
request.env['REMOTE_ADDR'] = "1.2.3.4"
controller.send(:current_user=, user)

post :audit

assigns(:company).audits.last.remote_address.should == '1.2.3.4'
end

end

describe "POST update_user" do
Expand All @@ -59,5 +69,6 @@ def update_user
post :update_user
}.to_not change( Audit, :count )
end

end
end
1 change: 1 addition & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
t.column :audited_changes, :text
t.column :version, :integer, :default => 0
t.column :comment, :string
t.column :remote_address, :string
t.column :created_at, :datetime
end

Expand Down
19 changes: 19 additions & 0 deletions test/db/version_3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ActiveRecord::Schema.define do
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 :audited_changes, :text
t.column :version, :integer, :default => 0
t.column :comment, :string
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

10 changes: 10 additions & 0 deletions test/upgrade_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
assert_match /rename_column :audits, :changes, :audited_changes/, content
end
end

test "should add a 'remote_address' to audits table" do
load_schema 3

run_generator %w(upgrade)

assert_migration "db/migrate/add_remote_address_to_audits.rb" do |content|
assert_match /add_column :audits, :remote_address, :string/, content
end
end
end

0 comments on commit 814b925

Please sign in to comment.