Class: Magick::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/magick/audit_log.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(adapter = nil) ⇒ AuditLog

Returns a new instance of AuditLog.



29
30
31
32
33
# File 'lib/magick/audit_log.rb', line 29

def initialize(adapter = nil)
  @adapter = adapter || default_adapter
  @logs = []
  @mutex = Mutex.new
end

Instance Method Details

#entries(feature_name: nil, limit: 100) ⇒ Object



50
51
52
53
54
# File 'lib/magick/audit_log.rb', line 50

def entries(feature_name: nil, limit: 100)
  result = @logs
  result = result.select { |e| e.feature_name == feature_name.to_s } if feature_name
  result.last(limit)
end

#log(feature_name, action, user_id: nil, changes: {}, metadata: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/magick/audit_log.rb', line 35

def log(feature_name, action, user_id: nil, changes: {}, metadata: {})
  entry = Entry.new(feature_name, action, user_id: user_id, changes: changes, metadata: )
  @mutex.synchronize do
    @logs << entry
    @adapter.append(entry) if @adapter.respond_to?(:append)
  end

  # Rails 8+ event
  if defined?(Magick::Rails::Events) && Magick::Rails::Events.rails8?
    Magick::Rails::Events.audit_logged(feature_name, action: action, user_id: user_id, changes: changes, **)
  end

  entry
end