spree_api 1.1.1 → 1.1.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -59,7 +59,7 @@ module Spree
59
59
 
60
60
  def find_product(id)
61
61
  begin
62
- product_scope.find_by_permalink!(id)
62
+ product_scope.find_by_permalink!(id.to_s)
63
63
  rescue ActiveRecord::RecordNotFound
64
64
  product_scope.find(id)
65
65
  end
@@ -68,9 +68,13 @@ module Spree
68
68
  def product_scope
69
69
  if current_api_user.has_role?("admin")
70
70
  scope = Product
71
+ unless params[:show_deleted]
72
+ scope = scope.not_deleted
73
+ end
71
74
  else
72
75
  scope = Product.active
73
76
  end
77
+
74
78
  scope.includes(:master)
75
79
  end
76
80
 
@@ -3,6 +3,7 @@ module Spree
3
3
  module V1
4
4
  class OrdersController < Spree::Api::V1::BaseController
5
5
  before_filter :map_nested_attributes, :only => [:create, :update]
6
+ before_filter :authorize_read!, :except => [:index, :search, :create]
6
7
 
7
8
  def index
8
9
  # should probably look at turning this into a CanCan step
@@ -11,11 +12,10 @@ module Spree
11
12
  end
12
13
 
13
14
  def show
14
- authorize! :read, order
15
15
  end
16
16
 
17
17
  def search
18
- @orders = Order.search(params[:q]).result.page(params[:page])
18
+ @orders = Order.ransack(params[:q]).result.page(params[:page])
19
19
  render :index
20
20
  end
21
21
 
@@ -55,6 +55,13 @@ module Spree
55
55
  render :show
56
56
  end
57
57
 
58
+ def empty
59
+ authorize! :read, order
60
+ order.line_items.destroy_all
61
+ order.update!
62
+ render :text => nil, :status => 200
63
+ end
64
+
58
65
  private
59
66
 
60
67
  def map_nested_attributes
@@ -72,6 +79,10 @@ module Spree
72
79
  render :could_not_transition, :status => 422
73
80
  end
74
81
  end
82
+
83
+ def authorize_read!
84
+ authorize! :read, order
85
+ end
75
86
  end
76
87
  end
77
88
  end
@@ -7,7 +7,7 @@ module Spree
7
7
  end
8
8
 
9
9
  def search
10
- @products = product_scope.search(params[:q]).result.page(params[:page])
10
+ @products = product_scope.ransack(params[:q]).result.page(params[:page])
11
11
  render :index
12
12
  end
13
13
 
@@ -6,24 +6,29 @@ module Spree
6
6
  before_filter :find_and_update_shipment, :only => [:ship, :ready]
7
7
 
8
8
  def ready
9
- @shipment.ready!
9
+ unless @shipment.ready?
10
+ @shipment.ready!
11
+ end
10
12
  render :show
11
13
  end
12
14
 
13
15
  def ship
14
- @shipment.ship!
16
+ unless @shipment.shipped?
17
+ @shipment.ship!
18
+ end
15
19
  render :show
16
20
  end
17
21
 
18
22
  private
19
23
 
20
24
  def find_order
21
- @order = Order.find_by_number!(params[:order_id])
25
+ @order = Spree::Order.find_by_number!(params[:order_id])
22
26
  end
23
27
 
24
28
  def find_and_update_shipment
25
29
  @shipment = @order.shipments.find_by_number!(params[:id])
26
30
  @shipment.update_attributes(params[:shipment])
31
+ @shipment.reload
27
32
  end
28
33
  end
29
34
  end
@@ -0,0 +1,45 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ZonesController < Spree::Api::V1::BaseController
5
+ def index
6
+ @zones = Zone.order('name ASC')
7
+ end
8
+
9
+ def show
10
+ zone
11
+ end
12
+
13
+ def create
14
+ authorize! :create, Zone
15
+ @zone = Zone.new(map_nested_attributes_keys(Spree::Zone, params[:zone]))
16
+ if @zone.save
17
+ render :show, :status => 201
18
+ else
19
+ invalid_resource!(@zone)
20
+ end
21
+ end
22
+
23
+ def update
24
+ authorize! :update, Zone
25
+ if zone.update_attributes(map_nested_attributes_keys(Spree::Zone, params[:zone]))
26
+ render :show, :status => 200
27
+ else
28
+ invalid_resource!(@zone)
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ authorize! :delete, Zone
34
+ zone.destroy
35
+ render :text => nil, :status => 200
36
+ end
37
+
38
+ private
39
+ def zone
40
+ @zone ||= Spree::Zone.find(params[:id])
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -16,6 +16,10 @@ module Spree
16
16
  :count_on_hand, :meta_description, :meta_keywords, :taxon_ids]
