Skip to content

Commit 52352bd

Browse files
Use track_blank_changes
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.
1 parent d3275e6 commit 52352bd

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

lib/mongoid/history/attributes/update.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def attributes
1818
private
1919

2020
def changes_from_parent
21+
track_blank_changes = trackable_class.history_trackable_options[:track_blank_changes]
2122
parent_changes = {}
2223
changes.each do |k, v|
2324
change_value = begin
@@ -26,7 +27,7 @@ def changes_from_parent
2627
elsif trackable_class.tracked_embeds_many?(k)
2728
embeds_many_changes_from_parent(k, v)
2829
elsif trackable_class.tracked?(k, :update)
29-
{ k => format_field(k, v) } unless v.all?(&:blank?)
30+
{ k => format_field(k, v) } unless !track_blank_changes && v.all?(&:blank?)
3031
end
3132
end
3233
parent_changes.merge!(change_value) if change_value.present?

0 commit comments

Comments
 (0)