Sha256: 0e40ca960e605ac54da1ff83a66a7da9c0fbf9ef19c33e08a47fee7e3f688dad
Contents?: true
Size: 1.12 KB
Versions: 3
Compression:
Stored size: 1.12 KB
Contents
module LogBook class SquashRecords def initialize(records) @records = records end 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 private attr_reader :records def squashed_changes(children, key) children.each_with_object({}) do |record, object| object[record.subject_key] ||= {} object[record.subject_key][record.subject_id] = record.send(key) end end def parent_in_records(parent) records.find { |record| record.subject == parent } || parent.new_record end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rails_log_book-0.3.0 | lib/log_book/squash_records.rb |
rails_log_book-0.2.0 | lib/log_book/squash_records.rb |
rails_log_book-0.1.0 | lib/log_book/squash_records.rb |