Sha256: 3252691af2bd31d44a31d60de2f0d7f93538253a5588c3516ceb6aaba84aa11f

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

require 'active_support'
require 'active_support/core_ext'

module Rack::ActionLogger
  module ActiveRecordExtension
    extend ActiveSupport::Concern

    included do
      after_create :capture_action_log_create
      after_update :capture_action_log_update
      after_destroy :capture_action_log_destroy
    end

    def capture_action_log_create
      record = { _method: 'create' }
      self.class.column_names.each do |column_name|
        record["_#{column_name}"] = self.try(column_name)
      end
      Rack::ActionLogger::Container.set_append_log(record, "model_#{self.class.table_name}")
    end

    def capture_action_log_update
      record = { _method: 'update' }
      self.class.column_names.each do |column_name|
        if column_name.end_with?('_id')
          record["_#{column_name}"] = self.try(column_name)
        elsif self.try("#{column_name}_changed?")
          record["_after:#{column_name}"] = self.try(column_name)
          record["_before:#{column_name}"] = self.try("#{column_name}_was")
        end
      end
      Rack::ActionLogger::Container.set_append_log(record, "model_#{self.class.table_name}")
    end

    def capture_action_log_destroy
      record = { _method: 'delete' }
      self.class.column_names.each do |column_name|
        if column_name == 'id' || column_name.end_with?('_id')
          record["_#{column_name}"] = self.try(column_name)
        end
      end
      Rack::ActionLogger::Container.set_append_log(record, "model_#{self.class.table_name}")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-action_logger-0.1.8 lib/rack/action_logger/active_record_extension.rb
rack-action_logger-0.1.7 lib/rack/action_logger/active_record_extension.rb
rack-action_logger-0.1.6 lib/rack/action_logger/active_record_extension.rb