Skip to content

Commit

Permalink
unnest images api from variants and products, use viewable_id instead
Browse files Browse the repository at this point in the history
  • Loading branch information
cmar authored and radar committed May 29, 2012
1 parent 1cc01fd commit 6e3beec
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 37 deletions.
35 changes: 9 additions & 26 deletions api/app/controllers/spree/api/v1/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,27 @@ module Spree
module Api
module V1
class ImagesController < Spree::Api::V1::BaseController
def show
@image = Image.find(params[:id])
end

def create
@image = product_or_variant.images.create!(params[:image])
@image = Image.create(params[:image])
render :show, :status => 201
end

def update
image.update_attributes(params[:image])
@image = Image.find(params[:id])
@image.update_attributes(params[:image])
render :show, :status => 200
end

def destroy
image.destroy
@image = Image.find(params[:id])
@image.destroy
render :text => nil
end

private

def image
@image = product_or_variant.images.find(params[:id])
end

def product_or_variant
return @product_or_variant if @product_or_variant
if params[:product_id]
@product_or_variant = product
else
@product_or_variant = variant
end
end

def variant
Variant.find(params[:variant_id])
end

def product
find_product(params[:product_id]).master
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion api/app/views/spree/api/v1/images/show.rabl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
object @image
attributes *image_attributes
attributes *image_attributes, :viewable_type, :viewable_id
2 changes: 1 addition & 1 deletion api/app/views/spree/api/v1/products/show.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ child :variants_including_master => :variants do
end

child :images => :images do
attributes *image_attributes
extends "spree/api/v1/images/show"
end

child :option_types => :option_types do
Expand Down
4 changes: 2 additions & 2 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
end

resources :variants
resources :images
end

resources :images

resources :variants, :only => [:index] do
resources :images
end

resources :orders do
Expand Down
18 changes: 12 additions & 6 deletions api/spec/controllers/spree/api/v1/images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Spree
render_views

let!(:product) { create(:product) }
let!(:attributes) { [:id, :position, :attachment_content_type,
:attachment_file_name, :type, :attachment_updated_at, :attachment_width,
let!(:attributes) { [:id, :position, :attachment_content_type,
:attachment_file_name, :type, :attachment_updated_at, :attachment_width,
:attachment_height, :alt] }

before do
Expand All @@ -15,15 +15,21 @@ module Spree

it "can upload a new image for a product" do
product.images.count.should == 0
api_post :create, :product_id => product.to_param, :image => { :attachment => upload_image("thinking-cat.jpg") }
api_post :create,
:image => { :attachment => upload_image("thinking-cat.jpg"),
:viewable_type => 'Spree::Product',
:viewable_id => product.id }
response.status.should == 201
json_response.should have_attributes(attributes)
product.images.count.should == 1
end

it "can upload a new image for a variant" do
product.master.images.count.should == 0
api_post :create, :variant_id => product.master.to_param, :image => { :attachment => upload_image("thinking-cat.jpg") }
api_post :create,
:image => { :attachment => upload_image("thinking-cat.jpg"),
:viewable_type => 'Spree::Variant',
:viewable_id => product.master.to_param }
response.status.should == 201
json_response.should have_attributes(attributes)
product.images.count.should == 1
Expand All @@ -34,14 +40,14 @@ module Spree

it "can update image data" do
product_image.position.should == 1
api_post :update, :variant_id => product.master.to_param, :image => { :position => 2 }, :id => product_image.id
api_post :update, :image => { :position => 2 }, :id => product_image.id
response.status.should == 200
json_response.should have_attributes(attributes)
product_image.reload.position.should == 2
end

it "can delete an image" do
api_delete :destroy, :variant_id => product.master.to_param, :id => product_image.id
api_delete :destroy, :id => product_image.id
response.status.should == 200
lambda { product_image.reload }.should raise_error(ActiveRecord::RecordNotFound)
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Image < Asset
validates_attachment_presence :attachment
validate :no_attachment_errors

attr_accessible :alt, :attachment, :position, :viewable_id
attr_accessible :alt, :attachment, :position, :viewable_type, :viewable_id

has_attached_file :attachment,
:styles => { :mini => '48x48>', :small => '100x100>', :product => '240x240>', :large => '600x600>' },
Expand Down

0 comments on commit 6e3beec

Please sign in to comment.