diff --git a/app/models/application_model/can_associations.rb b/app/models/application_model/can_associations.rb index 47c8a1812..c615ab4d2 100644 --- a/app/models/application_model/can_associations.rb +++ b/app/models/application_model/can_associations.rb @@ -176,7 +176,7 @@ returns =end - def attributes_with_association_names + def attributes_with_association_names(empty_keys: false) # get relations attributes = attributes_with_association_ids @@ -185,6 +185,9 @@ returns next if association_attributes_ignored.include?(assoc.name) ref = send(assoc.name) + if empty_keys + attributes[assoc.name.to_s] = nil + end next if !ref if ref.respond_to?(:first) diff --git a/app/models/concerns/can_csv_import.rb b/app/models/concerns/can_csv_import.rb index 91f5c735b..b73f69988 100644 --- a/app/models/concerns/can_csv_import.rb +++ b/app/models/concerns/can_csv_import.rb @@ -246,20 +246,20 @@ returns records.each do |record| record_attributes_with_association_names = record.attributes_with_association_names records_attributes_with_association_names.push record_attributes_with_association_names - record_attributes_with_association_names.each do |key, value| - next if value.instance_of?(ActiveSupport::HashWithIndifferentAccess) - next if value.instance_of?(Hash) - next if csv_attributes_ignored&.include?(key.to_sym) - next if key.end_with?('_id') - next if key.end_with?('_ids') - next if key == 'created_by' - next if key == 'updated_by' - next if key == 'created_at' - next if key == 'updated_at' - next if header.include?(key) + end + new.attributes_with_association_names(empty_keys: true).each do |key, value| + next if value.instance_of?(ActiveSupport::HashWithIndifferentAccess) + next if value.instance_of?(Hash) + next if csv_attributes_ignored&.include?(key.to_sym) + next if key.end_with?('_id') + next if key.end_with?('_ids') + next if key == 'created_by' + next if key == 'updated_by' + next if key == 'created_at' + next if key == 'updated_at' + next if header.include?(key) - header.push key - end + header.push key end rows = [] diff --git a/app/models/knowledge_base/answer/translation/content.rb b/app/models/knowledge_base/answer/translation/content.rb index e6aa85418..54de9202e 100644 --- a/app/models/knowledge_base/answer/translation/content.rb +++ b/app/models/knowledge_base/answer/translation/content.rb @@ -27,7 +27,7 @@ class KnowledgeBase::Answer::Translation::Content < ApplicationModel add_attachments_to_attributes(attrs) end - def attributes_with_association_names + def attributes_with_association_names(empty_keys: false) attrs = super add_attachments_to_attributes(attrs) end diff --git a/app/models/taskbar.rb b/app/models/taskbar.rb index 025e7c8cf..6c739c574 100644 --- a/app/models/taskbar.rb +++ b/app/models/taskbar.rb @@ -44,7 +44,7 @@ class Taskbar < ApplicationModel false end - def attributes_with_association_names + def attributes_with_association_names(empty_keys: false) add_attachments_to_attributes(super) end diff --git a/app/models/ticket/article.rb b/app/models/ticket/article.rb index a5f8374f9..a1cc259cd 100644 --- a/app/models/ticket/article.rb +++ b/app/models/ticket/article.rb @@ -270,7 +270,7 @@ returns =end - def attributes_with_association_names + def attributes_with_association_names(empty_keys: false) attributes = super add_attachments_to_attributes(attributes) Ticket::Article.insert_urls(attributes) diff --git a/spec/models/concerns/can_csv_import_examples.rb b/spec/models/concerns/can_csv_import_examples.rb index 3430962aa..21486c966 100644 --- a/spec/models/concerns/can_csv_import_examples.rb +++ b/spec/models/concerns/can_csv_import_examples.rb @@ -1,6 +1,18 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ RSpec.shared_examples 'CanCsvImport' do |unique_attributes: []| + describe '.csv_example' do + before do + described_class.destroy_all + end + + context 'when no data avaiable' do + it 'still returns the header for the csv example' do + expect(described_class.csv_example).to include('id') + end + end + end + describe '.csv_import' do let!(:params) { { string: <<~CSV, parse_params: { col_sep: ',' } } } #{described_class.attribute_names.join(',')}