Browse Source

Fixes #3514 - csv import example file download (organizations) is empty.

Rolf Schmidt 3 years ago
parent
commit
2d6d15f9be

+ 4 - 1
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)

+ 14 - 14
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)
-
-          header.push key
-        end
+      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
 
       rows = []

+ 1 - 1
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

+ 1 - 1
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
 

+ 1 - 1
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)

+ 12 - 0
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(',')}