Module: PaperTrail::Reifiers::HasMany Private
- Defined in:
- lib/paper_trail/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
-
.prepare_array(array, options, versions) ⇒ Object
private
Replaces each record in
arraywith its reified version, if present inversions. - .reify(assoc, model, options, transaction_id, version_table_name) ⇒ Object private
-
.versions_by_id(klass, version_id_subquery) ⇒ Object
private
Given a SQL fragment that identifies the IDs of version records, returns a
Hashmapping those IDs to ‘Version`s.
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.
33 34 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 |
# File 'lib/paper_trail/reifiers/has_many.rb', line 33 def prepare_array(array, , 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" [:mark_for_destruction] ? record.tap(&:mark_for_destruction) : nil else version.reify( .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( .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.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/paper_trail/reifiers/has_many.rb', line 8 def reify(assoc, model, , transaction_id, version_table_name) versions = load_versions_for_hm_association( assoc, model, version_table_name, transaction_id, [:version_at] ) collection = Array.new model.send(assoc.name).reload # to avoid cache prepare_array(collection, , versions) model.send(assoc.name).proxy_association.target = collection end |
.versions_by_id(klass, version_id_subquery) ⇒ 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.
83 84 85 86 87 88 |
# File 'lib/paper_trail/reifiers/has_many.rb', line 83 def versions_by_id(klass, version_id_subquery) klass. paper_trail.version_class. where("id IN (#{version_id_subquery})"). inject({}) { |a, e| a.merge!(e.item_id => e) } end |