lib/paper_trail/events/base.rb in paper_trail-12.1.0 vs lib/paper_trail/events/base.rb in paper_trail-12.2.0

- old
+ new

@@ -114,10 +114,24 @@ # @api private def changes_in_latest_version @changes_in_latest_version ||= load_changes_in_latest_version end + # @api private + def evaluate_only + only = @record.paper_trail_options[:only].dup + # Remove Hash arguments and then evaluate whether the attributes (the + # keys of the hash) should also get pushed into the collection. + only.delete_if do |obj| + obj.is_a?(Hash) && + obj.each { |attr, condition| + only << attr if condition.respond_to?(:call) && condition.call(@record) + } + end + only + end + # An attributed is "ignored" if it is listed in the `:ignore` option # and/or the `:skip` option. Returns true if an ignored attribute has # changed. # # @api private @@ -180,44 +194,45 @@ # @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, false) - else - @record.send(value) - end + metadatum_from_model_method(event, value) else value end end + # The model method can either be an attribute or a non-attribute method. + # + # If it is an attribute that is changing in an existing object, + # be sure to grab the correct version. + # # @api private + def metadatum_from_model_method(event, method) + if event != "create" && + @record.has_attribute?(method) && + attribute_changed_in_latest_version?(method) + attribute_in_previous_version(method, false) + else + @record.send(method) + end + end + + # @api private def notable_changes changes_in_latest_version.delete_if { |k, _v| !notably_changed.include?(k) } end # @api private def notably_changed # Memoized to reduce memory usage @notably_changed ||= begin - only = @record.paper_trail_options[:only].dup - # Remove Hash arguments and then evaluate whether the attributes (the - # keys of the hash) should also get pushed into the collection. - only.delete_if do |obj| - obj.is_a?(Hash) && - obj.each { |attr, condition| - only << attr if condition.respond_to?(:call) && condition.call(@record) - } - end - only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & only) + only = evaluate_only + cani = changed_and_not_ignored + only.empty? ? cani : (cani & only) end end # Returns hash of attributes (with appropriate attributes serialized), # omitting attributes to be skipped.