22# frozen_string_literal: true
33
44require 'rchardet'
5+ require 'zip'
56
67module ActiveAdminImport
78 class Model
89 include ActiveModel ::Model
910 include ActiveModel ::Validations
1011 include ActiveModel ::Validations ::Callbacks
1112
13+ module CONST
14+ ZIP_TYPE = 'application/zip' . freeze
15+ TMP_FILE = 'active-admin-import-unzipped' . freeze
16+ CSV_TYPES = %w(
17+ text/csv
18+ text/x-csv
19+ text/x-comma-separated-values
20+ text/comma-separated-values
21+ application/csv
22+ application/vnd.ms-excel
23+ application/vnd.msexcel
24+ ) . freeze
25+ end
26+
1227 validates :file , presence : {
1328 message : -> ( *_ ) { I18n . t ( 'active_admin_import.no_file_error' ) }
1429 } , unless : -> ( me ) { me . new_record? }
@@ -66,7 +81,7 @@ def persisted?
6681 end
6782
6883 def archive?
69- file_type == 'application/zip'
84+ file_type == CONST :: ZIP_TYPE
7085 end
7186
7287 alias to_hash attributes
@@ -90,24 +105,15 @@ def encode_file
90105
91106 def unzip_file
92107 Zip ::File . open ( file_path ) do |zip_file |
93- self . file = Tempfile . new ( 'active-admin-import-unzipped' )
108+ self . file = Tempfile . new ( CONST :: TMP_FILE )
94109 data = zip_file . entries . select ( &:file? ) . first . get_input_stream . read
95110 file << data
96111 file . close
97112 end
98113 end
99114
100115 def csv_allowed_types
101- [
102- 'text/csv' ,
103- 'text/x-csv' ,
104- 'text/x-comma-separated-values' ,
105- 'text/comma-separated-values' ,
106- 'application/csv' ,
107- 'application/vnd.ms-excel' ,
108- 'application/vnd.msexcel'
109-
110- ]
116+ CONST ::CSV_TYPES
111117 end
112118
113119 def correct_content_type
0 commit comments