Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unnest images api from variants and products, use viewable_id instead

  • Loading branch information...
commit 6e3beeccf43a1777ffd0b4359b86e308cde43245 1 parent 1cc01fd
Chris Mar cmar authored radar committed
35 api/app/controllers/spree/api/v1/images_controller.rb
View
@@ -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
2  api/app/views/spree/api/v1/images/show.rabl
View
@@ -1,2 +1,2 @@
object @image
-attributes *image_attributes
+attributes *image_attributes, :viewable_type, :viewable_id
2  api/app/views/spree/api/v1/products/show.rabl
View
@@ -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
4 api/config/routes.rb
View
@@ -16,11 +16,11 @@
end
resources :variants
- resources :images
end
+ resources :images
+
resources :variants, :only => [:index] do
- resources :images
end
resources :orders do
18 api/spec/controllers/spree/api/v1/images_controller_spec.rb
View
@@ -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
@@ -15,7 +15,10 @@ 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
@@ -23,7 +26,10 @@ module Spree
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
@@ -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
2  core/app/models/spree/image.rb
View
@@ -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>' },
Please sign in to comment.
Something went wrong with that request. Please try again.