Module: ActiveRecordAuditor

Defined in:
lib/active_record_auditor.rb,
lib/active_record_auditor/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.build_audit_table(table) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_record_auditor.rb', line 6

def self.build_audit_table(table)
  time = Time.now
  new_table_name = "#{table}_#{time.month}_#{time.year}"
  ActiveRecord::Base.connection.execute("CREATE TABLE #{new_table_name} like #{table}")
  ActiveRecord::Base.connection.execute("SHOW INDEX FROM #{new_table_name}").to_a.uniq{|index| index[2]}.each do |index|
    ActiveRecord::Base.connection.execute("ALTER TABLE #{new_table_name} DROP INDEX #{index[2]};") unless index[2] == 'PRIMARY'
  end
  ActiveRecord::Base.connection.execute("DESCRIBE #{new_table_name}").to_a.uniq{|index| index[0]}.each do |col|
    ActiveRecord::Base.connection.execute("ALTER TABLE #{new_table_name} MODIFY COLUMN #{col[0]} #{col[1]};") if col[2] == "YES"
  end
  ActiveRecord::Migration.add_column new_table_name.to_sym, :__username, :string
  ActiveRecord::Migration.add_column new_table_name.to_sym, :__deleted, :boolean, default: false
  ActiveRecord::Migration.add_column new_table_name.to_sym, :__canonical_id, :integer, null: false
  ActiveRecord::Migration.add_column new_table_name.to_sym, :__version, :integer, null: false
  ActiveRecord::Migration.add_column new_table_name.to_sym, :__created_at, :datetime, null: false
  ActiveRecord::Migration.add_index new_table_name.to_sym, [:__canonical_id, :__version], unique: true
end

.load_tasksObject



24
25
26
# File 'lib/active_record_auditor.rb', line 24

def self.load_tasks
  load "active_record_auditor/tasks/setup.rake"
end