17
17
  end
18
18
 
19
+ def product_property_attributes
20
+ [:id, :product_id, :property_id, :value, :property_name]
21
+ end
22
+
19
23
  def variant_attributes
20
24
  [:id, :name, :count_on_hand, :sku, :price, :weight, :height, :width, :depth, :is_master, :cost_price, :permalink]
21
25
  end
@@ -19,3 +19,7 @@ child :option_types => :option_types do
19
19
  attributes *option_value_attributes
20
20
  end
21
21
  end
22
+
23
+ child :product_properties => :product_properties do
24
+ attributes *product_property_attributes
25
+ end
@@ -0,0 +1,2 @@
1
+ collection @zones
2
+ extends 'spree/api/v1/zones/show'
@@ -0,0 +1,6 @@
1
+ object @zone
2
+ attributes :id, :name, :description
3
+
4
+ child :zone_members => :zone_members do
5
+ attributes :id, :name, :zoneable_type, :zoneable_id
6
+ end
data/config/routes.rb CHANGED
@@ -31,6 +31,7 @@ Spree::Core::Engine.routes.prepend do
31
31
  put :address
32
32
  put :delivery
33
33
  put :cancel
34
+ put :empty
34
35
  end
35
36
 
36
37
  resources :line_items
@@ -51,6 +52,7 @@ Spree::Core::Engine.routes.prepend do
51
52
  end
52
53
  end
53
54
 
55
+ resources :zones
54
56
  resources :countries, :only => [:index, :show]
55
57
  resources :addresses, :only => [:show, :update]
56
58
  resources :taxonomies do
data/script/rails CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
3
 
4
- ENGINE_PATH = File.expand_path('../..', __FILE__)
5
- load File.expand_path('../../spec/dummy/script/rails', __FILE__)
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/spree/api/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'
@@ -34,6 +34,23 @@ module Spree
34
34
  assert_unauthorized!
35
35
  end
36
36
 
37
+ it "cannot cancel an order that doesn't belong to them" do
38
+ order.update_attribute(:completed_at, Time.now)
39
+ order.update_attribute(:shipment_state, "ready")
40
+ api_put :cancel, :id => order.to_param
41
+ assert_unauthorized!
42
+ end
43
+
44
+ it "cannot add address information to an order that doesn't belong to them" do
45
+ api_put :address, :id => order.to_param
46
+ assert_unauthorized!
47
+ end
48
+
49
+ it "cannot change delivery information on an order that doesn't belong to them" do
50
+ api_put :delivery, :id => order.to_param
51
+ assert_unauthorized!
52
+ end
53
+
37
54
  it "can create an order" do
38
55
  variant = create(:variant)
39
56
  api_post :create, :order => { :line_items => [{ :variant_id => variant.to_param, :quantity => 5 }] }
@@ -47,6 +64,7 @@ module Spree
47
64
 
48
65
  context "working with an order" do
49
66
  before do
67
+ Order.any_instance.stub :user => current_api_user
50
68
  create(:payment_method)
51
69
  order.next # Switch from cart to address
52
70
  order.ship_address.should be_nil
@@ -128,6 +146,12 @@ module Spree
128
146
  json_response["errors"].should include("Invalid shipping method specified.")
129
147
  end
130
148
  end
149
+
150
+ it "can empty an order" do
151
+ api_put :empty, :id => order.to_param
152
+ response.status.should == 200
153
+ order.reload.line_items.should be_empty
154
+ end
131
155
  end
132
156
  end
133
157
 
@@ -48,6 +48,7 @@ module Spree
48
48
 
49
49
  it "gets a single product" do
50
50
  product.master.images.create!(:attachment => image("thinking-cat.jpg"))
51
+ product.set_property("spree", "rocks")
51
52
  api_get :show, :id => product.to_param
52
53
  json_response.should have_attributes(attributes)
53
54
  product_json = json_response["product"]
@@ -60,6 +61,10 @@ module Spree
60
61
  :attachment_width,
61
62
  :attachment_height,
62
63
  :attachment_content_type])
