cached_uploads 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cached_uploads.rb +25 -5
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25eb30d198519ca87aee08ab105fee301f416b2f
4
- data.tar.gz: d6fbb61e52061258e47f66da6871c062e8aa4c46
3
+ metadata.gz: fde9467cec8d08ea9ae8f1111c8f4fe2fed0bfef
4
+ data.tar.gz: 4a9b1e62c06d090618f95473dd5c5612618d5ab8
5
5
  SHA512:
6
- metadata.gz: 2cfd124c60fe3e437964b38b9309f7a6b12a444e84359aca3bc3c4fb647b71d339dedd382a34551f5109a8c5c8abeb65726197ca6a39f8caa062567332587c7d
7
- data.tar.gz: 3be31bebf1099ba241b2c57dbeb48504725dd415b5be4c9666c367916eb02f58f2dc2404bd1738a38d203dc18ec590968a050c7e1daa96ae1433ae5d5eb2cec9
6
+ metadata.gz: c7df3f750b350485861da5cfea8971627863a7781c374c4b2e7308a8feecffd139919fd27911c5bf90a49e34cf5ac21e328e70a5a15337f4b0102dd97ab07abb
7
+ data.tar.gz: 2ec34cd5dc9c55080da960808afe587dbd67ccc0ec818113d26ebb5b9de59720b44d56d76df0b81cd59b07ee5b9651abeac7b6aa466caa2bd9006c9eeca5f076
@@ -27,6 +27,16 @@ module CachedUploads
27
27
  end
28
28
  end
29
29
 
30
+ def write_hash(file_attr)
31
+ config = self.class.cached_uploads[file_attr.to_sym]
32
+ file = send file_attr
33
+ if file.present?
34
+ file.rewind
35
+ md5 = Digest::MD5.hexdigest(file.read)
36
+ send "#{config[:md5_attr]}=", md5
37
+ end
38
+ end
39
+
30
40
  def write_permanent_file(file_attr)
31
41
  config = self.class.cached_uploads[file_attr.to_sym]
32
42
  uploaded_file = send file_attr
@@ -61,7 +71,7 @@ module CachedUploads
61
71
  config = self.class.cached_uploads[file_attr.to_sym]
62
72
  file = send file_attr
63
73
 
64
- if file.present?
74
+ if file.present?
65
75
  # Read the uploaded file, calc its MD5, and write the MD5 instance variable.
66
76
  file.rewind
67
77
  md5 = Digest::MD5.hexdigest(file.read)
@@ -163,7 +173,9 @@ module CachedUploads
163
173
  # to +"#{file_attr}_ext"+.
164
174
  #
165
175
  # - +md5_attr+: Name of the instance attribute storing the file's MD5 hash. Defaults
166
- # to +"tmp_#{file_attr}_md5"+.
176
+ # to +"#{file_attr}_md5"+. It is often wise to make this attribute a database
177
+ # column. However, if you don't, you still need to define this attribute, so use
178
+ # #attr_accessor.
167
179
  #
168
180
  # - +no_prm:+ If set to true, permanent files won't be written to disk. You might
169
181
  # want to use this if, for example, you're hosting uploaded files on an external
@@ -177,7 +189,7 @@ module CachedUploads
177
189
  tmp_folder_method: "tmp_#{file_attr}_folder",
178
190
  tmp_file_expiration: 48.hours,
179
191
  ext_attr: "#{file_attr}_ext",
180
- md5_attr: "tmp_#{file_attr}_md5"
192
+ md5_attr: "#{file_attr}_md5"
181
193
  )
182
194
 
183
195
  # Initialize the configs hash.
@@ -197,8 +209,11 @@ module CachedUploads
197
209
  end
198
210
  )
199
211
 
200
- # Define the accessor for the temporary file MD5 string.
201
- attr_accessor options[:md5_attr]
212
+ # Define the accessor for the temporary file MD5 string. (Unless it's already
213
+ # defined, e.g. as a database column.)
214
+ unless method_defined? options[:md5_attr]
215
+ attr_accessor options[:md5_attr]
216
+ end
202
217
 
203
218
  # Define the path methods, if given.
204
219
  if options[:folder] and options[:filename]
@@ -241,6 +256,11 @@ module CachedUploads
241
256
  obj.delete_permanent_file file_attr
242
257
  end
243
258
  end
259
+
260
+ # Register the hash writer callback.
261
+ before_save do |obj|
262
+ obj.write_hash file_attr
263
+ end
244
264
  end
245
265
  end
246
266
  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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Colby