lib/ar-audit-tracer.rb in ar-audit-tracer-0.2.0 vs lib/ar-audit-tracer.rb in ar-audit-tracer-1.0.0

- old
+ new

@@ -1,66 +1,10 @@ +# encoding: utf-8 require 'concern/audit/author' -# ActiveRecordAuthors module ActiveRecord - module ConnectionAdapters - class TableDefinition - - # 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) - options = args.extract_options! - column(:created_by, type, options) - column(:updated_by, type, options) - end - - - end - - class Table - # Adds author (created_by and updated_by) columns to the table - # ===== Example - # t.authors - # - # @param Symbol type The desired type for the columns, defaults to :string - # @see SchemaStatements#add_authors - def authors(type=:string) - @base.add_authors(@table_name, type) - 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) - end - - end - - 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) - add_column table_name, :created_by, type - add_column table_name, :updated_by, type - end - - # Removes the author columns (created_by and updated_by) from the table definition. - # ===== Examples - # remove_authors(:suppliers) - def remove_authors(table_name) - remove_column table_name, :updated_by - remove_column table_name, :created_by - end - end - - end - # = 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>. @@ -70,22 +14,28 @@ # <tt>ActiveRecord::Base.record_authors = false</tt> module Author extend ActiveSupport::Concern included do - class_inheritable_accessor :record_authors, :instance_writer => false + class_attribute :record_authors self.record_authors = true end + def initialize_dup(other) + clear_author_attributes + end + private def create #:nodoc: - if record_authors + if self.record_authors current_author = Concern::Audit::Author.current all_author_attributes.each do |column| - write_attribute(column.to_s, current_author) if respond_to?(column) && self.send(column).nil? + if respond_to?(column) && self.send(column).nil? + write_attribute(column.to_s, current_author) + end end end super end @@ -102,32 +52,108 @@ end super end def should_record_authors? - record_authors && (!partial_updates? || changed?) + 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 #:nodoc: - author_attributes_for_create + author_attributes_for_update + 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) + add_column table_name, :created_by, type, :null => false + add_column table_name, :updated_by, type, :null => false + end + + # Removes the author columns (created_by and updated_by) from the table definition. + # ===== Examples + # remove_authors(:suppliers) + def remove_authors(table_name) + remove_column table_name, :updated_by + remove_column table_name, :created_by + end + end + + + class TableDefinition + + # 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) + options = args.extract_options! + column(:created_by, type, options) + column(:updated_by, type, options) + end + + end + + class Table + # Adds author (created_by and updated_by) columns to the table + # ===== Example + # t.authors + # + # @param Symbol type The desired type for the columns, defaults to :string + # @see SchemaStatements#add_authors + def authors(type=:string) + @base.add_authors(@table_name, type) + 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) + end + + end + + end + + Base.class_eval do include Author end end +