echo_uploads 0.0.18 → 0.0.19
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/echo_uploads/prm_file_writing.rb +77 -70
- data/lib/echo_uploads/tmp_file_writing.rb +1 -1
- data/lib/echo_uploads/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89d8ff5528868bf46e63aba75b9ea6f1d6f7d861
|
4
|
+
data.tar.gz: 117384aef09f78edd0dd32ac3e1e22bdca305a42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29f4d670433fc68eb23c8e14c23a11167215952191f6a21d750c54b8cf6543555215866cf24ded923778d02bb94f8c1882dc6d21d96cf20e082df49df741c501
|
7
|
+
data.tar.gz: 07642f719beb308e0fa74f9b8722b1f4e79e8d3c9965e82c447b62535a607502d7f5064732b25ab9e56e1d1ca1112acf7da43243e945a1730400c0235c3826e2
|
@@ -4,95 +4,102 @@ module EchoUploads
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
def echo_uploads_configure_prm_file_writing(attr, options)
|
7
|
+
define_model_callbacks :echo_uploads_write_prm
|
8
|
+
|
7
9
|
# Save the file and the metadata after this model saves.
|
8
10
|
after_save do |model|
|
9
11
|
@echo_uploads_prm_files_saved ||= {}
|
10
12
|
if (file = send(attr)).present? and @echo_uploads_prm_files_saved[attr.to_sym] != file
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
run_callbacks :echo_uploads_write_prm do
|
14
|
+
# A file is being uploaded during this request cycle. Further, we have not
|
15
|
+
# already done the permanent file saving during this request cycle. (It's
|
16
|
+
# not uncommon for a model to be saved twice in one request. If we ran this
|
17
|
+
# code twice, we'd have duplicate effort at best and exceptions at worst.)
|
15
18
|
|
16
|
-
|
19
|
+
@echo_uploads_prm_files_saved[attr.to_sym] = file
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
if options[:multiple]
|
22
|
+
metas = send("#{attr}_metadatas")
|
23
|
+
else
|
24
|
+
metas = [send("#{attr}_metadata")].compact
|
25
|
+
end
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
# metas is now an array of ::EchoUploads::File instances. The array may
|
28
|
+
# be empty.
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
if metas.any?
|
31
|
+
# Previous permanent file(s) exist. This is a new version being uploaded.
|
32
|
+
# Delete the old version(s).
|
33
|
+
metas.each(&:destroy)
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
# No previous permanent files exists. The metas array is currently empty or
|
37
|
+
# else contains deleted records. We need to rebuild that array by constructing
|
38
|
+
# (but not yet saving) new EchoUploads::File objects.
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
if options[:multiple]
|
41
|
+
mapped_files = send("mapped_#{attr}") ||
|
42
|
+
raise('echo_uploads called with :multiple, but :map option was missing')
|
43
|
+
metas = mapped_files.map do |mapped_file|
|
44
|
+
::EchoUploads::File.new(
|
45
|
+
owner: model, temporary: false, file: mapped_file
|
46
|
+
)
|
47
|
+
end
|
48
|
+
send("#{attr}_metadatas=", metas)
|
49
|
+
elsif options[:map]
|
50
|
+
mapped_files = send("mapped_#{attr}")
|
51
|
+
metas = [::EchoUploads::File.new(
|
52
|
+
owner: model, temporary: false, file: mapped_files.first
|
53
|
+
)]
|
54
|
+
send("#{attr}_metadata=", metas.first)
|
55
|
+
else
|
56
|
+
metas = [::EchoUploads::File.new(
|
57
|
+
owner: model, temporary: false, file: send(attr)
|
58
|
+
)]
|
59
|
+
send("#{attr}_metadata=", metas.first)
|
44
60
|
end
|
45
|
-
send("#{attr}_metadatas=", metas)
|
46
|
-
elsif options[:map]
|
47
|
-
mapped_files = send("mapped_#{attr}")
|
48
|
-
metas = [::EchoUploads::File.new(
|
49
|
-
owner: model, temporary: false, file: mapped_files.first
|
50
|
-
)]
|
51
|
-
send("#{attr}_metadata=", metas.first)
|
52
|
-
else
|
53
|
-
metas = [::EchoUploads::File.new(
|
54
|
-
owner: model, temporary: false, file: send(attr)
|
55
|
-
)]
|
56
|
-
send("#{attr}_metadata=", metas.first)
|
57
|
-
end
|
58
61
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
# metas is still an array of the EchoUploads::File instances. If the array was
|
63
|
+
# initially empty (meaning no previous permanent file existed), then it has
|
64
|
+
# since been populated.
|
62
65
|
|
63
|
-
|
64
|
-
|
66
|
+
metas.each do |meta|
|
67
|
+
meta.persist! attr, options
|
68
|
+
end
|
65
69
|
end
|
66
70
|
elsif metas = send("#{attr}_tmp_metadata")
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
run_callbacks :echo_uploads_write_prm do
|
72
|
+
# A file has not been uploaded during this request cycle. However, the
|
73
|
+
# submitted form "remembered" a temporary metadata record that was previously
|
74
|
+
# saved.
|
70
75
|
|
71
|
-
|
72
|
-
|
73
|
-
|
76
|
+
# Delete any existing metadata record. (It's possible we
|
77
|
+
# were trying to replace an old version of the file, and there were validation
|
78
|
+
# errors on the first attempt.)
|
74
79
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
+
if options[:multiple]
|
81
|
+
model.send("#{attr}_metadatas").each(&:destroy)
|
82
|
+
elsif old = model.send("#{attr}_metadata")
|
83
|
+
old.destroy
|
84
|
+
end
|
80
85
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
metas.each do |meta|
|
86
|
-
meta.owner = model
|
87
|
-
meta.temporary = false
|
88
|
-
meta.expires_at = nil
|
89
|
-
meta.save!
|
90
|
-
end
|
86
|
+
# We need not call persist! here, because the file is already persisted. (Nor
|
87
|
+
# could we call it, because persist! requires an
|
88
|
+
# ActionDispatch::HTTP::UploadedFile.) Mark the metadata record as permanent
|
89
|
+
# and set its owner.
|
91
90
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
91
|
+
metas.each do |meta|
|
92
|
+
meta.owner = model
|
93
|
+
meta.temporary = false
|
94
|
+
meta.expires_at = nil
|
95
|
+
meta.save!
|
96
|
+
end
|
97
|
+
|
98
|
+
if options[:multiple]
|
99
|
+
send("#{attr}_metadatas=", metas)
|
100
|
+
else
|
101
|
+
send("#{attr}_metadata=", metas.first)
|
102
|
+
end
|
96
103
|
end
|
97
104
|
end
|
98
105
|
end
|
@@ -4,7 +4,7 @@ module EchoUploads
|
|
4
4
|
# This module writes temporary EchoUploads::Files on failed attempts to save the main
|
5
5
|
# ActiveRecord model. Because ActiveRecord wraps save attempts in transactions, we can't
|
6
6
|
# use after_save callbacks. If we tried, the EchoUploads::Files would be lost upon
|
7
|
-
# rollback. Instead, we have to wrap #save, #create, and #update methods so that the
|
7
|
+
# rollback. Instead, we have to wrap the #save, #create, and #update methods so that the
|
8
8
|
# EchoUploads::Files are saved outside the transactions.
|
9
9
|
#
|
10
10
|
# Here is the ancestor chain for ActiveRecord::Base, including this module,
|
data/lib/echo_uploads/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: echo_uploads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarrett Colby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|