Class: EffectiveLogger

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective_logger.rb

Overview

Call EffectiveLog.info or EffectiveLog.success EffectiveLog.error

Class Method Summary collapse

Class Method Details

.log(message, status = EffectiveLogging.statuses.first, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/effective_logger.rb', line 4

def self.log(message, status = EffectiveLogging.statuses.first, options = {})
  if options[:user].present? && !options[:user].kind_of?(User)
    raise ArgumentError.new("Log.log :user => ... argument must be a User object")
  end

  if options[:parent].present? && !options[:parent].kind_of?(Effective::Log)
    raise ArgumentError.new("Log.log :parent => ... argument must be an Effective::Log object")
  end

  if options[:associated].present? && !options[:associated].kind_of?(ActiveRecord::Base)
    raise ArgumentError.new("Log.log :associated => ... argument must be an ActiveRecord::Base object")
  end

  Effective::Log.new().tap do |log|
    log.message = message
    log.status = status

    log.user_id = options.delete(:user_id)
    log.user = options.delete(:user)
    log.parent = options.delete(:parent)
    log.associated = options.delete(:associated)

    log.details = options.delete_if { |k, v| v.blank? } if options.kind_of?(Hash)

    log.save
  end
end