better_record 0.14.2 → 0.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/better_record/concerns/controllers/uploadable.rb +58 -56
- data/lib/better_record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a2ed3d62e84a39ecb61ad3477e689758e95b22a369ffcc79d6fbcae6499b455
|
4
|
+
data.tar.gz: bcaa6746d5cda1499bc80fb45c72ad20c3c1023711f8e784a719768425ae1340
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a66740d53fbec76869ab77a58764244a80e5f84742a2bf1ac9a5f960cf41120877e1579b5e799217a338545be42160db7d533d872f6feef3d532f662d53a4a0f
|
7
|
+
data.tar.gz: 539875e24030c254395ac8e1b50f3f4951017677eed9a5b6e8ed34eeedc817d89a2db2520d4301a5f1f62c075178bd51a333edee1a5574db25ec49832648b16c
|
@@ -1,68 +1,70 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
4
|
-
|
3
|
+
module BetterRecord
|
4
|
+
module Uploadable
|
5
|
+
extend ActiveSupport::Concern
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def whitelisted_upload_params
|
8
|
+
params.require(:upload).permit(:file, :staff_id)
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
11
|
+
def csv_upload(job, whitelisted_params, upload_key, prefix, job_sym = :staff_id, redirecting = false)
|
12
|
+
p whitelisted_params
|
13
|
+
uploaded = whitelisted_params[upload_key]
|
14
|
+
job_id = whitelisted_params[job_sym]
|
15
|
+
@file_stats = {
|
16
|
+
name: uploaded.original_filename,
|
17
|
+
"mime-type" => uploaded.content_type,
|
18
|
+
size: view_context.number_to_human_size(uploaded.size)
|
19
|
+
}
|
20
|
+
if verify_file(whitelisted_params, upload_key)
|
21
|
+
File.open(Rails.root.join('public', 'import_csv', "#{prefix}_#{Time.now.to_i}#{rand(1000..100000)}.csv"), 'wb') do |file|
|
22
|
+
uploaded = BetterRecord::Encoder.new(uploaded.read).to_utf8
|
23
|
+
file.write(uploaded)
|
24
|
+
job.perform_later file.path, job_id, @file_stats[:name]
|
25
|
+
end
|
26
|
+
if redirecting
|
27
|
+
flash[:success] ||= []
|
28
|
+
flash[:success] << 'File Uploaded'
|
29
|
+
else
|
30
|
+
flash.now[:success] ||= []
|
31
|
+
flash.now[:success] << 'File Uploaded'
|
32
|
+
end
|
33
|
+
return true
|
28
34
|
else
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
msg = [
|
35
|
-
'something went wrong',
|
36
|
-
'Only csv files with the correct headers are supported',
|
37
|
-
"content type: #{whitelisted_params[upload_key].content_type}", "file name: #{whitelisted_params[upload_key].original_filename}"
|
38
|
-
]
|
35
|
+
msg = [
|
36
|
+
'something went wrong',
|
37
|
+
'Only csv files with the correct headers are supported',
|
38
|
+
"content type: #{whitelisted_params[upload_key].content_type}", "file name: #{whitelisted_params[upload_key].original_filename}"
|
39
|
+
]
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
if redirecting
|
42
|
+
flash[:danger] = msg
|
43
|
+
else
|
44
|
+
flash.now[:danger] = msg
|
44
45
|
|
45
|
-
|
46
|
+
render :show
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
|
-
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
private
|
52
|
+
def verify_file(whitelisted_params, upload_key)
|
53
|
+
correct_mime_type(whitelisted_params, upload_key) && /\.csv/ =~ whitelisted_params[upload_key].original_filename
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
56
|
+
def correct_mime_type(whitelisted_params, upload_key)
|
57
|
+
[
|
58
|
+
"text/csv",
|
59
|
+
"text/plain",
|
60
|
+
"application/vnd.ms-excel",
|
61
|
+
"text/x-csv",
|
62
|
+
"application/csv",
|
63
|
+
"application/x-csv",
|
64
|
+
"text/csv",
|
65
|
+
"text/comma-separated-values",
|
66
|
+
"text/x-comma-separated-values"
|
67
|
+
].any? {|mime| mime == whitelisted_params[upload_key].content_type}
|
68
|
+
end
|
69
|
+
end
|
68
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.14.
|
4
|
+
version: 0.14.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sampson Crowley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|