Commit 7b0afe61

mo khan <mo@mokhan.ca>
2014-05-31 16:51:41
fix broken tests. move tmp file upload to tmp dir before processing.
1 parent e443a12
Changed files (5)
app
config
initializers
spec
app/services/application/photo_to_jq_json_mapper.rb
@@ -0,0 +1,11 @@
+class PhotoToJQJsonMapper
+  def map_from(photo)
+    {
+      name: photo.read_attribute(:image),
+      url: photo.image.url,
+      thumbnail_url: photo.is_processed? ? photo.image.thumb.url : photo.image.thumb.default_url,
+      delete_url: photo.id,
+      delete_type: "DELETE"
+    }
+  end
+end
app/services/application/upload_photo.rb
@@ -1,37 +1,33 @@
 class UploadPhoto
-  def initialize(message_bus = Spank::IOC.resolve(:message_bus), mapper = PhotoToJQJsonMapper.new)
+  def initialize(message_bus = Spank::IOC.resolve(:message_bus))
     @message_bus = message_bus
-    @mapper = mapper
   end
 
   def run(cake_id, params)
     photo = Creation.find(cake_id).photos.create!(image_processing: true)
+    system "mkdir -p #{Rails.root.join("tmp/uploads/")}"
+    original_path = params[:image].path
+    new_path = "#{Rails.root.join("tmp", "uploads")}/#{SecureRandom.uuid}#{File.extname(params[:image].original_filename)}"
+    FileUtils.mv(original_path, new_path)
     message = {
       cake_id: cake_id,
       photo_id: photo.id,
-      file_path: params[:image].tempfile.path,
+      file_path: new_path,
       original_filename: params[:image].original_filename,
       content_type: params[:image].content_type,
-      headers: params[:image].headers
     }
     @message_bus.publish(:upload_photo, message)
-    map_from(photo)
+    map_from(photo.id, params[:image].original_filename)
   end
 
   private
 
-  def map_from(photo)
-    @mapper.map_from(photo)
-  end
-end
-
-class PhotoToJQJsonMapper
-  def map_from(photo)
+  def map_from(photo_id, name)
     {
-      name: photo.read_attribute(:image),
-      url: photo.image.url,
-      thumbnail_url: photo.is_processed? ? photo.image.thumb.url : photo.image.thumb.default_url,
-      delete_url: photo.id,
+      name: name,
+      url: "/_default.png",
+      thumbnail_url: "/assets/thumb_default.png",
+      delete_url: photo_id,
       delete_type: "DELETE"
     }
   end
app/services/infrastructure/queued_job.rb
@@ -4,7 +4,7 @@ class QueuedJob < Struct.new(:event, :payload)
   end
 
   def error(job, exception)
-    ExceptionNotifier.notify_exception(exception)
+    ExceptionNotifier.notify_exception(exception) unless Rails.env.test?
   end
 
   private
config/initializers/container.rb
@@ -7,6 +7,7 @@ container.register(:message_handler) { |builder| builder.build(ProcessPhoto) }
 container.register(:queue) { |c| Delayed::Job }
 container.register(:message_bus) { |c| c.build(MessageBus) }.as_singleton
 container.register(:twitter_publisher) { |c| c.build(TwitterPublisher) }.as_singleton
-container.register(:cakes) { |builder| Cake }
+container.register(:cakes) { |builder| Creation }
+container.register(:photos) { |builder| Photo }
 
 Spank::IOC.bind_to(container)
spec/controllers/my/photos_controller_spec.rb
@@ -10,11 +10,11 @@ module My
       http_login(user)
     end
 
-    describe :post do
+    describe :create do
       let(:image) { Rack::Test::UploadedFile.new("spec/fixtures/images/gorilla.jpg", "image/jpeg") }
 
       before :each do
-        post :create, :cake_id => cake.id, :photo => { :image => image }
+        xhr :post, :create, cake_id: cake.id, photo: { image: image }
       end
 
       it "returns http success" do
@@ -22,8 +22,12 @@ module My
       end
 
       it "should upload a new photo" do
-        assigns(:photo).should_not be_nil
-        assigns(:photo).image.to_s.should include("gorilla.jpg")
+        json = JSON.parse(response.body)
+        json["files"].first["name"].should_not be_nil
+        json["files"].first["url"].should_not be_nil
+        json["files"].first["thumbnail_url"].should_not be_nil
+        json["files"].first["delete_url"].should_not be_nil
+        json["files"].first["delete_type"].should == "DELETE"
       end
     end