Skip to content

Commit

Permalink
add update variant api.
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Aug 1, 2012
1 parent 8fe62b0 commit 84532aa
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 2 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/v1/product_variants_controller.rb
@@ -0,0 +1,13 @@
# encoding: utf-8
module Api::V1
class ProductVariantsController < AppController
doorkeeper_for :update, scopes: [:write_products], unless: lambda { @api_client }

def update
@variant = shop.variants.find(params[:id])
@variant.update_attributes params[:variant].except(:id)
render :show
end

end
end
2 changes: 2 additions & 0 deletions app/views/api/v1/product_variants/show.rabl
@@ -0,0 +1,2 @@
object @variant => :variant
attributes :id, :product_id, :price, :compare_at_price, :weight, :sku, :position, :option1, :option2, :option3, :requires_shipping, :inventory_quantity, :inventory_management, :inventory_policy, :created_at, :updated_at
2 changes: 1 addition & 1 deletion app/views/api/v1/products/index.rabl
@@ -1,2 +1,2 @@
collection @products, root: :products, object_root: false
attributes :id, :title, :body_html, :handle, :product_type, :vendor, :created_at, :updated_at
extends "api/v1/products/show"
6 changes: 6 additions & 0 deletions app/views/api/v1/products/show.rabl
@@ -0,0 +1,6 @@
object @product
attributes :id, :title, :body_html, :handle, :product_type, :vendor, :created_at, :updated_at

child :variants => :variants do
extends "api/v1/product_variants/show"
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -9,6 +9,7 @@
get '/shop' , to: "shops#show"
post '/themes/install', to: 'themes#install'
get '/products' , to: "products#index"
put '/variants/:id' , to: "product_variants#update"
get '/orders' , to: "orders#index"
get '/orders/:id' , to: "orders#show"
post '/webhooks' , to: "webhooks#create"
Expand Down
70 changes: 70 additions & 0 deletions spec/controllers/api/v1/product_variants_controller_spec.rb
@@ -0,0 +1,70 @@
#encoding: utf-8
require 'spec_helper'

describe Api::V1::ProductVariantsController do

let(:shop) { Factory(:user).shop }

let(:iphone4) { Factory :iphone4, shop: shop }

let(:variant) { iphone4.variants.first }

let(:body) { {id: variant.id, price: 45 }} # modify to

before :each do
request.host = "#{shop.primary_domain.host}"
end

context '#update' do

before { [iphone4] }

let(:application) { Factory :express_application } # OAuth application

context 'with scopes' do # 资源访问范围匹配

let(:token) { Factory :access_token, application: application, resource_owner_id: shop.id, scopes: "write_products" }

it 'should be success' do
put :update, id: variant.id, variant: body, format: :json, access_token: token.token
response.should be_success
json = JSON(response.body)['variant']
pattern = {
id: variant.id,
product_id: iphone4.id,
price: 45.0,
compare_at_price: 3500.0,
weight: 2.9,
sku: "APPLE1000",
position: 1,
option1: "默认标题",
option2: nil,
option3: nil,
requires_shipping: true,
inventory_quantity: nil,
inventory_management: nil,
inventory_policy: "deny",
created_at: WILDCARD_MATCHER,
updated_at: WILDCARD_MATCHER
}
json.should match_json_expression(pattern)
end

end

context 'without scopes' do # 资源访问范围不匹配

let(:token) { Factory :access_token, application: application, resource_owner_id: shop.id }

it 'should be fail' do # 认证失败
put :update, id: variant.id, variant: body, format: :json, access_token: token.token
response.should_not be_success
response.status.should eql 401
end

end


end

end
20 changes: 19 additions & 1 deletion spec/controllers/api/v1/products_controller_spec.rb
Expand Up @@ -40,7 +40,25 @@
product_type: "手机",
vendor: "Apple",
created_at: WILDCARD_MATCHER,
updated_at: WILDCARD_MATCHER
updated_at: WILDCARD_MATCHER,
variants: [{
id: 1,
product_id: 1,
price: 3000.0,
compare_at_price: 3500.0,
weight: 2.9,
sku: "APPLE1000",
position: 1,
option1: "默认标题",
option2: nil,
option3: nil,
requires_shipping: true,
inventory_quantity: nil,
inventory_management: nil,
inventory_policy: "deny",
created_at: WILDCARD_MATCHER,
updated_at: WILDCARD_MATCHER
}]
}
product_json.should match_json_expression(pattern)
end
Expand Down

0 comments on commit 84532aa

Please sign in to comment.