module Sequel::Plugins::Audited::InstanceMethods

Public Instance Methods

blame() click to toggle source

Returns who put the post into its current state.

post.blame  # => 'joeblogs'

post.last_audited_by  # => 'joeblogs'

Note! returns 'not audited' if there's no audited version (new unsaved record)

# File lib/sequel/plugins/audited.rb, line 210
def blame
  v = versions.last unless versions.empty?
  v ? v.username : "not audited"
end
Also aliased as: last_audited_by
last_audited_at() click to toggle source

Returns who put the post into its current state.

post.last_audited_at  # => '2015-12-19 @ 08:24:45'

post.last_audited_on  # => 'joeblogs'

Note! returns 'not audited' if there's no audited version (new unsaved record)

# File lib/sequel/plugins/audited.rb, line 224
def last_audited_at
  v = versions.last unless versions.empty?
  v ? v.created_at : "not audited"
end
Also aliased as: last_audited_on
last_audited_by()
Alias for: blame
last_audited_on()
Alias for: last_audited_at

Private Instance Methods

add_audited(event) click to toggle source
# File lib/sequel/plugins/audited.rb, line 254
def add_audited(event)
  changed_items = audited_json(event)
  unless changed_items.blank?
    add_version(
      item_type:  model,
      item_uuid:  pk,
      event:      event,
      changed:    changed_items
    )
  end
end
after_create() click to toggle source

CALLBACKS ###

Calls superclass method
# File lib/sequel/plugins/audited.rb, line 269
def after_create
  super
  add_audited("create")
end
after_destroy() click to toggle source
Calls superclass method
# File lib/sequel/plugins/audited.rb, line 279
def after_destroy
  super
  add_audited("destroy")
end
after_update() click to toggle source
Calls superclass method
# File lib/sequel/plugins/audited.rb, line 274
def after_update
  super
  add_audited("update")
end
audited_json(event) click to toggle source
# File lib/sequel/plugins/audited.rb, line 233
def audited_json(event)
  case event
  when "create"
    # store all values on create
    self.values.to_json
  when "update"
    # store only audited columns (skip ignored columns)
    cols_changed = column_changes.empty? ? previous_changes : column_changes
    changes = {}
    cols_changed.keys.each do |ck|
      changes[ck.to_sym] = cols_changed[ck.to_sym] if self.class.audited_columns.include?(ck.to_sym)
    end
    # pass nil if no changes
    changes.empty? ? nil : changes.to_json
  when "destroy"
    # store all values on destroy
    self.values.to_json
  end
end