Sha256: 7412cd8a04d52bb1c108da25ded25f183cab7913340a688a0e65b13f55f07d98

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 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
      record = Rack::ActionLogger::ParameterFiltering.apply_filter(record)
      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
      record = Rack::ActionLogger::ParameterFiltering.apply_filter(record)
      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
      record = Rack::ActionLogger::ParameterFiltering.apply_filter(record)
      Rack::ActionLogger::Container.set_append_log(record, "model_#{self.class.table_name}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-action_logger-0.1.9 lib/rack/action_logger/active_record_extension.rb