Permalink
Browse files

zones api

Merges #1615
  • Loading branch information...
1 parent 8f53742 commit c9f241978f9496171dff5b84d484b60b654ecc80 cmar committed with radar May 31, 2012
@@ -0,0 +1,45 @@
+module Spree
+ module Api
+ module V1
+ class ZonesController < Spree::Api::V1::BaseController
+ def index
+ @zones = Zone.order('name ASC')
+ end
+
+ def show
+ zone
+ end
+
+ def create
+ authorize! :create, Zone
+ @zone = Zone.new(map_nested_attributes_keys(Spree::Zone, params[:zone]))
+ if @zone.save
+ render :show, :status => 201
+ else
+ invalid_resource!(@zone)
+ end
+ end
+
+ def update
+ authorize! :update, Zone
+ if zone.update_attributes(map_nested_attributes_keys(Spree::Zone, params[:zone]))
+ render :show, :status => 200
+ else
+ invalid_resource!(@zone)
+ end
+ end
+
+ def destroy
+ authorize! :delete, Zone
+ zone.destroy
+ render :text => nil, :status => 200
+ end
+
+ private
+ def zone
+ @zone ||= Spree::Zone.find(params[:id])
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,2 @@
+collection @zones
+extends 'spree/api/v1/zones/show'
@@ -0,0 +1,6 @@
+object @zone
+attributes :id, :name, :description
+
+child :zone_members => :zone_members do
+ attributes :id, :name, :zoneable_type, :zoneable_id
+end
View
@@ -51,6 +51,7 @@
end
end
+ resources :zones
resources :countries, :only => [:index, :show]
resources :addresses, :only => [:show, :update]
resources :taxonomies do
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+module Spree
+ describe Api::V1::ZonesController do
+ render_views
+
+ let!(:attributes) { [:id, :name, :zone_members] }
+
+ before do
+ stub_authentication!
+ @zone = create(:zone, :name => 'Europe')
+ end
+
+ it "gets list of zones" do
+ api_get :index
+ json_response.first.should have_attributes(attributes)
+ end
+
+ it "gets a zone" do
+ api_get :show, :id => @zone.id
+ json_response.should have_attributes(attributes)
+ json_response['zone']['name'].should eq @zone.name
+ json_response['zone']['zone_members'].size.should eq @zone.zone_members.count
+ end
+
+ context "as an admin" do
+ sign_in_as_admin!
+
+ it "can create a new zone" do
+ api_post :create, :zone => { :name => "North Pole",
+ :zone_members => [ :zone_member => {
+ :zoneable_id => 1 }] }
+ response.status.should == 201
+ json_response.should have_attributes(attributes)
+ end
+
+ it "updates a zone" do
+ api_put :update, :id => @zone.id,
+ :zone => { :name => "Americas",
+ :zone_members => [ :zone_member => {
+ :zoneable_type => 'Spree::Country',
+ :zoneable_id => 1 }]}
+ response.status.should == 200
+ json_response['zone']['name'].should eq 'Americas'
+ end
+
+ it "can delete a zone" do
+ api_delete :destroy, :id => @zone.id
+ response.status.should == 200
+ lambda { @zone.reload }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+end

0 comments on commit c9f2419

Please sign in to comment.