64
+
65
+ product_json["product_properties"].first.should have_attributes([:value,
66
+ :product_id,
67
+ :property_name])
63
68
  end
64
69
 
65
70
 
@@ -127,6 +132,23 @@ module Spree
127
132
  json_response["pages"].should == 1
128
133
  end
129
134
 
135
+ # Regression test for #1626
136
+ context "deleted products" do
137
+ before do
138
+ create(:product, :deleted_at => Time.now)
139
+ end
140
+
141
+ it "does not include deleted products" do
142
+ api_get :index
143
+ json_response["products"].count.should == 2
144
+ end
145
+
146
+ it "can include deleted products" do
147
+ api_get :index, :show_deleted => 1
148
+ json_response["products"].count.should == 3
149
+ end
150
+ end
151
+
130
152
  it "can create a new product" do
131
153
  api_post :create, :product => { :name => "The Other Product",
132
154
  :price => 19.99 }
@@ -5,7 +5,7 @@ describe Spree::Api::V1::ShipmentsController do
5
5
  let!(:attributes) { [:id, :tracking, :number, :cost, :shipped_at] }
6
6
 
7
7
  before do
8
- shipment.order.payment_state == 'paid'
8
+ Spree::Order.any_instance.stub(:paid? => true)
9
9
  stub_authentication!
10
10
  end
11
11
 
@@ -16,12 +16,13 @@ describe Spree::Api::V1::ShipmentsController do
16
16
  api_put :ready
17
17
  json_response.should have_attributes(attributes)
18
18
  json_response["shipment"]["state"].should == "ready"
19
+ shipment.reload.state.should == "ready"
19
20
  end
20
21
 
21
22
  context "can transition a shipment from ready to ship" do
22
23
  before do
23
- shipment.order.update_attribute(:payment_state, 'paid')
24
24
  shipment.update!(shipment.order)
25
+ shipment.state.should == "ready"
25
26
  end
26
27
 
27
28
  it "can transition a shipment from ready to ship" do
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::ZonesController do
5
+ render_views
6
+
7
+ let!(:attributes) { [:id, :name, :zone_members] }
8
+
9
+ before do
10
+ stub_authentication!
11
+ @zone = create(:zone, :name => 'Europe')
12
+ end
13
+
14
+ it "gets list of zones" do
15
+ api_get :index
16
+ json_response.first.should have_attributes(attributes)
17
+ end
18
+
19
+ it "gets a zone" do
20
+ api_get :show, :id => @zone.id
21
+ json_response.should have_attributes(attributes)
22
+ json_response['zone']['name'].should eq @zone.name
23
+ json_response['zone']['zone_members'].size.should eq @zone.zone_members.count
24
+ end
25
+
26
+ context "as an admin" do
27
+ sign_in_as_admin!
28
+
29
+ it "can create a new zone" do
30
+ api_post :create, :zone => { :name => "North Pole",
31
+ :zone_members => [ :zone_member => {
32
+ :zoneable_id => 1 }] }
33
+ response.status.should == 201
34
+ json_response.should have_attributes(attributes)
35
+ end
36
+
37
+ it "updates a zone" do
38
+ api_put :update, :id => @zone.id,
39
+ :zone => { :name => "Americas",
40
+ :zone_members => [ :zone_member => {
41
+ :zoneable_type => 'Spree::Country',
42
+ :zoneable_id => 1 }]}
43
+ response.status.should == 200
44
+ json_response['zone']['name'].should eq 'Americas'
45
+ end
46
+
47
+ it "can delete a zone" do
48
+ api_delete :destroy, :id => @zone.id
49
+ response.status.should == 200
50
+ lambda { @zone.reload }.should raise_error(ActiveRecord::RecordNotFound)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -5,14 +5,14 @@ module Spree
5
5
  let(:user) { stub_model(User) }
6
6
 
7
7
  it 'can build an order from API parameters' do
8
-
9
- Spree::Variant.should_receive(:find).and_return(stub_model(Variant, :id => 1))
10
- order = Order.build_from_api(user, { :line_items_attributes => [{ :variant_id => 1, :quantity => 5 }]})
8
+ product = Spree::Product.create!(:name => 'Test', :sku => 'TEST-1', :price => 33.22)
9
+ variant_id = product.master.id
10
+ order = Order.build_from_api(user, { :line_items_attributes => [{ :variant_id => variant_id, :quantity => 5 }]})
11
11
 
12
12
  order.user.should == user
13
13
  line_item = order.line_items.first
