1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Merge pull request #893 from jnstq/master

Suggestion to move record update data to it's own method
This commit is contained in:
Jared Beck 2016-11-29 16:21:07 -05:00 committed by GitHub
commit 4ae88af474

View file

@ -165,6 +165,15 @@ module PaperTrail
def record_create
return unless enabled?
versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create! data_for_create
update_transaction_id(version)
save_associations(version)
end
# Returns data for record create
# @api private
def data_for_create
data = {
event: @record.paper_trail_event || "create",
whodunnit: PaperTrail.whodunnit
@ -176,23 +185,12 @@ module PaperTrail
data[:object_changes] = recordable_object_changes
end
add_transaction_id_to(data)
versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create! merge_metadata(data)
update_transaction_id(version)
save_associations(version)
merge_metadata(data)
end
def record_destroy
if enabled? && !@record.new_record?
data = {
item_id: @record.id,
item_type: @record.class.base_class.name,
event: @record.paper_trail_event || "destroy",
object: recordable_object,
whodunnit: PaperTrail.whodunnit
}
add_transaction_id_to(data)
version = @record.class.paper_trail.version_class.create(merge_metadata(data))
version = @record.class.paper_trail.version_class.create(data_for_destroy)
if version.errors.any?
log_version_errors(version, :destroy)
else
@ -204,6 +202,20 @@ module PaperTrail
end
end
# Returns data for record destroy
# @api private
def data_for_destroy
data = {
item_id: @record.id,
item_type: @record.class.base_class.name,
event: @record.paper_trail_event || "destroy",
object: recordable_object,
whodunnit: PaperTrail.whodunnit
}
add_transaction_id_to(data)
merge_metadata(data)
end
# Returns a boolean indicating whether to store serialized version diffs
# in the `object_changes` column of the version record.
# @api private
@ -214,20 +226,8 @@ module PaperTrail
def record_update(force)
if enabled? && (force || changed_notably?)
data = {
event: @record.paper_trail_event || "update",
object: recordable_object,
whodunnit: PaperTrail.whodunnit
}
if @record.respond_to?(:updated_at)
data[:created_at] = @record.updated_at
end
if record_object_changes?
data[:object_changes] = recordable_object_changes
end
add_transaction_id_to(data)
versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create(merge_metadata(data))
version = versions_assoc.create(data_for_update)
if version.errors.any?
log_version_errors(version, :update)
else
@ -237,6 +237,24 @@ module PaperTrail
end
end
# Returns data for record update
# @api private
def data_for_update
data = {
event: @record.paper_trail_event || "update",
object: recordable_object,
whodunnit: PaperTrail.whodunnit
}
if @record.respond_to?(:updated_at)
data[:created_at] = @record.updated_at
end
if record_object_changes?
data[:object_changes] = recordable_object_changes
end
add_transaction_id_to(data)
merge_metadata(data)
end
# Returns an object which can be assigned to the `object` attribute of a
# nascent version record. If the `object` column is a postgres `json`
# column, then a hash can be used in the assignment, otherwise the column