app/controllers/uploader/attachments_controller.rb in rails-uploader-0.1.4 vs app/controllers/uploader/attachments_controller.rb in rails-uploader-0.2.0
- old
+ new
@@ -1,10 +1,11 @@
module Uploader
class AttachmentsController < ActionController::Metal
include AbstractController::Callbacks
before_filter :find_klass
+ before_filter :find_asset, :only => [:destroy]
def create
@asset = @klass.new(params[:asset])
@asset.uploader_create(params, request)
render_resourse(@asset, 201)
@@ -19,31 +20,35 @@
render_json(:files => [])
end
def destroy
- @asset = @klass.find(params[:id])
@asset.uploader_destroy(params, request)
render_resourse(@asset, 200)
end
protected
def find_klass
@klass = Uploader.constantize(params[:klass])
raise ActionController::RoutingError.new("Class not found #{params[:klass]}") if @klass.nil?
end
+
+ def find_asset
+ @asset = @klass.where(:public_token => params[:id]).first
+ raise ActionController::RoutingError.new("Asset not found by guid #{params[:id]}") if @asset.nil?
+ end
def render_resourse(record, status = 200)
if record.errors.empty?
render_json({:files => [record]}, status)
else
render_json(record.errors, 422)
end
end
def render_json(hash_or_object, status = 200)
- ctype = self.env["HTTP_USER_AGENT"].include?('Android') ? 'text/plain' : "application/json"
+ ctype = env["HTTP_USER_AGENT"] && env["HTTP_USER_AGENT"].include?("Android") ? "text/plain" : "application/json"
self.status = status
self.content_type = ctype
self.response_body = hash_or_object.to_json(:root => false)
end