spree_api 4.2.0.rc2 → 4.2.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/spree/api/base_controller.rb +8 -2
  3. data/app/controllers/spree/api/v1/shipments_controller.rb +16 -6
  4. data/app/controllers/spree/api/v2/base_controller.rb +0 -25
  5. data/app/controllers/spree/api/v2/resource_controller.rb +82 -0
  6. data/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb +3 -11
  7. data/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb +7 -15
  8. data/app/controllers/spree/api/v2/storefront/account/orders_controller.rb +5 -24
  9. data/app/controllers/spree/api/v2/storefront/account_controller.rb +1 -5
  10. data/app/controllers/spree/api/v2/storefront/countries_controller.rb +9 -16
  11. data/app/controllers/spree/api/v2/storefront/order_status_controller.rb +1 -5
  12. data/app/controllers/spree/api/v2/storefront/products_controller.rb +4 -14
  13. data/app/controllers/spree/api/v2/storefront/stores_controller.rb +2 -6
  14. data/app/controllers/spree/api/v2/storefront/taxons_controller.rb +3 -13
  15. data/app/models/spree/api_dependencies.rb +4 -2
  16. data/app/serializers/spree/v2/storefront/address_serializer.rb +7 -2
  17. data/app/serializers/spree/v2/storefront/base_serializer.rb +1 -1
  18. data/app/serializers/spree/v2/storefront/cart_serializer.rb +2 -1
  19. data/app/serializers/spree/v2/storefront/country_serializer.rb +6 -0
  20. data/app/serializers/spree/v2/storefront/estimated_shipping_rate_serializer.rb +3 -1
  21. data/app/serializers/spree/v2/storefront/payment_serializer.rb +1 -1
  22. data/app/serializers/spree/v2/storefront/product_property_serializer.rb +7 -2
  23. data/app/serializers/spree/v2/storefront/product_serializer.rb +15 -4
  24. data/app/serializers/spree/v2/storefront/shipment_serializer.rb +3 -1
  25. data/app/serializers/spree/v2/storefront/shipping_rate_serializer.rb +3 -1
  26. data/app/serializers/spree/v2/storefront/store_credit_category_serializer.rb +11 -0
  27. data/app/serializers/spree/v2/storefront/store_credit_serializer.rb +1 -1
  28. data/app/serializers/spree/v2/storefront/store_serializer.rb +1 -1
  29. data/app/serializers/spree/v2/storefront/taxon_serializer.rb +11 -3
  30. data/app/serializers/spree/v2/storefront/user_serializer.rb +3 -1
  31. data/app/serializers/spree/v2/storefront/variant_serializer.rb +11 -3
  32. data/config/locales/en.yml +1 -1
  33. data/docs/oauth/index.yml +78 -15
  34. data/docs/v2/storefront/index.yaml +3232 -663
  35. data/lib/spree/api/testing_support/v2/current_order.rb +2 -0
  36. data/lib/spree_api.rb +1 -1
  37. data/spree_api.gemspec +1 -1
  38. metadata +12 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 870bda309064df374e65aee63ddd2d7abb72004dbb6d218f0b1b77b7f5b0219f
4
- data.tar.gz: e8fd20974f60d065473f2fb1303dd86221de56173f6a1e957c7efc8d5dba2aed
3
+ metadata.gz: 91e71f07d6f92ec8d91766b639b1b8cac45eb77d7b315cb9f1c64386781f4e93
4
+ data.tar.gz: 30599040f61b4ce8a2b43f806d7b5a1ecb06caab6412de565da6eed3370924b5
5
5
  SHA512:
6
- metadata.gz: 37e0a2ef4037067d2ffd5391d70b7c9fd6953d0529721e83ba7935755b1bca9515bc047e16525cae1df3d68e0815082f21e3e5579521f80259c717abe06d0b76
7
- data.tar.gz: 87d24be4132f732043258a35db7b5398f3a63fe99908141e6bbba92786e5897a2c9c9ddf0c3ed5efa9b3da6b65a73f2ac5e4a895fc4bb69038ab5e597ece2932
6
+ metadata.gz: c5b26dc1484564a4fc64da2ef148a2662c6b9d5ac11f059b894feb1362659e4841c496bc2c8926f8e60536251a1b5d8ff8f17bcc23eecd265edc5ee95d12e458
7
+ data.tar.gz: 4d7e2cf195104266d197256c0b9fb1fd08cbe13855f11dfead0246c780f6fa92b0c2f0ca86483b1708eebd77b03bec6ece68f40d28cf97b1f53c06cc5b9db1b7
@@ -81,10 +81,16 @@ module Spree
81
81
  end
