Skip to content

Commit

Permalink
zones api
Browse files Browse the repository at this point in the history
Merges #1615
  • Loading branch information
cmar authored and radar committed Jun 2, 2012
1 parent 03e272a commit b522703
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
45 changes: 45 additions & 0 deletions api/app/controllers/spree/api/v1/zones_controller.rb
@@ -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
2 changes: 2 additions & 0 deletions api/app/views/spree/api/v1/zones/index.rabl
@@ -0,0 +1,2 @@
collection @zones
extends 'spree/api/v1/zones/show'
6 changes: 6 additions & 0 deletions api/app/views/spree/api/v1/zones/show.rabl
@@ -0,0 +1,6 @@
object @zone
attributes :id, :name, :description

child :zone_members => :zone_members do
attributes :id, :name, :zoneable_type, :zoneable_id
end
1 change: 1 addition & 0 deletions api/config/routes.rb
Expand Up @@ -51,6 +51,7 @@
end
end

resources :zones
resources :countries, :only => [:index, :show]
resources :addresses, :only => [:show, :update]
resources :taxonomies do
Expand Down
54 changes: 54 additions & 0 deletions api/spec/controllers/spree/api/v1/zones_controller_spec.rb
@@ -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 b522703

Please sign in to comment.