Module: PaperTrailAssociationTracking::Reifiers::HasMany Private

Defined in:
lib/paper_trail_association_tracking/reifiers/has_many.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Reify a single, direct (not ‘through`) `has_many` association of `model`.

Class Method Summary collapse

Class Method Details

.prepare_array(array, options, versions) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replaces each record in ‘array` with its reified version, if present in `versions`.

Once modified by this method, ‘array` will be assigned to the AR association currently being reified.

Parameters:

  • array
    • The collection to be modified.

  • options
  • versions
    • A ‘Hash` mapping IDs to `Version`s

Returns:

  • nil - Always returns ‘nil`



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/paper_trail_association_tracking/reifiers/has_many.rb', line 35

def prepare_array(array, options, versions)
  # Iterate each child to replace it with the previous value if there is
  # a version after the timestamp.
  array.map! do |record|
    if (version = versions.delete(record.id)).nil?
      record
    elsif version.event == "create"
      options[:mark_for_destruction] ? record.tap(&:mark_for_destruction) : nil
    else
      version.reify(
        options.merge(
          has_many: false,
          has_one: false,
          belongs_to: false,
          has_and_belongs_to_many: false
        )
      )
    end
  end

  # Reify the rest of the versions and add them to the collection, these
  # versions are for those that have been removed from the live
  # associations.
  array.concat(
    versions.values.map { |v|
      v.reify(
        options.merge(
          has_many: false,
          has_one: false,
          belongs_to: false,
          has_and_belongs_to_many: false
        )
      )
    }
  )

  array.compact!

  nil
end

.reify(assoc, model, options, transaction_id, version_table_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paper_trail_association_tracking/reifiers/has_many.rb', line 10

def reify(assoc, model, options, transaction_id, version_table_name)
  versions = load_versions_for_hm_association(
    assoc,
    model,
    version_table_name,
    transaction_id,
    options[:version_at]
  )
  collection = Array.new model.send(assoc.name).reload # to avoid cache
  prepare_array(collection, options, versions)
  model.send(assoc.name).proxy_association.target = collection
end

.versions_by_id(klass, version_ids) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Given a SQL fragment that identifies the IDs of version records, returns a ‘Hash` mapping those IDs to `Version`s.

Parameters:

  • klass
    • An ActiveRecord class.

  • version_ids
    • Array. The IDs of version records.

Returns:

  • A ‘Hash` mapping IDs to `Version`s



84
85
86
87
88
89
# File 'lib/paper_trail_association_tracking/reifiers/has_many.rb', line 84

def versions_by_id(klass, version_ids)
  klass.
    paper_trail.version_class.
    where(id: version_ids).
    inject({}) { |a, e| a.merge!(e.item_id => e) }
end