Class: PaperTrailHashDiff

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

Overview

Allows storing only incremental changes in the object_changes column Uses HashDiff (github.com/liufengyun/hashdiff)

Instance Method Summary collapse

Instance Method Details

#diff(changes) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/paper_trail_hashdiff.rb', line 6

def diff(changes)
  diff_changes = {}
  changes.each do |field, value_changes|
    diff_changes[field] = Hashdiff.diff(value_changes[0], value_changes[1], array_path: true)
  end
  diff_changes
end

#load_changeset(version) ⇒ Object



22
23
24
# File 'lib/paper_trail_hashdiff.rb', line 22

def load_changeset(version)
  HashWithIndifferentAccess.new(version.object_changes_deserialized)
end

#where_object_changes(version_model_class, attributes) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/paper_trail_hashdiff.rb', line 14

def where_object_changes(version_model_class, attributes)
  scope = version_model_class
  attributes.each do |k, v|
    scope = scope.where('(((object_changes -> ?)::jsonb ->> 0)::jsonb @> ?)', k.to_s, v.to_s)
  end
  scope
end