Class: Audit::Changeset

Inherits:
Struct
  • Object
show all
Defined in:
lib/audit/changeset.rb

Overview

A structure for tracking an atomic group of changes to a model.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#changesObject

Returns the value of attribute changes

Returns:

  • (Object)

    the current value of changes



5
6
7
# File 'lib/audit/changeset.rb', line 5

def changes
  @changes
end

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



5
6
7
# File 'lib/audit/changeset.rb', line 5

def 
  
end

Class Method Details

.from_enumerable(enum) ⇒ Object

Recreate a changeset given one or more stored audit records.

enum - an Array of change Hashes (see ‘from_hash` for details)

Returns an Array of Changeset objects, one for each atomic change



35
36
37
38
39
40
41
42
# File 'lib/audit/changeset.rb', line 35

def self.from_enumerable(enum)
  case enum
  when Hash
    from_hash(enum)
  when Array
    enum.map { |hsh| from_hash(hsh) }
  end
end

.from_hash(hsh) ⇒ Object

Recreate a changeset given change data as generated by ActiveRecord.

hsh - the Hash to convert to a Changeset. Recognizes two keys:

"changes" - a Hash of changes as generated by ActiveRecord
"metadata" - user-provided  regarding this change

Examples:

Audit::Changeset.from_hash({"changes" => {'age' => [30, 31]}})
# [<struct Audit::Changeset @attribute="age" @old_value=30 
#  @new_value=31>]

Returns an Array of Changeset objects, one for each changed attribute



20
21
22
23
24
25
26
27
28
# File 'lib/audit/changeset.rb', line 20

def self.from_hash(hsh)
  changes = hsh["changes"].map do |k, v|
    attribute = k
    old_value = v.first
    new_value = v.last
    Audit::Change.new(attribute, old_value, new_value)
  end
  new(changes, hsh["metadata"])
end