82
82
 
83
83
  def error_during_processing(exception)
84
- Rails.logger.error exception.message
84
+ message = if exception.respond_to?(:original_message)
85
+ exception.original_message
86
+ else
87
+ exception.message
88
+ end
89
+
90
+ Rails.logger.error message
85
91
  Rails.logger.error exception.backtrace.join("\n")
86
92
 
87
- unprocessable_entity(exception.message)
93
+ unprocessable_entity(message)
88
94
  end
89
95
 
90
96
  def unprocessable_entity(message)
@@ -92,12 +92,17 @@ module Spree
92
92
  @stock_location = Spree::StockLocation.find(params[:stock_location_id])
93
93
 
94
94
  unless @quantity > 0
95
- unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")
95
+ unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")
96
96
  return
97
97
  end
98
98
 
99
- @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
100
- render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
99
+ transfer = @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
100
+ if transfer.valid?
101
+ transfer.run!
102
+ render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
103
+ else
104
+ render json: { message: transfer.errors.full_messages.to_sentence }, status: 422
105
+ end
101
106
  end
102
107
 
103
108
  def transfer_to_shipment
@@ -113,10 +118,15 @@ module Spree
113
118
  end
114
119
 
115
120
  if error
116
- unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n#{error}")
121
+ unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n#{error}")
117
122
  else
118
- @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
119
- render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
123
+ transfer = @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
124
+ if transfer.valid?
125
+ transfer.run!
126
+ render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
127
+ else
128
+ render json: { message: transfer.errors.full_messages }, status: 422
129
+ end
120
130
  end
121
131
  end
122
132
 
@@ -13,31 +13,6 @@ module Spree
13
13
  Spree::Api::Config[:api_v2_content_type]
14
14
  end
15
15
 
16
- private
17
-
18
- def serialize_collection(collection)
19
- collection_serializer.new(
20
- collection,
21
- collection_options(collection)
22
- ).serializable_hash
23
- end
24
-
25
- def serialize_resource(resource)
26
- resource_serializer.new(
27
- resource,
28
- include: resource_includes,
29
- fields: sparse_fields
30
- ).serializable_hash
31
- end
32
-
33
- def paginated_collection
34
- collection_paginator.new(sorted_collection, params).call
35
- end
36
-
37
- def collection_paginator
38
- Spree::Api::Dependencies.storefront_collection_paginator.constantize
39
- end
40
-
41
16
  def render_serialized_payload(status = 200)
42
17
  render json: yield, status: status, content_type: content_type
43
18
  rescue ArgumentError => exception
@@ -0,0 +1,82 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class ResourceController < ::Spree::Api::V2::BaseController
5
+ include Spree::Api::V2::CollectionOptionsHelpers
6
+
7
+ def index
8
+ render_serialized_payload { serialize_collection(paginated_collection) }
9
+ end
10
+
11
+ def show
12
+ render_serialized_payload { serialize_resource(resource) }
13
+ end
14
+
15
+ protected
16
+
17
+ def serialize_collection(collection)
18
+ collection_serializer.new(
19
+ collection,
20
+ collection_options(collection)
21
+ ).serializable_hash
22
+ end
23
+
24
+ def serialize_resource(resource)
25
+ resource_serializer.new(
26
+ resource,
27
+ include: resource_includes,
28
+ fields: sparse_fields
29
+ ).serializable_hash
30
+ end
31
+
32
+ def paginated_collection
33
+ collection_paginator.new(sorted_collection, params).call
34
+ end
35
+
36
+ def collection_paginator
37
+ Spree::Api::Dependencies.storefront_collection_paginator.constantize
38
+ end
39
+
40
+ def sorted_collection
41
+ collection_sorter.new(collection, params, allowed_sort_attributes).call
42
+ end
43
+
44
+ def allowed_sort_attributes
45
+ default_sort_atributes
46
+ end
47
+
48
+ def default_sort_atributes
49
+ [:id, :updated_at, :created_at]
50
+ end
51
+
52
+ def scope
53
+ model_class.accessible_by(current_ability, :show).includes(scope_includes)
54
+ end
55
+
56
+ def scope_includes
57
+ []
58
+ end
59
+
60
+ def resource
61
+ @resource ||= if defined?(resource_finder)
62
+ resource_finder.new(scope: scope, params: params).execute
63
+ else
64
+ scope.find(params[:id])
65
+ end
66
+ end
67
+
68
+ def collection
69
+ @collection ||= if defined?(collection_finder)
70
+ collection_finder.new(scope: scope, params: params).execute
71
+ else
72
+ scope
73
+ end
74
+ end
75
+
76
+ def collection_sorter
77
+ Spree::Api::Dependencies.storefront_collection_sorter.constantize
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -3,13 +3,9 @@ module Spree
3
3
  module V2
