Skip to content

Commit

Permalink
Use track_blank_changes
Browse files Browse the repository at this point in the history
Use the track_blank_changes option. If it's true, any change reported by the
change method is tracked, even if both the original and new values are blank
(i.e. respond with true to blank?).

The tests now all run successfully (and there are no rubocopy complaints,
although I did increase the maximum class size in commit d3275e6 as
Mongoid::History::Options was exactly at the class size limit). Here are the
complete differences between the initial test output and now:

  $ diff -u testresults/0-baseline.txt testresults/5-use-track_blank_changes-option.txt
  --- testresults/0-baseline.txt	2024-08-05 11:17:55.000000000 -0700
  +++ testresults/5-use-track_blank_changes-option.txt	2024-08-05 20:00:33.000000000 -0700
  @@ -320,8 +320,62 @@
	   should save audit history under relation alias
	 when original and modified value same
	   is expected not to include "emb_ones"
  -    when original and modified values blank
  -      is expected not to include "other_dummy_parent_ids"
  +    when original value blank and modified value nil
  +      when track_blank_changes default
  +        many-to-many field
  +          changes should not include other_dummy_parent_ids
  +        boolean field
  +          changes should not include boolean
  +        empty string field
  +          changes should not include string
  +        all whitespace string field
  +          changes should not include string
  +      when track_blank_changes false
  +        many-to-many field
  +          changes should not include other_dummy_parent_ids
  +        boolean field
  +          changes should not include boolean
  +        empty string field
  +          changes should not include string
  +        all whitespace string field
  +          changes should not include string
  +      when track_blank_changes true
  +        many-to-many field
  +          changes should include other_dummy_parent_ids
  +        boolean field
  +          changes should include boolean
  +        empty string field
  +          changes should include string
  +        all whitespace string field
  +          changes should include string
  +    when original value nil and modified value blank
  +      when track_blank_changes default
  +        many-to-many field
  +          changes should not include other_dummy_parent_ids
  +        boolean field
  +          changes should not include boolean
  +        empty string field
  +          changes should not include string
  +        all whitespace string field
  +          changes should not include string
  +      when track_blank_changes false
  +        many-to-many field
  +          changes should not include other_dummy_parent_ids
  +        boolean field
  +          changes should not include boolean
  +        empty string field
  +          changes should not include string
  +        all whitespace string field
  +          changes should not include string
  +      when track_blank_changes true
  +        many-to-many field
  +          changes should include other_dummy_parent_ids
  +        boolean field
  +          changes should include boolean
  +        empty string field
  +          changes should include string
  +        all whitespace string field
  +          changes should include string

   Mongoid::History::Options
     :if
  @@ -537,6 +591,8 @@
	     is expected to equal false
	   :track_destroy
	     is expected to equal true
  +        :track_blank_changes
  +          is expected to equal true
	   #remove_reserved_fields
	     is expected to eq ["foo"]
	     is expected to eq []
  @@ -824,6 +880,6 @@
	# ./spec/unit/attributes/create_spec.rb:340

   Finished in n minute n seconds (files took n seconds to load)
  -432 examples, 0 failures, 2 pending
  +456 examples, 0 failures, 2 pending

  [Coveralls] Outside the CI environment, not sending data.

24 new tests, including testing when track_blank_changes is true, and still
0 failures.
  • Loading branch information
BrianLMatthews committed Aug 6, 2024
1 parent d3275e6 commit 52352bd
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongoid/history/attributes/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def attributes
private

def changes_from_parent
track_blank_changes = trackable_class.history_trackable_options[:track_blank_changes]
parent_changes = {}
changes.each do |k, v|
change_value = begin
Expand All @@ -26,7 +27,7 @@ def changes_from_parent
elsif trackable_class.tracked_embeds_many?(k)
embeds_many_changes_from_parent(k, v)
elsif trackable_class.tracked?(k, :update)
{ k => format_field(k, v) } unless v.all?(&:blank?)
{ k => format_field(k, v) } unless !track_blank_changes && v.all?(&:blank?)
end
end
parent_changes.merge!(change_value) if change_value.present?
Expand Down

0 comments on commit 52352bd

Please sign in to comment.