Commit ae0b2ea1
Changed files (4)
app
config
initializers
app/controllers/my/photos_controller.rb
@@ -2,10 +2,10 @@ module My
class PhotosController < BaseController
before_filter :find_creation
- def initialize(mapper = PhotoToJQJsonMapper.new)
- @mapper = mapper
- super()
- end
+ #def initialize(mapper = PhotoToJQJsonMapper.new)
+ #@mapper = mapper
+ #super()
+ #end
def index
@photos = @cake.photos
@@ -17,18 +17,13 @@ module My
end
def create
- attributes = photo_params
- if params[:photo][:image].class == Array
- raise "heck"
- attributes[:image] = params[:photo][:image].first
- end
-
- @photo = @cake.photos.build(attributes)
- if @photo.save
- render json: { files: [@mapper.map_from(@photo)] }
- else
- render json: [ { error: "oops... we're sorry but we weren't able to upload your photo." } ], status: 304
- end
+ render json: { files: [UploadPhoto.new.run(params[:cake_id], photo_params)] }
+ #@photo = @cake.photos.build(photo_params)
+ #if @photo.save
+ #render json: { files: [@mapper.map_from(@photo)] }
+ #else
+ #render json: [ { error: "oops... we're sorry but we weren't able to upload your photo." } ], status: 304
+ #end
end
def destroy
@@ -53,15 +48,3 @@ module My
end
end
end
-
-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/models/photo.rb
@@ -1,9 +1,9 @@
class Photo < ActiveRecord::Base
belongs_to :creation, :counter_cache => true, touch: true
- validates :image, :presence => true
+ #validates :image, :presence => true
mount_uploader :image, PhotoUploader
- process_in_background :image if Rails.env.test?
- store_in_background :image, UploadImageWorker unless Rails.env.test?
+ #process_in_background :image if Rails.env.test?
+ #store_in_background :image, UploadImageWorker unless Rails.env.test?
def thumb_url
image.thumb.url
app/services/application/upload_photo.rb
@@ -0,0 +1,39 @@
+class UploadPhoto
+ def initialize(message_bus = Spank::IOC.resolve(:message_bus), mapper = PhotoToJQJsonMapper.new)
+ @message_bus = message_bus
+ @mapper = mapper
+ end
+
+ def run(cake_id, params)
+ file_path = params[:image].tempfile.path
+ photo = Creation.find(cake_id).photos.create!(image_tmp: file_path, image_processing: true)
+ message = {
+ cake_id: cake_id,
+ photo_id: photo.id,
+ file_path: file_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)
+ end
+
+ private
+
+ def map_from(photo)
+ @mapper.map_from(photo)
+ end
+end
+
+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
config/initializers/container.rb
@@ -5,5 +5,6 @@ container.register(:message_handler) { |builder| builder.build(PublishCakeToTwit
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 }
Spank::IOC.bind_to(container)