Sha256: 59f86d5b5cf14bd8816bb974672fb1acbb4ebc0ff8f59cf83e9a6e35dfbe8e3d

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module ModelLog
  module Modeller
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def model_log
        after_create :model_log_create
        after_destroy :model_log_destroy
        before_update :model_log_update
      end
    end

    private

    def model_log_create
      model_log_write(self, :create)
    end

    def model_log_update
      model_log_write(self, :update)
    end

    def model_log_destroy
      model_log_write(self, :destroy)
    end

    def model_log_write(resource, action)
      # ModelLog.logger.debug resource.changes
      # update changes is not empty
      # create and destroy changes is empty
      if log_content(resource, action)
        ModelLog.logger.info log_content(resource, action)
      end
    rescue => ex
      ModelLog.logger.error "#{ex.class}: #{ex.message}"
    end

    def log_content(resource, action)
      @log_content ||= Log::Content.new(resource, action, ModelLog.config.formatter).content
    end
  end
end

ActiveRecord::Base.send(:include, ModelLog::Modeller) if defined? ActiveRecord::Base

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
model_log-2.0.0 lib/model_log/modeller.rb