Commit 03e38089
Changed files (2)
app
controllers
models
app/controllers/photos_controller.rb
@@ -7,7 +7,7 @@ class PhotosController < ApplicationController
@photos = @creation.photos
respond_to do |format|
format.html # index.html.erb
- format.json { render json: { files: @photos.map { |photo| photo.to_jq_upload } } }
+ format.json { render json: { files: @photos.map { |photo| PhotoToJQJsonMapper.new.map_from(photo) } } }
end
end
@@ -22,13 +22,15 @@ class PhotosController < ApplicationController
def create
attributes = photo_params
- attributes[:image] = params[:photo][:image].first if params[:photo][:image].class == Array
+ if params[:photo][:image].class == Array
+ attributes[:image] = params[:photo][:image].first
+ end
@photo = @creation.photos.build(attributes)
if @photo.save
respond_to do |format|
- format.html { render :json => {files: [@photo.to_jq_upload]}.to_json, :content_type => 'text/html', :layout => false }
- format.json { render :json => {files: [@photo.to_jq_upload]}.to_json }
+ format.html { render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json, :content_type => 'text/html', :layout => false }
+ format.json { render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json }
end
else
render :json => [{:error => "oops... we're sorry but we weren't able to upload your photo."}], :status => 304
@@ -39,7 +41,7 @@ class PhotosController < ApplicationController
@photo = @creation.photos.find(params[:id])
if @photo.destroy
@creation.touch
- render :json => {files: [@photo.to_jq_upload]}.to_json
+ render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json
else
render :json => [{:error => "could not remove the photo"}], :status => 304
end
@@ -55,4 +57,16 @@ class PhotosController < ApplicationController
def photo_params
params.require(:photo).permit(:image)
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
end
app/models/photo.rb
@@ -5,16 +5,6 @@ class Photo < ActiveRecord::Base
process_in_background :image if Rails.env.test?
store_in_background :image, UploadImageWorker unless Rails.env.test?
- def to_jq_upload
- {
- :name => read_attribute(:image),
- :url => image.url,
- :thumbnail_url => is_processed? ? image.thumb.url : image.thumb.default_url,
- :delete_url => id,
- :delete_type => "DELETE"
- }
- end
-
def thumb_url
image.thumb.url
end
@@ -23,8 +13,6 @@ class Photo < ActiveRecord::Base
creation.watermark
end
- private
-
def is_processed?
self.image_processing == nil
end