4
4
  module Storefront
5
5
  module Account
6
- class AddressesController < ::Spree::Api::V2::BaseController
6
+ class AddressesController < ::Spree::Api::V2::ResourceController
7
7
  before_action :require_spree_current_user
8
8
 
9
- def index
10
- render_serialized_payload { serialize_collection(collection) }
11
- end
12
-
13
9
  def create
14
10
  result = create_service.call(user: spree_current_user, address_params: address_params)
15
11
  render_result(result)
@@ -26,12 +22,8 @@ module Spree
26
22
  collection_finder.new(scope: scope, params: params).execute
27
23
  end
28
24
 
29
- def resource
30
- @resource ||= scope.find(params[:id])
31
- end
32
-
33
- def scope
34
- spree_current_user.addresses
25
+ def model_class
26
+ Spree::Address
35
27
  end
36
28
 
37
29
  def collection_finder
@@ -3,27 +3,23 @@ module Spree
3
3
  module V2
4
4
  module Storefront
5
5
  module Account
6
- class CreditCardsController < ::Spree::Api::V2::BaseController
6
+ class CreditCardsController < ::Spree::Api::V2::ResourceController
7
7
  before_action :require_spree_current_user
8
8
 
9
- def index
10
- render_serialized_payload { serialize_collection(resource) }
11
- end
12
-
13
- def show
14
- render_serialized_payload { serialize_resource(resource) }
15
- end
16
-
17
9
  private
18
10
 
19
- def resource
20
- resource_finder.new.execute(scope: scope, params: params)
11
+ def model_class
12
+ Spree::CreditCard
21
13
  end
22
14
 
23
15
  def collection_serializer
24
16
  Spree::Api::Dependencies.storefront_credit_card_serializer.constantize
25
17
  end
26
18
 
19
+ def collection_finder
20
+ Spree::Api::Dependencies.storefront_credit_card_finder.constantize
21
+ end
22
+
27
23
  def resource_serializer
28
24
  Spree::Api::Dependencies.storefront_credit_card_serializer.constantize
29
25
  end
@@ -39,10 +35,6 @@ module Spree
39
35
  fields: sparse_fields
40
36
  ).serializable_hash
41
37
  end
42
-
43
- def scope
44
- spree_current_user.credit_cards.accessible_by(current_ability, :show)
45
- end
46
38
  end
47
39
  end
48
40
  end
@@ -3,26 +3,11 @@ module Spree
3
3
  module V2
4
4
  module Storefront
5
5
  module Account
6
- class OrdersController < ::Spree::Api::V2::BaseController
7
- include Spree::Api::V2::CollectionOptionsHelpers
6
+ class OrdersController < ::Spree::Api::V2::ResourceController
8
7
  before_action :require_spree_current_user
9
8
 
10
- def index
11
- render_serialized_payload { serialize_collection(paginated_collection) }
12
- end
13
-
14
- def show
15
- spree_authorize! :show, resource
16
-
17
- render_serialized_payload { serialize_resource(resource) }
18
- end
19
-
20
9
  private
21
10
 
22
- def sorted_collection
23
- collection_sorter.new(collection, params).call
24
- end
25
-
26
11
  def collection
27
12
  collection_finder.new(user: spree_current_user).execute
28
13
  end
@@ -34,6 +19,10 @@ module Spree
34
19
  resource
35
20
  end
36
21
 
22
+ def allowed_sort_attributes
23
+ super << :completed_at
24
+ end
25
+
37
26
  def collection_serializer
38
27
  Spree::Api::Dependencies.storefront_order_serializer.constantize
39
28
  end
@@ -49,14 +38,6 @@ module Spree
49
38
  def resource_finder
50
39
  Spree::Api::Dependencies.storefront_completed_order_finder.constantize
51
40
  end
