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:
commit
4ae88af474
1 changed files with 44 additions and 26 deletions
|
@ -165,6 +165,15 @@ module PaperTrail
|
||||||
|
|
||||||
def record_create
|
def record_create
|
||||||
return unless enabled?
|
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 = {
|
data = {
|
||||||
event: @record.paper_trail_event || "create",
|
event: @record.paper_trail_event || "create",
|
||||||
whodunnit: PaperTrail.whodunnit
|
whodunnit: PaperTrail.whodunnit
|
||||||
|
@ -176,23 +185,12 @@ module PaperTrail
|
||||||
data[:object_changes] = recordable_object_changes
|
data[:object_changes] = recordable_object_changes
|
||||||
end
|
end
|
||||||
add_transaction_id_to(data)
|
add_transaction_id_to(data)
|
||||||
versions_assoc = @record.send(@record.class.versions_association_name)
|
merge_metadata(data)
|
||||||
version = versions_assoc.create! merge_metadata(data)
|
|
||||||
update_transaction_id(version)
|
|
||||||
save_associations(version)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def record_destroy
|
def record_destroy
|
||||||
if enabled? && !@record.new_record?
|
if enabled? && !@record.new_record?
|
||||||
data = {
|
version = @record.class.paper_trail.version_class.create(data_for_destroy)
|
||||||
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))
|
|
||||||
if version.errors.any?
|
if version.errors.any?
|
||||||
log_version_errors(version, :destroy)
|
log_version_errors(version, :destroy)
|
||||||
else
|
else
|
||||||
|
@ -204,6 +202,20 @@ module PaperTrail
|
||||||
end
|
end
|
||||||
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
|
# Returns a boolean indicating whether to store serialized version diffs
|
||||||
# in the `object_changes` column of the version record.
|
# in the `object_changes` column of the version record.
|
||||||
# @api private
|
# @api private
|
||||||
|
@ -214,20 +226,8 @@ module PaperTrail
|
||||||
|
|
||||||
def record_update(force)
|
def record_update(force)
|
||||||
if enabled? && (force || changed_notably?)
|
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)
|
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?
|
if version.errors.any?
|
||||||
log_version_errors(version, :update)
|
log_version_errors(version, :update)
|
||||||
else
|
else
|
||||||
|
@ -237,6 +237,24 @@ module PaperTrail
|
||||||
end
|
end
|
||||||
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
|
# Returns an object which can be assigned to the `object` attribute of a
|
||||||
# nascent version record. If the `object` column is a postgres `json`
|
# nascent version record. If the `object` column is a postgres `json`
|
||||||
# column, then a hash can be used in the assignment, otherwise the column
|
# column, then a hash can be used in the assignment, otherwise the column
|
||||||
|
|
Loading…
Reference in a new issue