Merge pull request #6798 from opf/fix/newlines_in_cf_journals

Fix/newlines in cf journals

[ci skip]
pull/6801/head
Oliver Günther 6 years ago committed by GitHub
commit df8ec0f6e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/models/custom_value/string_strategy.rb
  2. 9
      app/models/journal_manager.rb
  3. 7
      lib/plugins/acts_as_journalized/lib/journal_changes.rb
  4. 16
      spec/models/journal_manager_spec.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

@ -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)
@ -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)

@ -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)

@ -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

Loading…
Cancel
Save