Skip to content

Instantly share code, notes, and snippets.

@verbaleks
Created April 1, 2016 07:50
Show Gist options
  • Save verbaleks/b8eb0364959778e14a28a8e514b801ad to your computer and use it in GitHub Desktop.
Save verbaleks/b8eb0364959778e14a28a8e514b801ad to your computer and use it in GitHub Desktop.
Paperclip handler for Hashie::Mash and Hash (Grape API)
module Paperclip
class HashieMashUploadedFileAdapter < AbstractAdapter
attr_accessor :original_filename # use this accessor if you have paperclip version < 3.3.0
def initialize(target)
@tempfile, @content_type, @size = target.tempfile, target.type, target.tempfile.size
self.original_filename = target.filename
end
end
class HashUploadedFileAdapter < AbstractAdapter
attr_accessor :original_filename # use this accessor if you have paperclip version < 3.3.0
def initialize(target)
@tempfile, @content_type, @size = target[:tempfile], target[:type], target[:tempfile].size
self.original_filename = target[:filename]
end
end
end
Paperclip.io_adapters.register Paperclip::HashieMashUploadedFileAdapter do |target|
target.is_a? Hashie::Mash
end
Paperclip.io_adapters.register Paperclip::HashUploadedFileAdapter do |target|
target.is_a?(Hash) && target[:tempfile].is_a?(Tempfile)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment