public
Last active

methods can convey meaning

  • Download Gist
non-sucky.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
def for(params, *args)
params = Map.for(params)
options = Map.options_for!(args)
key = Array(options[:key] || args).flatten.compact
key = [:upload] if key.empty?
return(
currently_uploaded_file(params, key, options) or
previously_uploaded_file(params, key, options) or
default_uploaded_file(params, key, options)
)
end
def currently_uploaded_file(params, key, options)
upload = params.get(key)
if upload.respond_to?(:read)
tmpdir do |tmp|
original_basename =
[:original_path, :original_filename, :path, :filename].
map{|msg| upload.send(msg) if upload.respond_to?(msg)}.compact.first
basename = cleanname(original_basename)
path = File.join(tmp, basename)
open(path, 'wb'){|fd| fd.write(upload.read)}
upload_cache = UploadCache.new(key, path, options)
params.set(key, upload_cache.io)
return upload_cache
end
end
false
end
def previously_uploaded_file(params, key, options)
cache_key = cache_key_for(key)
upload_cache = params.get(cache_key)
if upload_cache
dirname, basename = File.split(File.expand_path(upload_cache))
relative_dirname = File.basename(dirname)
relative_basename = File.join(relative_dirname, basename)
path = root + '/' + relative_basename
upload_cache = UploadCache.new(key, path, options)
params.set(key, upload_cache.io)
return upload_cache
end
false
end
def default_uploaded_file(params, key, options)
upload_cache = UploadCache.new(key, options)
params.set(key, upload_cache.io) if upload_cache.io
return upload_cache
end
sucky.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def for(params, *args)
params = Map.for(params)
options = Map.options_for!(args)
 
key = Array(options[:key] || args).flatten.compact
key = [:upload] if key.empty?
 
upload = params.get(key)
 
if upload.respond_to?(:read)
tmpdir do |tmp|
original_basename =
[:path, :filename, :original_path, :original_filename].
map{|msg| upload.send(msg) if upload.respond_to?(msg)}.compact.first
basename = cleanname(original_basename)
 
path = File.join(tmp, basename)
open(path, 'wb'){|fd| fd.write(upload.read)}
upload_cache = UploadCache.new(key, path, options)
params.set(key, upload_cache.io)
return upload_cache
end
end
 
cache_key = cache_key_for(key)
upload_cache = params.get(cache_key)
 
if upload_cache
dirname, basename = File.split(upload_cache)
relative_dirname = File.expand_path(File.dirname(dirname))
relative_basename = File.join(relative_dirname, basename)
path = root + '/' + relative_basename
upload_cache = UploadCache.new(key, path, options)
params.set(key, upload_cache.io)
return upload_cache
end
 
upload_cache = UploadCache.new(key, options)
params.set(key, upload_cache.io) if upload_cache.io
return upload_cache
end

I love little well named methods that get called as part of a larger process! Makes shitty stuff like this much easier to figure out, and its easy to test the edge cases. Took me a while to start writing code like non-sucky.rb

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.