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

Extract private methods out of merge_metadata

This commit is contained in:
Jared Beck 2016-12-13 22:23:30 -05:00
parent 3aafeba1c8
commit 42de9e73e4
2 changed files with 42 additions and 25 deletions

View file

@ -8,7 +8,7 @@ Metrics/CyclomaticComplexity:
Max: 8 # Goal: 6 Max: 8 # Goal: 6
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
Max: 10 # Goal: 7 Max: 9 # Goal: 7
Style/FrozenStringLiteralComment: Style/FrozenStringLiteralComment:
Enabled: false Enabled: false

View file

@ -118,32 +118,49 @@ module PaperTrail
source_version.nil? source_version.nil?
end end
# Updates `data` from the model's `meta` option and from `controller_info`.
# @api private # @api private
def merge_metadata(data) def merge_metadata_into(data)
# First we merge the model-level metadata in `meta`. merge_metadata_from_model_into(data)
@record.paper_trail_options[:meta].each do |k, v| merge_metadata_from_controller_into(data)
data[k] = end
if v.respond_to?(:call)
v.call(@record)
elsif v.is_a?(Symbol) && @record.respond_to?(v, true)
# If it is an attribute that is changing in an existing object,
# be sure to grab the current version.
if @record.has_attribute?(v) &&
attribute_changed_in_latest_version?(v) &&
data[:event] != "create"
attribute_in_previous_version(v)
else
@record.send(v)
end
else
v
end
end
# Second we merge any extra data from the controller (if available). # Updates `data` from `controller_info`.
# @api private
def merge_metadata_from_controller_into(data)
data.merge(PaperTrail.controller_info || {}) data.merge(PaperTrail.controller_info || {})
end end
# Updates `data` from the model's `meta` option.
# @api private
def merge_metadata_from_model_into(data)
@record.paper_trail_options[:meta].each do |k, v|
data[k] = model_metadatum(v, data[:event])
end
end
# Given a `value` from the model's `meta` option, returns an object to be
# persisted. The `value` can be a simple scalar value, but it can also
# be a symbol that names a model method, or even a Proc.
# @api private
def model_metadatum(value, event)
if value.respond_to?(:call)
value.call(@record)
elsif value.is_a?(Symbol) && @record.respond_to?(value, true)
# If it is an attribute that is changing in an existing object,
# be sure to grab the current version.
if event != "create" &&
@record.has_attribute?(value) &&
attribute_changed_in_latest_version?(value)
attribute_in_previous_version(value)
else
@record.send(value)
end
else
value
end
end
# Returns the object (not a Version) as it became next. # Returns the object (not a Version) as it became next.
# NOTE: if self (the item) was not reified from a version, i.e. it is the # NOTE: if self (the item) was not reified from a version, i.e. it is the
# "live" item, we return nil. Perhaps we should return self instead? # "live" item, we return nil. Perhaps we should return self instead?
@ -210,7 +227,7 @@ 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)
merge_metadata(data) merge_metadata_into(data)
end end
def record_destroy def record_destroy
@ -238,7 +255,7 @@ module PaperTrail
whodunnit: PaperTrail.whodunnit whodunnit: PaperTrail.whodunnit
} }
add_transaction_id_to(data) add_transaction_id_to(data)
merge_metadata(data) merge_metadata_into(data)
end end
# Returns a boolean indicating whether to store serialized version diffs # Returns a boolean indicating whether to store serialized version diffs
@ -280,7 +297,7 @@ 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)
merge_metadata(data) merge_metadata_into(data)
end 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