Skip to content

Instantly share code, notes, and snippets.

@roberto
Created February 4, 2010 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roberto/294561 to your computer and use it in GitHub Desktop.
Save roberto/294561 to your computer and use it in GitHub Desktop.
<%= photo_upload_box(@article, :picture) %>
<% javascript_tag do %>
$(document).ready(function() {
$(function() {
button = $("#button_upload_box");
frame = $("#frame_upload_box");
<% if object.new_record? %>
action = '<%= url_for(:controller => 'temp_uploads', :action => 'create') %>';
new AjaxUpload(button.attr('id'), {action: action,
name: 'upload',
responseType: 'json',
autoSubmit: true,
onSubmit: function(file, extension){
if (! (extension && /^(jpg|png|jpeg|gif)$/.test(extension))){
alert("<%= t("upload_box.invalid_extension") %>: ." + extension);
return false; //cancel upload
}
button.val("<%= t("upload_box.loading") %>...");
this.setData({'authenticity_token' : '<%= form_authenticity_token if protect_against_forgery? %>'});
this.disable(); },
onComplete: function(file, response) {
if(response['response'] == 'success'){
$("#<%="#{klass_id}_#{field}"%>").val(response['filename']);
image = frame.find("img");
image.attr('src', response['thumb']);
}else if(response['response'] == 'failure'){
alert(response['message']);
}else{
alert("<%= t("upload_box.error") %>");
}
button.val("<%= t("upload_box.edit") %>");
this.enable()}
});
<% else %>
action = '<%= url_for(object) %>';
new AjaxUpload(button.attr('id'), {action: action,
name: '<%= "#{klass_id}[#{field}]" %>',
autoSubmit: true,
responseType: 'json',
onSubmit: function(file, extension){
if (! (extension && /^(jpg|png|jpeg|gif)$/.test(extension))){
alert("<%= t("upload_box.invalid_extension") %>: ." + extension);
return false; //cancel upload
}
button.val("<%= t("upload_box.sending") %>...");
this.setData({'authenticity_token' : '<%= form_authenticity_token if protect_against_forgery? %>',
'_method': 'put',
'ajaxupload': true});
this.disable(); },
onComplete: function(file, response) {
if(response['response'] == 'success'){
image = frame.find("img");
image.attr('src', response['url']);
$("#destroy_upload_box").show();
}else if(response['response'] == 'failure'){
alert(response['message']) %>");
}else{
alert("<%= t("upload_box.error") %>");
}
button.val("<%= t("upload_box.edit") %>");
this.enable()}
});
<% end %>
});
});
<% end %>
<% attachment = object.send(field) %>
<% default_image_url = default_image_url_for(attachment) %>
<% image_url = image_url_for(object, field, klass_id) %>
<div id="frame_upload_box">
<div class="frame"><%= image_tag image_url %></div>
<% if default_image_url != image_url %>
<%= button_to t("upload_box.edit"), "#", :id => "button_upload_box" %>
<% else %>
<%= button_to t("upload_box.new"), "#", :id => "button_upload_box" %>
<% end %>
<% unless object.new_record? %>
<%= link_to_remote(t("upload_box.destroy"),
:url => {:action => 'update'},
:with => "'ajaxupload=true&_method=put&#{klass_id}[#{field}]=\"\"'",
:html => {:id => "destroy_upload_box", :class => 'small', :style => "#{"display: none" unless attachment.exists?}"},
:success => "$('#destroy_upload_box').hide();
image = $('#frame_upload_box').find('img');
image.attr('src', '#{default_image_url}');",
:failure => "alert('#{t("upload_box.error")}')") %>
<% end %>
</div>
<%= hidden_field(klass_id, field, :value => image_path_from_params(klass_id, field)) if object.new_record? %>
class ArticlesController < ApplicationController
before_filter :load_temp_upload, :only => ["create", "update"]
def update
if params[:ajaxupload]
if @article.update_attributes(:picture => params[:article][:picture])
render(:text => {:response => 'success', :url => @article.picture.url(:thumb)}.to_json)
else
render(:text => {:response => 'failure', :message => @article.errors.full_messages.join('\n')}.to_json)
end
else
#...
end
end
protected
def load_temp_upload
if !params[:article][:picture].blank? && params[:article][:picture].kind_of?(String)
params[:article][:picture] = TempUpload.load(params[:article][:picture])
end
end
end
class TempUpload
attr_reader :path, :filename, :thumb_path, :uploaded_file
def initialize(uploaded_file)
@uploaded_file = uploaded_file
@filename = "#{Time.now.to_i}_#{@uploaded_file.original_filename}".gsub(/[^A-Za-z\d\.\-_]+/, '_') #timestamp to prevent duplicated file names and gsub for paperclip compatibility
@path = File.join(TMP_UPLOAD_FOLDER, self.filename)
@thumb_path = File.join(TMP_UPLOAD_FOLDER, 'thumb' ,self.filename)
end
def save
save_original
save_thumb
end
def save_original
@uploaded_file.binmode if @uploaded_file.respond_to? :binmode
tempfile = @uploaded_file.to_tempfile
tempfile.close
FileUtils.mkdir_p(File.dirname(@path))
FileUtils.mv(tempfile.path, @path)
end
def save_thumb
thumb_temp = Paperclip::Thumbnail.make(@uploaded_file, {:geometry => "60x60"})
FileUtils.mkdir_p(File.dirname(@thumb_path))
FileUtils.mv(thumb_temp.path, @thumb_path)
end
private :save_original, :save_thumb
def url
self.path.gsub(/^(.)*\/public/, '')
end
def thumb_url
self.thumb_path.gsub(/^(.)*\/public/, '')
end
def self.load(filename)
path = File.join(TMP_UPLOAD_FOLDER, filename)
File.open(path) if File.file?(path)
end
end
class TempUploadsController < ApplicationController
def create
if params[:upload].blank?
return render(:text => {:response => 'failure', :message => t('upload_box.no_file')}.to_json)
end
temp_upload = TempUpload.new(params[:upload])
temp_upload.save
render :text => {:response => 'success', :filename => temp_upload.filename, :thumb => temp_upload.thumb_url}.to_json
end
end
module TempUploadsHelper
def photo_upload_box(object, field)
klass_id = @controller.singular_class_name(object)
render :partial => "temp_uploads/photo_upload_box", :locals => {:object => object, :field => field, :klass_id => klass_id}
end
def image_url_for(object, field, klass_id = nil)
attachment = object.send(field)
return attachment.url(:thumb) unless object.new_record?
klass_id ||= @controller.singular_class_name(object)
filename = attachment.original_filename
filename ||= image_path_from_params(klass_id, field)
if !filename.nil? && File.file?(file_path = File.join(TMP_UPLOAD_FOLDER, 'thumb', filename))
file_path.gsub(/^(.)*public/, '')
else
default_image_url_for(attachment)
end
end
def image_path_from_params(klass_id, field)
(params[klass_id][field].respond_to?(:original_filename) ? params[klass_id][field].original_filename : params[klass_id][field]) if params[klass_id]
end
def default_image_url_for(attachment)
attachment.send(:interpolate, attachment.instance_variable_get("@default_url"), :thumb)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment