echo_uploads 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2871758ae650fff59f9237e880bdd4170788ff9c
4
- data.tar.gz: 7078fba7b15ed162a99eee3f6c78cac77c9972d5
3
+ metadata.gz: 89d8ff5528868bf46e63aba75b9ea6f1d6f7d861
4
+ data.tar.gz: 117384aef09f78edd0dd32ac3e1e22bdca305a42
5
5
  SHA512:
6
- metadata.gz: f2a64cf51e0f552d6adeef3259d2dfa419d87ffc6a5927c4e884fce8f785a72cc6414ad89fce7da41bf7b7de205a4dff3377847020243fd4055fdc839763d2a6
7
- data.tar.gz: c83b65cb78659d9746438edd1947c01ebac9de32aa8ffe678e707d7a24a35d1b02c7d8c66513d4ac9c7ad2131dc0cacc090b06585ea9735a904a0f2f448de7a0
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
- # A file is being uploaded during this request cycle. Further, we have not
12
- # already done the permanent file saving during this request cycle. (It's
13
- # not uncommon for a model to be saved twice in one request. If we ran this
14
- # code twice, we'd have duplicate effort at best and exceptions at worst.)
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
- @echo_uploads_prm_files_saved[attr.to_sym] = file
19
+ @echo_uploads_prm_files_saved[attr.to_sym] = file
17
20
 
18
- if options[:multiple]
19
- metas = send("#{attr}_metadatas")
20
- else
21
- metas = [send("#{attr}_metadata")].compact
22
- end
21
+ if options[:multiple]
22
+ metas = send("#{attr}_metadatas")
23
+ else
24
+ metas = [send("#{attr}_metadata")].compact
25
+ end
23
26
 
24
- # metas is now an array of ::EchoUploads::File instances. The array may
25
- # be empty.
27
+ # metas is now an array of ::EchoUploads::File instances. The array may
28
+ # be empty.
26
29
 
27
- if metas.any?
28
- # Previous permanent file(s) exist. This is a new version being uploaded.
29
- # Delete the old version(s).
30
- metas.each(&:destroy)
31
- end
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
- # No previous permanent files exists. The metas array is currently empty or
34
- # else contains deleted records. We need to rebuild that array by constructing
35
- # (but not yet saving) new EchoUploads::File objects.
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
- if options[:multiple]
38
- mapped_files = send("mapped_#{attr}") ||
39
- raise('echo_uploads called with :multiple, but :map option was missing')
40
- metas = mapped_files.map do |mapped_file|
41
- ::EchoUploads::File.new(
42
- owner: model, temporary: false, file: mapped_file
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
- # metas is still an array of the EchoUploads::File instances. If the array was
60
- # initially empty (meaning no previous permanent file existed), then it has
61
- # since been populated.
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
- metas.each do |meta|
64
- meta.persist! attr, options
66
+ metas.each do |meta|
67
+ meta.persist! attr, options
68
+ end
65
69
  end
66
70
  elsif metas = send("#{attr}_tmp_metadata")
67
- # A file has not been uploaded during this request cycle. However, the
68
- # submitted form "remembered" a temporary metadata record that was previously
69
- # saved.
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
- # Delete any existing metadata record. (It's possible we
72
- # were trying to replace an old version of the file, and there were validation
73
- # errors on the first attempt.)
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
- if options[:multiple]
76
- model.send("#{attr}_metadatas").each(&:destroy)
77
- elsif old = model.send("#{attr}_metadata")
78
- old.destroy
79
- end
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
- # We need not call persist! here, because the file is already persisted. (Nor
82
- # could we call it, because persist! requires an
83
- # ActionDispatch::HTTP::UploadedFile.) Mark the metadata record as permanent
84
- # and set its owner.
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
- if options[:multiple]
93
- send("#{attr}_metadatas=", metas)
94
- else
95
- send("#{attr}_metadata=", metas.first)
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,
@@ -1,3 +1,3 @@
1
1
  module EchoUploads
2
- VERSION = '0.0.18'
2
+ VERSION = '0.0.19'
3
3
  end
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.18
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-01-14 00:00:00.000000000 Z
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types