Skip to content

Instantly share code, notes, and snippets.

@ceneon
Forked from WizardOfOgz/gist:1012107
Created August 13, 2013 15:39
Show Gist options
  • Save ceneon/6222483 to your computer and use it in GitHub Desktop.
Save ceneon/6222483 to your computer and use it in GitHub Desktop.
class Avatar < ActiveRecord::Base
attr_accessor :content_type, :original_filename, :image_data
before_save :decode_base64_image
has_attached_file :image,
PAPERCLIP_CONFIG.merge(
:styles => {
:thumb => '32x32#',
:medium => '64x64#',
:large => '256x256#'
})
protected
def decode_base64_image
if image_data && content_type && original_filename
decoded_data = Base64.decode64(image_data)
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
data.content_type = content_type
data.original_filename = File.basename(original_filename)
self.image = data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment