Skip to content

Instantly share code, notes, and snippets.

@ewoodh2o
Forked from akasper/size_upload.rb
Created May 6, 2011 20:19
Show Gist options
  • Save ewoodh2o/959710 to your computer and use it in GitHub Desktop.
Save ewoodh2o/959710 to your computer and use it in GitHub Desktop.
A method for sizing an upload. Response to https://gist.github.com/959582
SIZING_METHODS = {:enable_images? => :resize, :enable_videos? => :encode}
def size_upload
result = {:export => {use: []}}
return result unless self.uploads_enabled?
['', '_thumb'].each do |insert|
w = "upload#{insert}_width".to_sym
w? = "#{w}?".to_sym
h = "upload#{insert}_height".to_sym
h? = "#{h}?".to_sym
if self.send(w?) && self.send(h?)
SIZING_METHODS.each do |method, result_key|
if self.send(method)
result["#{result_key}#{insert}".to_sym] = {
width: self.send(w),
height: self.send(h)
}
end
end
end
SIZING_METHODS.each { |method, pushed_value| result[:export][:use].push(pushed_value.to_s).push("#{pushed_value}_thumb") if self.send(method) }
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment