module PaperTrailAssociationTracking::Reifier::ClassMethods

Public Instance Methods

reify(version, options) click to toggle source

See `VersionConcern#reify` for documentation. @api private

Calls superclass method
# File lib/paper_trail_association_tracking/reifier.rb, line 16
def reify(version, options)
  options = apply_defaults_to(options, version)
  model = super
  reify_associations(model, options, version)
  model
end
reify_has_manys(transaction_id, model, options = {}) click to toggle source

Restore the `model`'s has_many associations as they were at version_at timestamp We lookup the first child versions after version_at timestamp or in same transaction. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 27
def reify_has_manys(transaction_id, model, options = {})
  assoc_has_many_through, assoc_has_many_directly =
    model.class.reflect_on_all_associations(:has_many).
      partition { |assoc| assoc.options[:through] }
  reify_has_many_associations(transaction_id, assoc_has_many_directly, model, options)
  reify_has_many_through_associations(transaction_id, assoc_has_many_through, model, options)
end

Private Instance Methods

apply_defaults_to(options, version) click to toggle source

Given a hash of `options` for `.reify`, return a new hash with default values applied. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 40
def apply_defaults_to(options, version)
  {
    version_at: version.created_at,
    mark_for_destruction: false,
    has_one: false,
    has_many: false,
    belongs_to: false,
    has_and_belongs_to_many: false,
    unversioned_attributes: :nil
  }.merge(options)
end
each_enabled_association(associations, model) { |assoc| ... } click to toggle source

@api private

# File lib/paper_trail_association_tracking/reifier.rb, line 53
def each_enabled_association(associations, model)
  associations.each do |assoc|
    assoc_klass = assoc.polymorphic? ?
                    model.send(assoc.foreign_type).constantize : assoc.klass
    next unless ::PaperTrail.request.enabled_for_model?(assoc_klass)
    yield assoc
  end
end
reify_associations(model, options, version) click to toggle source

@api private

# File lib/paper_trail_association_tracking/reifier.rb, line 63
def reify_associations(model, options, version)
  if options[:has_one]
    reify_has_one_associations(version.transaction_id, model, options)
  end
  if options[:belongs_to]
    reify_belongs_to_associations(version.transaction_id, model, options)
  end
  if options[:has_many]
    reify_has_manys(version.transaction_id, model, options)
  end
  if options[:has_and_belongs_to_many]
    reify_habtm_associations version.transaction_id, model, options
  end
end
reify_belongs_to_associations(transaction_id, model, options = {}) click to toggle source

Reify all `belongs_to` associations of `model`. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 91
def reify_belongs_to_associations(transaction_id, model, options = {})
  associations = model.class.reflect_on_all_associations(:belongs_to)
  each_enabled_association(associations, model) do |assoc|
    ::PaperTrailAssociationTracking::Reifiers::BelongsTo.reify(assoc, model, options, transaction_id)
  end
end
reify_habtm_associations(transaction_id, model, options = {}) click to toggle source

Reify all HABTM associations of `model`. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 118
def reify_habtm_associations(transaction_id, model, options = {})
  model.class.reflect_on_all_associations(:has_and_belongs_to_many).each do |assoc|
    pt_enabled = ::PaperTrail.request.enabled_for_model?(assoc.klass)
    next unless model.class.paper_trail_save_join_tables.include?(assoc.name) || pt_enabled
    ::PaperTrailAssociationTracking::Reifiers::HasAndBelongsToMany.reify(pt_enabled, assoc, model, options, transaction_id)
  end
end
reify_has_many_associations(transaction_id, associations, model, options = {}) click to toggle source

Reify all direct (not `through`) `has_many` associations of `model`. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 100
def reify_has_many_associations(transaction_id, associations, model, options = {})
  version_table_name = model.class.paper_trail.version_class.table_name
  each_enabled_association(associations, model) do |assoc|
    ::PaperTrailAssociationTracking::Reifiers::HasMany.reify(assoc, model, options, transaction_id, version_table_name)
  end
end
reify_has_many_through_associations(transaction_id, associations, model, options = {}) click to toggle source

Reify all HMT associations of `model`. This must be called after the direct (non-`through`) has_manys have been reified. @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 110
def reify_has_many_through_associations(transaction_id, associations, model, options = {})
  each_enabled_association(associations, model) do |assoc|
    ::PaperTrailAssociationTracking::Reifiers::HasManyThrough.reify(assoc, model, options, transaction_id)
  end
end
reify_has_one_associations(transaction_id, model, options = {}) click to toggle source

Restore the `model`'s has_one associations as they were when this version was superseded by the next (because that's what the user was looking at when they made the change). @api private

# File lib/paper_trail_association_tracking/reifier.rb, line 82
def reify_has_one_associations(transaction_id, model, options = {})
  associations = model.class.reflect_on_all_associations(:has_one)
  each_enabled_association(associations, model) do |assoc|
    ::PaperTrailAssociationTracking::Reifiers::HasOne.reify(assoc, model, options, transaction_id)
  end
end