14
14
  line_item.quantity.should == 5
15
- line_item.variant_id.should == 1
15
+ line_item.variant_id.should == variant_id
16
16
  end
17
17
  end
18
18
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
5
- prerelease:
4
+ version: 1.1.2.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Bigg
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-16 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spree_core
16
- requirement: &70243020240540 !ruby/object:Gem::Requirement
16
+ requirement: &70220783785700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
20
20
  - !ruby/object:Gem::Version
21
- version: 1.1.1
21
+ version: 1.1.2.rc1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70243020240540
24
+ version_requirements: *70220783785700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: spree_auth
27
- requirement: &70243020240060 !ruby/object:Gem::Requirement
27
+ requirement: &70220783782780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
31
31
  - !ruby/object:Gem::Version
32
- version: 1.1.1
32
+ version: 1.1.2.rc1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70243020240060
35
+ version_requirements: *70220783782780
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rabl
38
- requirement: &70243020239600 !ruby/object:Gem::Requirement
38
+ requirement: &70220783780280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.6.5
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70243020239600
46
+ version_requirements: *70220783780280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec-rails
49
- requirement: &70243020238960 !ruby/object:Gem::Requirement
49
+ requirement: &70220783772020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - =
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.9.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70243020238960
57
+ version_requirements: *70220783772020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: database_cleaner
60
- requirement: &70243020238580 !ruby/object:Gem::Requirement
60
+ requirement: &70220783768740 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70243020238580
68
+ version_requirements: *70220783768740
69
69
  description: Spree's API
70
70
  email:
71
71
  - ryan@spreecommerce.com
@@ -90,6 +90,7 @@ files:
90
90
  - app/controllers/spree/api/v1/taxonomies_controller.rb
91
91
  - app/controllers/spree/api/v1/taxons_controller.rb
92
92
  - app/controllers/spree/api/v1/variants_controller.rb
93
+ - app/controllers/spree/api/v1/zones_controller.rb
93
94
  - app/helpers/spree/api/api_helpers.rb
94
95
  - app/models/spree/line_item_decorator.rb
95
96
  - app/models/spree/option_value_decorator.rb
@@ -141,6 +142,8 @@ files:
141
142
  - app/views/spree/api/v1/variants/new.rabl
142
143
  - app/views/spree/api/v1/variants/show.rabl
143
144
  - app/views/spree/api/v1/variants/variant.rabl
145
+ - app/views/spree/api/v1/zones/index.rabl
146
+ - app/views/spree/api/v1/zones/show.rabl
144
147
  - config/initializers/metal_load_paths.rb
145
148
  - config/locales/en.yml
146
149
  - config/routes.rb
@@ -166,6 +169,7 @@ files:
166
169
  - spec/controllers/spree/api/v1/taxonomies_controller_spec.rb
167
170
  - spec/controllers/spree/api/v1/taxons_controller_spec.rb
168
171
  - spec/controllers/spree/api/v1/variants_controller_spec.rb
172
+ - spec/controllers/spree/api/v1/zones_controller_spec.rb
169
173
  - spec/fixtures/thinking-cat.jpg
170
174
  - spec/models/spree/order_spec.rb
171
175
  - spec/models/spree/user_spec.rb
@@ -188,16 +192,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
192
  version: '0'
189
193
  segments:
190
194
  - 0
191
- hash: -3593481641416743509
195
+ hash: -1105458712086829155
192
196
  required_rubygems_version: !ruby/object:Gem::Requirement
193
197
  none: false
194
198
  requirements:
195
- - - ! '>='
199
+ - - ! '>'
196
200
  - !ruby/object:Gem::Version
197
- version: '0'
198
- segments:
199
- - 0
200
- hash: -3593481641416743509
201
+ version: 1.3.1
201
202
  requirements: []
202
203
  rubyforge_project:
203
204
  rubygems_version: 1.8.10
@@ -217,6 +218,7 @@ test_files:
217
218
  - spec/controllers/spree/api/v1/taxonomies_controller_spec.rb
218
219
  - spec/controllers/spree/api/v1/taxons_controller_spec.rb
219
220
  - spec/controllers/spree/api/v1/variants_controller_spec.rb
221
+ - spec/controllers/spree/api/v1/zones_controller_spec.rb
220
222
  - spec/fixtures/thinking-cat.jpg
221
223
  - spec/models/spree/order_spec.rb
222
224
  - spec/models/spree/user_spec.rb