cached_uploads 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cached_uploads.rb +21 -18
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f001577d8b2960c0ca257fba9204b6be90b3fcde
4
- data.tar.gz: 66a23a6c52e90bf7c9ddf5e763d37057634e40df
3
+ metadata.gz: 37755d8531f052e5a14e8336dec883291e46921d
4
+ data.tar.gz: e5034fece7ace248880a90234fb09cc87ad52854
5
5
  SHA512:
6
- metadata.gz: 0ebf1726a88388803a99ca7f2de38274f673dbde3bdbe63d913c46bd1a2a01c8b1b4f3765ce8be2452dc14c6b05d22beb7c871f215027c3bc3c09c2bf90f1813
7
- data.tar.gz: 6ed8b66e76b106b191e3dfd45c46a02ba7c4345ce5e5ac1b483d8ee20b5e82ee7689c366f469ef69de1c4c3fcb86a2ae6743dfae0136a42a897c9a1a3df4c6ba
6
+ metadata.gz: 7f4e68a60c40926b88675c1d76ed840f75d7cba545674014ace5651bb4f03e30c45721c62ef8e31834d49167eccc5fbef4f180855d81795e2678621401264d37
7
+ data.tar.gz: bf37b9d143c8ca6417fb05c3bcc4bd38ad098c6558837f20ab072ec50f7fc0aa10702b474824122f106fa586a558597f5384c867189db5f67e585494e792d1a8
@@ -27,13 +27,14 @@ module CachedUploads
27
27
  end
28
28
  end
29
29
 
30
- def write_hash(file_attr)
30
+ def write_permanent_file_md5(file_attr)
31
31
  config = self.class.cached_uploads[file_attr.to_sym]
32
32
  file = send file_attr
33
- if file.present?
33
+ method = "#{config[:prm_md5_attr]}="
34
+ if file.present? and respond_to?(method)
34
35
  file.rewind
35
36
  md5 = Digest::MD5.hexdigest(file.read)
36
- send "#{config[:md5_attr]}=", md5
37
+ send method, md5
37
38
  end
38
39
  end
39
40
 
@@ -52,7 +53,7 @@ module CachedUploads
52
53
  uploaded_file.rewind
53
54
  out_file.write uploaded_file.read
54
55
  end
55
- elsif send(config[:md5_attr]).present?
56
+ elsif send(config[:tmp_md5_attr]).present?
56
57
  # This executes if we've set the temporary file MD5 attribute instead of the file
57
58
  # attribute. This is invoked when the user has submitted invalid data at least once.
58
59
  # In which case we've saved the uploaded data to a tempfile on the server. Now the
@@ -75,7 +76,7 @@ module CachedUploads
75
76
  # Read the uploaded file, calc its MD5, and write the MD5 instance variable.
76
77
  file.rewind
77
78
  md5 = Digest::MD5.hexdigest(file.read)
78
- send "#{config[:md5_attr]}=", md5
79
+ send "#{config[:tmp_md5_attr]}=", md5
79
80
 
80
81
  # Write the temporary file, using its MD5 hash to generate the filename.
81
82
  file.rewind
@@ -173,10 +174,14 @@ module CachedUploads
173
174
  # to +"#{file_attr}_ext"+. CachedUploads does not define this method for you.
174
175
  # Typically, this attribute would be a database column.
175
176
  #
176
- # - +md5_attr+: Name of the instance attribute storing the file's MD5 hash. Defaults
177
- # to +"#{file_attr}_md5"+. It is often wise to make this attribute a database
178
- # column. However, if you don't, you still need to define this attribute, so use
179
- # #attr_accessor.
177
+ # - +prm_md5_attr+: Name of the instance attribute storing the permanent file's MD5
178
+ # hash. Defaults to +"#{file_attr}_md5"+. If this attribute exists, it should be a
179
+ # database column, but it need not exist at all.
180
+ #
181
+ # - +tmp_md5_attr+: Name of the instance attribute storing the temporary file's MD5
182
+ # hash. Defaults to +"tmp_#{file_attr}_md5"+. This attribute is typically not a
183
+ # database column. If you don't define it yourself, CachedUploads will define it
184
+ # for you.
180
185
  #
181
186
  # - +no_prm:+ If set to true, permanent files won't be written to disk. You might
182
187
  # want to use this if, for example, you're hosting uploaded files on an external
@@ -190,7 +195,8 @@ module CachedUploads
190
195
  tmp_folder_method: "tmp_#{file_attr}_folder",
191
196
  tmp_file_expiration: 48.hours,
192
197
  ext_attr: "#{file_attr}_ext",
193
- md5_attr: "#{file_attr}_md5"
198
+ prm_md5_attr: "#{file_attr}_md5",
199
+ tmp_md5_attr: "tmp_#{file_attr}_md5"
194
200
  )
195
201
 
196
202
  # Initialize the configs hash.
@@ -210,13 +216,10 @@ module CachedUploads
210
216
  end
211
217
  )
212
218
 
213
- # Define the accessor for the temporary file MD5 string. (Unless it's already
214
- # defined, e.g. as a database column.)
215
- unless (
216
- method_defined? options[:md5_attr] or
217
- (respond_to? :columns and columns.map { |c| c.name.to_sym }.include?(options[:md5_attr].to_sym))
218
- )
219
- attr_accessor options[:md5_attr]
219
+ # Define the accessor for the temporary file MD5 string. This should never be a
220
+ # database column.
221
+ unless method_defined? options[:tmp_md5_attr]
222
+ attr_accessor options[:tmp_md5_attr]
220
223
  end
221
224
 
222
225
  # Define the path methods, if given.
@@ -263,7 +266,7 @@ module CachedUploads
263
266
 
264
267
  # Register the hash writer callback.
265
268
  before_save do |obj|
266
- obj.write_hash file_attr
269
+ obj.write_permanent_file_md5 file_attr
267
270
  end
268
271
  end
269
272
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_uploads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Colby