class LogBook::SaveRecords

Attributes

tree[R]

Public Class Methods

call() click to toggle source
# File lib/log_book/save_records.rb, line 7
def self.call
  new.call
end
new() click to toggle source
# File lib/log_book/save_records.rb, line 3
def initialize
  @tree = LogBook::Store.tree
end

Public Instance Methods

call() click to toggle source
# File lib/log_book/save_records.rb, line 11
def call
  return unless LogBook.recording_enabled

  squash_tree(tree) if LogBook.record_squashing_enabled

  tree.records(only_roots: LogBook.record_squashing_enabled).each do |_key, record|
    create_record(record.value)
  end
end

Private Instance Methods

create_record(record) click to toggle source
# File lib/log_book/save_records.rb, line 44
def create_record(record)
  return unless record.changes?

  attributes = record.to_h
  attributes.merge!(
    author: tree.author,
    action: tree.action,
    request_uuid: tree.request_uuid
  )
  LogBook::Record.create(attributes)
end
squash_tree(tree) click to toggle source
# File lib/log_book/save_records.rb, line 25
def squash_tree(tree)
  tree.depth.downto(1).each do |depth|
    nodes = tree.at_depth(depth)
    nodes.each do |_, node|
      next unless node.value.changes?
      parent = node.parent.value

      parent.record_changes = squashed_changes(node.value, parent.record_changes, :record_changes)
      parent.meta = squashed_changes(node.value, parent.meta, :meta)
    end
  end
end
squashed_changes(record, object, key) click to toggle source
# File lib/log_book/save_records.rb, line 38
def squashed_changes(record, object, key)
  object[record.subject_key] ||= {}
  object[record.subject_key][record.subject_id] = record.send(key)
  object
end