From 3060bfa4bd62c46c04e1ed0071ec3d6506111f89 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 28 Sep 2023 10:22:05 -0400 Subject: [PATCH] Extract filename and csv helper methods from `Form::Import` (#26129) --- app/models/form/import.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/models/form/import.rb b/app/models/form/import.rb index 2fc74715b5..29a2975c7b 100644 --- a/app/models/form/import.rb +++ b/app/models/form/import.rb @@ -43,14 +43,14 @@ class Form::Import validate :validate_data def guessed_type - return :muting if csv_data.headers.include?('Hide notifications') - return :following if csv_data.headers.include?('Show boosts') || csv_data.headers.include?('Notify on new posts') || csv_data.headers.include?('Languages') - return :following if data.original_filename&.start_with?('follows') || data.original_filename&.start_with?('following_accounts') - return :blocking if data.original_filename&.start_with?('blocks') || data.original_filename&.start_with?('blocked_accounts') - return :muting if data.original_filename&.start_with?('mutes') || data.original_filename&.start_with?('muted_accounts') - return :domain_blocking if data.original_filename&.start_with?('domain_blocks') || data.original_filename&.start_with?('blocked_domains') - return :bookmarks if data.original_filename&.start_with?('bookmarks') - return :lists if data.original_filename&.start_with?('lists') + return :muting if csv_headers_match?('Hide notifications') + return :following if csv_headers_match?('Show boosts') || csv_headers_match?('Notify on new posts') || csv_headers_match?('Languages') + return :following if file_name_matches?('follows') || file_name_matches?('following_accounts') + return :blocking if file_name_matches?('blocks') || file_name_matches?('blocked_accounts') + return :muting if file_name_matches?('mutes') || file_name_matches?('muted_accounts') + return :domain_blocking if file_name_matches?('domain_blocks') || file_name_matches?('blocked_domains') + return :bookmarks if file_name_matches?('bookmarks') + return :lists if file_name_matches?('lists') end # Whether the uploaded CSV file seems to correspond to a different import type than the one selected @@ -79,6 +79,14 @@ class Form::Import private + def file_name_matches?(string) + data.original_filename&.start_with?(string) + end + + def csv_headers_match?(string) + csv_data.headers.include?(string) + end + def default_csv_headers case type.to_sym when :following, :blocking, :muting