Skip to content

Instantly share code, notes, and snippets.

@ScotterC
Created June 7, 2013 13:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScotterC/5729366 to your computer and use it in GitHub Desktop.
Save ScotterC/5729366 to your computer and use it in GitHub Desktop.
Basic mockup of combining Filepicker.io with Paperclip & Delayed Paperclip
View
<%= form_for image do |f| %>
<%= link_to "Pick File", "#", class: 'filepicker' %>
<%= f.hidden_field :remote_url %>
<%= f.hidden_field :filepicker_url %>
<%= f.hidden_field :attachment_file_size %>
<%= f.hidden_field :attachment_file_name %>
<%= f.hidden_field :attachment_content_type %>
<% end %>
Coffeescript
# Load filepicker js and set key
picker: =>
filepicker.pickAndStore
mimetypes: "image/*"
,
location: "S3"
path: "/tmp/"
access: 'public'
, (fpfile) =>
@handleImage fpfile
handleImage: (fpfile) ->
s3_prefix = "https://s3.amazonaws.com/#{bucket}/"
s3_url = s3_prefix + fpfile[0].key
file = fpfile[0]
field_prefix = @getPrefix()
# Fill in image hidded in puts
$("#image_remote_url").val s3_url
$("#image_filepicker_url").val file.url
$("#image_attachment_file_size").val file.size
$("#image_attachment_file_name").val file.filename
$("#image_attachment_content_type").val file.mimetype
class Image
has_attached_file :attachment
process_in_background :attachment, if: :remote_url_not_present?
after_commit :update_image_from_remote
def update_image_from_remote
return unless remote_url
# Saving file name because assigning attachment overwrites it
filename = self.attachment_file_name
self.attachment = URI.parse(URI::encode(self.remote_url))
self.attachment_file_name = filename
# Run paperclip callbacks to process image
# and enqueue manually
self.attachment.save_with_prepare_enqueueing
self.enqueue_delayed_processing
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment