From 076fc17f7c54f8b92d47892ab1ae698f6cc7a623 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Wed, 31 Oct 2018 10:48:55 +0100 Subject: [PATCH 1/2] do not count newline only changes --- app/models/journal_manager.rb | 2 +- spec/models/journal_manager_spec.rb | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb index a1464d0148..ba083c5e0e 100644 --- a/app/models/journal_manager.rb +++ b/app/models/journal_manager.rb @@ -68,7 +68,7 @@ class JournalManager def changed_references(merged_references) merged_references - .select { |_, (old_value, new_value)| old_value.present? && new_value.present? && old_value != new_value } + .select { |_, (old_value, new_value)| old_value.present? && new_value.present? && old_value.strip != new_value.strip } end def to_changes_format(references, key) diff --git a/spec/models/journal_manager_spec.rb b/spec/models/journal_manager_spec.rb index 074012a3be..55fe9bc710 100644 --- a/spec/models/journal_manager_spec.rb +++ b/spec/models/journal_manager_spec.rb @@ -101,7 +101,7 @@ describe JournalManager, type: :model do end end - describe 'self.#update_user_references' do + describe '.update_user_references' do let!(:work_package) { FactoryBot.create :work_package } let!(:doomed_user) { work_package.author } let!(:data1) do @@ -146,4 +146,18 @@ describe JournalManager, type: :model do expect(some_other_journal.reload.user.is_a?(DeletedUser)).to be_falsey end end + + describe '.changes_on_association' do + context 'with one of the values having a newline' do + let(:current) { { id: 2, value: 'some value', custom_field_id: 123 }.with_indifferent_access } + let(:predecessor) { { id: 1, value: "some value\n", custom_field_id: 123 }.with_indifferent_access } + + it 'does not identify a change' do + changes = JournalManager.changes_on_association([current], [predecessor], 'custom_fields', :custom_field_id, :value) + + expect(changes) + .to be_empty + end + end + end end From 060e2ea6928f5d0d2674ea658d5578819f7888f5 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Wed, 31 Oct 2018 10:49:11 +0100 Subject: [PATCH 2/2] linting --- app/models/custom_value/string_strategy.rb | 3 +-- app/models/journal_manager.rb | 7 ++++--- lib/plugins/acts_as_journalized/lib/journal_changes.rb | 7 ++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/models/custom_value/string_strategy.rb b/app/models/custom_value/string_strategy.rb index 40378cf064..9538f098a4 100644 --- a/app/models/custom_value/string_strategy.rb +++ b/app/models/custom_value/string_strategy.rb @@ -32,6 +32,5 @@ class CustomValue::StringStrategy < CustomValue::FormatStrategy value end - def validate_type_of_value - end + def validate_type_of_value; end end diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb index ba083c5e0e..26ba81147c 100644 --- a/app/models/journal_manager.rb +++ b/app/models/journal_manager.rb @@ -250,7 +250,8 @@ class JournalManager def self.create_journal(journable, journal_attributes, user = User.current, notes = '') type = base_class(journable.class) - extended_journal_attributes = journal_attributes.merge(journable_type: type.to_s) + extended_journal_attributes = journal_attributes + .merge(journable_type: type.to_s) .merge(notes: notes) .except(:details) .except(:id) @@ -320,9 +321,9 @@ class JournalManager end def self.normalize_newlines(data) - data.each_with_object({}) { |e, h| + data.each_with_object({}) do |e, h| h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1]) - } + end end def self.with_send_notifications(send_notifications, &block) diff --git a/lib/plugins/acts_as_journalized/lib/journal_changes.rb b/lib/plugins/acts_as_journalized/lib/journal_changes.rb index 3d13fd94e6..493a2baad7 100644 --- a/lib/plugins/acts_as_journalized/lib/journal_changes.rb +++ b/lib/plugins/acts_as_journalized/lib/journal_changes.rb @@ -36,12 +36,13 @@ module JournalChanges @changes = HashWithIndifferentAccess.new if predecessor.nil? - @changes = data.journaled_attributes + @changes = data + .journaled_attributes .reject { |_, new_value| new_value.nil? } - .inject({}) { |result, (attribute, new_value)| + .inject({}) do |result, (attribute, new_value)| result[attribute] = [nil, new_value] result - } + end else normalized_new_data = JournalManager.normalize_newlines(data.journaled_attributes) normalized_old_data = JournalManager.normalize_newlines(predecessor.data.journaled_attributes)