lib/ar-audit-tracer.rb in ar-audit-tracer-1.0.2 vs lib/ar-audit-tracer.rb in ar-audit-tracer-2.0.0
- old
+ new
@@ -1,115 +1,28 @@
# encoding: utf-8
+require 'active_record/migration/command_recorder'
+require 'active_record/author_stamp'
require 'concern/audit/author'
module ActiveRecord
- # = Active Record Author
- #
- # Active Record automatically authors create and update operations if the
- # table has fields named <tt>created_by</tt> or
- # <tt>updated_by</tt>.
- #
- # Authoring can be turned off by setting:
- #
- # <tt>ActiveRecord::Base.record_authors = false</tt>
- module Author
- extend ActiveSupport::Concern
- included do
- class_attribute :record_authors
- self.record_authors = true
- end
-
- def initialize_dup(other)
- clear_author_attributes
- end
-
- private
-
- def create #:nodoc:
- if self.record_authors
- current_author = Concern::Audit::Author.current
-
- all_author_attributes.each do |column|
- if respond_to?(column) && self.send(column).nil?
- write_attribute(column.to_s, current_author)
- end
- end
- end
-
- super
- end
-
- def update(*args) #:nodoc:
- if should_record_authors?
- current_author = Concern::Audit::Author.current
-
- author_attributes_for_update_in_model.each do |column|
- column = column.to_s
- next if attribute_changed?(column)
- write_attribute(column, current_author)
- end
- end
- super
- end
-
- def should_record_authors?
- self.record_authors && (!partial_updates? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
- end
-
- def author_attributes_for_update_in_model
- author_attributes_for_update.select { |c| respond_to?(c) }
- end
-
- def author_attributes_for_create_in_model
- author_attributes_for_create.select { |c| respond_to?(c) }
- end
-
- def all_author_attributes
- author_attributes_for_create + author_attributes_for_update
- end
-
- def author_attributes_for_update #:nodoc:
- [:updated_by]
- end
-
- def author_attributes_for_create #:nodoc:
- [:created_by]
- end
-
- def all_author_attributes_in_model
- author_attributes_for_create_in_model + author_attributes_for_update_in_model
- end
-
- # Clear attributes and changed_attributes
- def clear_author_attributes
- all_author_attributes_in_model.each do |attribute_name|
- self[attribute_name] = nil
- changed_attributes.delete(attribute_name)
- end
- end
-
- end
-
-
-
module ConnectionAdapters
module SchemaStatements
# Adds author columns (created_by and updated_by) to the named table.
# ===== Examples
# add_authors(:suppliers)
- def add_authors(table_name, type=:string, *args)
+ def add_authorstamps(table_name, type=:string, *args)
options = {:null => false}.merge(args.extract_options!)
add_column table_name, :created_by, type, options
add_column table_name, :updated_by, type, options
end
# Removes the author columns (created_by and updated_by) from the table definition.
# ===== Examples
# remove_authors(:suppliers)
- def remove_authors(table_name)
+ def remove_authorstamps(table_name)
remove_column table_name, :updated_by
remove_column table_name, :created_by
end
end
@@ -118,11 +31,11 @@
# Creates author columns ...
#
# @param Symbol type The desired type for the columns, defaults to :string
# @param Hash *args Column options from rails
- def authors(type=:string, *args)
+ def authorstamps(type=:string, *args)
options = args.extract_options!
column(:created_by, type, options)
column(:updated_by, type, options)
end
@@ -133,28 +46,28 @@
# ===== Example
# t.authors
#
# @param Symbol type The desired type for the columns, defaults to :string
# @see SchemaStatements#add_authors
- def authors(type=:string, *args)
+ def authorstamps(type=:string, *args)
options = {:null => false}.merge(args.extract_options!)
- @base.add_authors(@table_name, type, options)
+ @base.add_authorstamps(@table_name, type, options)
end
# Removes the author columns (created_by and updated_by) from the table.
# ===== Example
# t.remove_authors
- def remove_authors
- @base.remove_authors(@table_name)
+ def remove_authorstamps
+ @base.remove_authorstamps(@table_name)
end
end
end
Base.class_eval do
- include Author
+ include ActiveRecord::AuthorStamp
end
end