Class: LogBook::SquashRecords

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

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ SquashRecords

Returns a new instance of SquashRecords.



3
4
5
# File 'lib/log_book/squash_records.rb', line 3

def initialize(records)
  @records = records
end

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/log_book/squash_records.rb', line 7

def call
  records.group_by(&:parent).each do |parent, children|
    next if parent.nil?
    children_to_squash = children.select { |child| child.subject.try(:to_squash?) }
    next if children_to_squash.empty?

    parent_in_records = parent_in_records(parent)
    parent_in_records.record_changes.merge!(squashed_changes(children_to_squash, :record_changes))
    parent_in_records.meta.merge!(squashed_changes(children_to_squash, :meta))
    parent_in_records.created_at ||= children_to_squash.first.created_at
    parent_in_records.save

    children_to_squash.each(&:delete)
  end
end