52
-
53
- def collection_sorter
54
- Spree::Api::Dependencies.storefront_order_sorter.constantize
55
- end
56
-
57
- def collection_paginator
58
- Spree::Api::Dependencies.storefront_collection_paginator.constantize
59
- end
60
41
  end
61
42
  end
62
43
  end
@@ -2,13 +2,9 @@ module Spree
2
2
  module Api
3
3
  module V2
4
4
  module Storefront
5
- class AccountController < ::Spree::Api::V2::BaseController
5
+ class AccountController < ::Spree::Api::V2::ResourceController
6
6
  before_action :require_spree_current_user
7
7
 
8
- def show
9
- render_serialized_payload { serialize_resource(resource) }
10
- end
11
-
12
8
  private
13
9
 
14
10
  def resource
@@ -2,17 +2,7 @@ module Spree
2
2
  module Api
3
3
  module V2
4
4
  module Storefront
5
- class CountriesController < ::Spree::Api::V2::BaseController
6
- include Spree::Api::V2::CollectionOptionsHelpers
7
-
8
- def index
9
- render_serialized_payload { serialize_collection(collection) }
10
- end
11
-
12
- def show
13
- render_serialized_payload { serialize_resource(resource) }
14
- end
15
-
5
+ class CountriesController < ::Spree::Api::V2::ResourceController
16
6
  private
17
7
 
18
8
  def serialize_collection(collection)
@@ -24,12 +14,15 @@ module Spree
24
14
  resource,
25
15
  include: resource_includes,
26
16
  fields: sparse_fields,
27
- params: { include_states: true }
17
+ params: resource_serializer_params
28
18
  ).serializable_hash
29
19
  end
30
20
 
31
- def collection
32
- collection_finder.new(scope, params).call
21
+ def resource_serializer_params
22
+ {
23
+ include_states: true,
24
+ current_store: current_store
25
+ }
33
26
  end
34
27
 
35
28
  def resource
@@ -52,8 +45,8 @@ module Spree
52
45
  Spree::Api::Dependencies.storefront_country_finder.constantize
53
46
  end
54
47
 
55
- def scope
56
- Spree::Country.accessible_by(current_ability, :show)
48
+ def model_class
49
+ Spree::Country
57
50
  end
58
51
  end
59
52
  end
@@ -2,15 +2,11 @@ module Spree
2
2
  module Api
3
3
  module V2
4
4
  module Storefront
5
- class OrderStatusController < ::Spree::Api::V2::BaseController
5
+ class OrderStatusController < ::Spree::Api::V2::ResourceController
6
6
  include Spree::Api::V2::Storefront::OrderConcern
7
7
 
8
8
  before_action :ensure_order_token
9
9
 
10
- def show
11
- render_serialized_payload { serialize_resource(resource) }
12
- end
13
-
14
10
  private
15
11
 
16
12
  def resource
@@ -2,21 +2,11 @@ module Spree
2
2
  module Api
3
3
  module V2
4
4
  module Storefront
5
- class ProductsController < ::Spree::Api::V2::BaseController
6
- include Spree::Api::V2::CollectionOptionsHelpers
7
-
8
- def index
9
- render_serialized_payload { serialize_collection(paginated_collection) }
10
- end
11
-
12
- def show
13
- render_serialized_payload { serialize_resource(resource) }
14
- end
15
-
5
+ class ProductsController < ::Spree::Api::V2::ResourceController
16
6
  private
17
7
 
18
8
  def sorted_collection
19
- collection_sorter.new(collection, params, current_currency).call
9
+ collection_sorter.new(collection, current_currency, params, allowed_sort_attributes).call
20
10
  end
21
11
 
22
12
  def collection
@@ -43,8 +33,8 @@ module Spree
43
33
  Spree::Api::Dependencies.storefront_product_serializer.constantize
44
34
  end
45
35
 
46
- def scope
47
- Spree::Product.accessible_by(current_ability, :show).includes(scope_includes)
36
+ def model_class
37
+ Spree::Product
48
38
  end
49
39
 
50
40
  def scope_includes
@@ -2,14 +2,10 @@ module Spree
2
2
  module Api
3
3
  module V2
4
4
  module Storefront
5
- class StoresController < ::Spree::Api::V2::BaseController
6
- def show
7
- render_serialized_payload { serialize_resource(resource) }
8
- end
9
-
5
+ class StoresController < ::Spree::Api::V2::ResourceController
10
6
  private
11
7
 
12
- def scope
8
+ def model_class
13
9
  Spree::Store
14
10
  end
15
11