Module: PaperTrailAssociationTracking::RecordTrail

Included in:
PaperTrail::RecordTrail
Defined in:
lib/paper_trail_association_tracking/record_trail.rb

Instance Method Summary collapse

Instance Method Details

#appear_as_new_recordObject

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.

Utility method for reifying. Anything executed inside the block will appear like a new record.

> .. as best as I can tell, the purpose of > appear_as_new_record was to attempt to prevent the callbacks in > AutosaveAssociation (which is the module responsible for persisting > foreign key changes earlier than most people want most of the time > because backwards compatibility or the maintainer hates himself or > something) from running. By also stubbing out persisted? we can > actually prevent those. A more stable option might be to use suppress > instead, similar to the other branch in reify_has_one. > -Sean Griffin (github.com/paper-trail-gem/paper_trail/pull/899)



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 19

def appear_as_new_record
  @record.instance_eval {
    alias :old_new_record? :new_record?
    alias :new_record? :present?
    alias :old_persisted? :persisted?
    alias :persisted? :nil?
  }
  yield
  @record.instance_eval {
    alias :new_record? :old_new_record?
    alias :persisted? :old_persisted?
  }
end

#data_for_createObject

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.



43
44
45
46
47
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 43

def data_for_create
  data = super
  add_transaction_id_to(data)
  data
end

#data_for_destroyObject

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.



60
61
62
63
64
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 60

def data_for_destroy
  data = super
  add_transaction_id_to(data)
  data
end

#data_for_update(*args) ⇒ 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.

Used during ‘record_update`, returns a hash of data suitable for an AR `create`. That is, all the attributes of the nascent `Version` record.



88
89
90
91
92
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 88

def data_for_update(*args)
  data = super
  add_transaction_id_to(data)
  data
end

#data_for_update_columns(*args) ⇒ 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.

Returns data for record_update_columns



106
107
108
109
110
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 106

def data_for_update_columns(*args)
  data = super
  add_transaction_id_to(data)
  data
end

#record_createObject

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.



34
35
36
37
38
39
40
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 34

def record_create
  version = super
  if version
    update_transaction_id(version)
    save_associations(version)
  end
end

#record_destroy(*args) ⇒ 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.



50
51
52
53
54
55
56
57
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 50

def record_destroy(*args)
  version = super
  if version && version.respond_to?(:errors) && version.errors.empty?
    update_transaction_id(version)
    save_associations(version)
  end
  version
end

#record_object_changes?Boolean

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.

Returns a boolean indicating whether to store serialized version diffs in the ‘object_changes` column of the version record.

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 69

def record_object_changes?
  @record.paper_trail_options[:save_changes] &&
    @record.class.paper_trail.version_class.column_names.include?("object_changes")
end

#record_update(**opts) ⇒ 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.



75
76
77
78
79
80
81
82
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 75

def record_update(**opts)
  version = super
  if version && version.respond_to?(:errors) && version.errors.empty?
    update_transaction_id(version)
    save_associations(version)
  end
  version
end

#record_update_columns(*args) ⇒ 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.



95
96
97
98
99
100
101
102
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 95

def record_update_columns(*args)
  version = super
  if version && version.respond_to?(:errors) && version.errors.empty?
    update_transaction_id(version)
    save_associations(version)
  end
  version
end

#save_associations(version) ⇒ Object

Saves associations if the join table for ‘VersionAssociation` exists.



113
114
115
116
117
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 113

def save_associations(version)
  return unless ::PaperTrail.config.track_associations?
  save_bt_associations(version)
  save_habtm_associations(version)
end

#save_bt_associations(version) ⇒ 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.

Save all ‘belongs_to` associations.



121
122
123
124
125
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 121

def save_bt_associations(version)
  @record.class.reflect_on_all_associations(:belongs_to).each do |assoc|
    save_bt_association(assoc, version)
  end
end

#save_habtm_associations(version) ⇒ 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.

When a record is created, updated, or destroyed, we determine what the HABTM associations looked like before any changes were made, by using the ‘paper_trail_habtm` data structure. Then, we create `VersionAssociation` records for each of the associated records.



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/paper_trail_association_tracking/record_trail.rb', line 132

def save_habtm_associations(version)
  @record.class.reflect_on_all_associations(:has_and_belongs_to_many).each do |a|
    next unless save_habtm_association?(a)
    habtm_assoc_ids(a).each do |id|
      ::PaperTrail::VersionAssociation.create(
        version_id: version.transaction_id,
        foreign_key_name: a.name,
        foreign_key_id: id,
        foreign_type: a.klass
      )
    end
  end
end