Module: PaperTrail::Reifier Private

Defined in:
lib/paper_trail/reifier.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.

Given a version record and some options, builds a new model object.

Class Method Summary collapse

Class Method Details

.reify(version, options) ⇒ 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.

See ‘VersionConcern#reify` for documentation.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/paper_trail/reifier.rb', line 10

def reify(version, options)
  options = apply_defaults_to(options, version)
  attrs = version.object_deserialized

  # Normally a polymorphic belongs_to relationship allows us to get the
  # object we belong to by calling, in this case, `item`.  However this
  # returns nil if `item` has been destroyed, and we need to be able to
  # retrieve destroyed objects.
  #
  # In this situation we constantize the `item_type` to get hold of the
  # class...except when the stored object's attributes include a `type`
  # key.  If this is the case, the object we belong to is using single
  # table inheritance and the `item_type` will be the base class, not the
  # actual subclass. If `type` is present but empty, the class is the base
  # class.
  if options[:dup] != true && version.item
    model = version.item
    if options[:unversioned_attributes] == :nil
      init_unversioned_attrs(attrs, model)
    end
  else
    klass = version_reification_class(version, attrs)
    # The `dup` option always returns a new object, otherwise we should
    # attempt to look for the item outside of default scope(s).
    if options[:dup] || (item_found = klass.unscoped.find_by_id(version.item_id)).nil?
      model = klass.new
    elsif options[:unversioned_attributes] == :nil
      model = item_found
      init_unversioned_attrs(attrs, model)
    end
  end

  reify_attributes(model, version, attrs)
  model.send "#{model.class.version_association_name}=", version
  reify_associations(model, options, version)
  model
end