echo_uploads 0.0.19 → 0.0.20

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: 89d8ff5528868bf46e63aba75b9ea6f1d6f7d861
4
- data.tar.gz: 117384aef09f78edd0dd32ac3e1e22bdca305a42
3
+ metadata.gz: 81819f2de3a42bf1503f4cc4014134dbe8646e74
4
+ data.tar.gz: 2d8ed15098b73f1a06934743ee9c88aad08d75eb
5
5
  SHA512:
6
- metadata.gz: 29f4d670433fc68eb23c8e14c23a11167215952191f6a21d750c54b8cf6543555215866cf24ded923778d02bb94f8c1882dc6d21d96cf20e082df49df741c501
7
- data.tar.gz: 07642f719beb308e0fa74f9b8722b1f4e79e8d3c9965e82c447b62535a607502d7f5064732b25ab9e56e1d1ca1112acf7da43243e945a1730400c0235c3826e2
6
+ metadata.gz: 81c71087ae0363e7ad7d5cde6483fbfdcf127f65c7bc7e4fbea61a1c9d3fa001de9a2ad7f2dc53b55692b5ae25b37617ca3ec733945d7e688d675ec8d4b3bda4
7
+ data.tar.gz: 770e016b4defa22d3fafa82e91a367cfee94a31050c0901cc85d45cf105e1fee29130d5bd6eb792a96e82d6eb3cf9456ab078829295a8b6b7d25541acc9aa691
@@ -64,7 +64,7 @@ module EchoUploads
64
64
  end
65
65
 
66
66
  # Configure and save the metadata object.
67
- self.key = options[:key].call file # Normall, this is .default_key_proc.
67
+ self.key = options[:key].call file # Normally, this is .default_key_proc.
68
68
  self.owner_attr = attr
69
69
  self.original_extension = ::File.extname(file.original_filename)
70
70
  self.original_basename = ::File.basename(file.original_filename, original_extension)
@@ -213,7 +213,7 @@ module EchoUploads
213
213
  end
214
214
 
215
215
  # Define the has_tmp_x? method. Returns true if the record "remembers"
216
- # a a temporary metadata record. (Typically because validation errors caused
216
+ # a temporary metadata record. (Typically because validation errors caused
217
217
  # the form to be redisplayed.)
218
218
  define_method("has_tmp_#{attr}?") do
219
219
  send("#{attr}_tmp_metadata").present?
@@ -225,6 +225,10 @@ module EchoUploads
225
225
  echo_uploads_map_metadata(attr, options, &:read)
226
226
  end
227
227
 
228
+ define_method("write_#{attr}") do |&block|
229
+ echo_uploads_write_prm_file(attr, options, &block)
230
+ end
231
+
228
232
  define_method("#{attr}_size") do
229
233
  echo_uploads_map_metadata(attr, options, &:size)
230
234
  end
@@ -2,6 +2,17 @@ module EchoUploads
2
2
  module PrmFileWriting
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ def echo_uploads_write_prm_file(attr, options)
6
+ meta = send("#{attr}_metadata")
7
+ writable_file = EchoUploads::WritableFile.new meta, options
8
+ if block_given?
9
+ yield writable_file
10
+ writable_file.close
11
+ else
12
+ writable_file
13
+ end
14
+ end
15
+
5
16
  module ClassMethods
6
17
  def echo_uploads_configure_prm_file_writing(attr, options)
7
18
  define_model_callbacks :echo_uploads_write_prm
@@ -1,3 +1,3 @@
1
1
  module EchoUploads
2
- VERSION = '0.0.19'
2
+ VERSION = '0.0.20'
3
3
  end
@@ -0,0 +1,39 @@
1
+ module EchoUploads
2
+ # Used by `EchoUploads::PrmFileWriting#echo_uploads_write_prm_file`.
3
+ class WritableFile
4
+ def close
5
+ # Using ActionDispatch::Http::UploadedFile is ugly. We use it because
6
+ # EchoUploads::File#persist! expects that. In version 1 of this gem, we should
7
+ # refactor. Everything should be more modular in general.
8
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
9
+ tempfile: @tempfile,
10
+ filename: @metadata.original_filename
11
+ )
12
+ ActiveRecord::Base.transaction do
13
+ # Duping the EchoUploads::File and destroy the prior one. This ensure that the
14
+ # old data is cleaned out if necessary.
15
+ new_metadata = @metadata.dup
16
+ @metadata.destroy
17
+ new_metadata.file = uploaded_file
18
+ new_metadata.persist! new_metadata.owner_attr, @options
19
+ @tempfile.close!
20
+ end
21
+ end
22
+
23
+ # Takes an EchoUploads::File.
24
+ def initialize(metadata, options)
25
+ tmp_name = SecureRandom.hex 10
26
+ @tempfile = Tempfile.new 'echo_uploads', Rails.root.join('tmp')
27
+ @metadata = metadata
28
+ @options = options
29
+ end
30
+
31
+ def method_missing(meth, *args)
32
+ if @tempfile.respond_to? meth
33
+ @tempfile.send meth, *args
34
+ else
35
+ super meth, *args
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/echo_uploads.rb CHANGED
@@ -10,6 +10,7 @@ require 'echo_uploads/model'
10
10
  require 'echo_uploads/file'
11
11
  require 'echo_uploads/mapper'
12
12
  require 'echo_uploads/mapped_file'
13
+ require 'echo_uploads/writable_file'
13
14
  require 'echo_uploads/abstract_store'
14
15
  require 'echo_uploads/filesystem_store'
15
16
  require 'echo_uploads/s3_store'
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.19
4
+ version: 0.0.20
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-05-03 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -60,6 +60,7 @@ files:
60
60
  - lib/echo_uploads/tmp_file_writing.rb
61
61
  - lib/echo_uploads/validation.rb
62
62
  - lib/echo_uploads/version.rb
63
+ - lib/echo_uploads/writable_file.rb
63
64
  homepage: https://github.com/jarrett/echo_uploads
64
65
  licenses:
65
66
  - MIT