spree_api 3.0.10 → 3.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/spree/api/base_controller.rb +21 -22
  3. data/app/controllers/spree/api/v1/addresses_controller.rb +46 -0
  4. data/app/controllers/spree/api/v1/checkouts_controller.rb +103 -0
  5. data/app/controllers/spree/api/v1/classifications_controller.rb +20 -0
  6. data/app/controllers/spree/api/v1/countries_controller.rb +24 -0
  7. data/app/controllers/spree/api/v1/credit_cards_controller.rb +27 -0
  8. data/app/controllers/spree/api/v1/images_controller.rb +56 -0
  9. data/app/controllers/spree/api/v1/inventory_units_controller.rb +54 -0
  10. data/app/controllers/spree/api/v1/line_items_controller.rb +70 -0
  11. data/app/controllers/spree/api/v1/option_types_controller.rb +51 -0
  12. data/app/controllers/spree/api/v1/option_values_controller.rb +60 -0
  13. data/app/controllers/spree/api/v1/orders_controller.rb +137 -0
  14. data/app/controllers/spree/api/v1/payments_controller.rb +82 -0
  15. data/app/controllers/spree/api/v1/product_properties_controller.rb +73 -0
  16. data/app/controllers/spree/api/v1/products_controller.rb +126 -0
  17. data/app/controllers/spree/api/v1/promotions_controller.rb +28 -0
  18. data/app/controllers/spree/api/v1/properties_controller.rb +72 -0
  19. data/app/controllers/spree/api/v1/return_authorizations_controller.rb +71 -0
  20. data/app/controllers/spree/api/v1/shipments_controller.rb +163 -0
  21. data/app/controllers/spree/api/v1/states_controller.rb +37 -0
  22. data/app/controllers/spree/api/v1/stock_items_controller.rb +77 -0
  23. data/app/controllers/spree/api/v1/stock_locations_controller.rb +52 -0
  24. data/app/controllers/spree/api/v1/stock_movements_controller.rb +45 -0
  25. data/app/controllers/spree/api/v1/stores_controller.rb +57 -0
  26. data/app/controllers/spree/api/v1/taxonomies_controller.rb +66 -0
  27. data/app/controllers/spree/api/v1/taxons_controller.rb +95 -0
  28. data/app/controllers/spree/api/v1/users_controller.rb +60 -0
  29. data/app/controllers/spree/api/v1/variants_controller.rb +75 -0
  30. data/app/controllers/spree/api/v1/zones_controller.rb +51 -0
  31. data/app/helpers/spree/api/api_helpers.rb +3 -2
  32. data/app/views/spree/api/{addresses → v1/addresses}/show.v1.rabl +0 -0
  33. data/app/views/spree/api/{adjustments → v1/adjustments}/show.v1.rabl +0 -0
  34. data/app/views/spree/api/{config → v1/config}/money.v1.rabl +0 -0
  35. data/app/views/spree/api/{config → v1/config}/show.v1.rabl +0 -0
  36. data/app/views/spree/api/{countries → v1/countries}/index.v1.rabl +0 -0
  37. data/app/views/spree/api/{countries → v1/countries}/show.v1.rabl +0 -0
  38. data/app/views/spree/api/{credit_cards → v1/credit_cards}/index.v1.rabl +1 -1
  39. data/app/views/spree/api/{credit_cards → v1/credit_cards}/show.v1.rabl +0 -0
  40. data/app/views/spree/api/{images → v1/images}/index.v1.rabl +1 -1
  41. data/app/views/spree/api/v1/images/new.v1.rabl +3 -0
  42. data/app/views/spree/api/{images → v1/images}/show.v1.rabl +0 -0
  43. data/app/views/spree/api/{inventory_units → v1/inventory_units}/show.rabl +0 -0
  44. data/app/views/spree/api/{line_items → v1/line_items}/new.v1.rabl +0 -0
  45. data/app/views/spree/api/{line_items → v1/line_items}/show.v1.rabl +3 -3
  46. data/app/views/spree/api/v1/option_types/index.v1.rabl +3 -0
  47. data/app/views/spree/api/v1/option_types/new.v1.rabl +3 -0
  48. data/app/views/spree/api/{option_types → v1/option_types}/show.v1.rabl +0 -0
  49. data/app/views/spree/api/v1/option_values/index.v1.rabl +3 -0
  50. data/app/views/spree/api/v1/option_values/new.v1.rabl +3 -0
  51. data/app/views/spree/api/{option_values → v1/option_values}/show.v1.rabl +0 -0
  52. data/app/views/spree/api/{orders → v1/orders}/address.v1.rabl +0 -0
  53. data/app/views/spree/api/{orders → v1/orders}/canceled.v1.rabl +0 -0
  54. data/app/views/spree/api/{orders → v1/orders}/cart.v1.rabl +0 -0
  55. data/app/views/spree/api/{orders → v1/orders}/complete.v1.rabl +0 -0
  56. data/app/views/spree/api/{orders → v1/orders}/could_not_apply_coupon.v1.rabl +0 -0
  57. data/app/views/spree/api/{orders → v1/orders}/could_not_transition.v1.rabl +0 -0
  58. data/app/views/spree/api/{orders → v1/orders}/index.v1.rabl +1 -1
  59. data/app/views/spree/api/{orders → v1/orders}/invalid_shipping_method.v1.rabl +0 -0
  60. data/app/views/spree/api/{orders → v1/orders}/mine.v1.rabl +1 -1
  61. data/app/views/spree/api/{orders → v1/orders}/order.v1.rabl +1 -0
  62. data/app/views/spree/api/{orders → v1/orders}/payment.v1.rabl +0 -0
  63. data/app/views/spree/api/{orders → v1/orders}/show.v1.rabl +9 -9
  64. data/app/views/spree/api/{payments → v1/payments}/credit_over_limit.v1.rabl +0 -0
  65. data/app/views/spree/api/{payments → v1/payments}/index.v1.rabl +0 -0
  66. data/app/views/spree/api/{payments → v1/payments}/new.v1.rabl +0 -0
  67. data/app/views/spree/api/{payments → v1/payments}/show.v1.rabl +0 -0
  68. data/app/views/spree/api/{payments → v1/payments}/update_forbidden.v1.rabl +0 -0
  69. data/app/views/spree/api/{product_properties → v1/product_properties}/index.v1.rabl +0 -0
  70. data/app/views/spree/api/{product_properties → v1/product_properties}/new.v1.rabl +0 -0
  71. data/app/views/spree/api/{product_properties → v1/product_properties}/show.v1.rabl +0 -0
  72. data/app/views/spree/api/{products → v1/products}/index.v1.rabl +1 -1
  73. data/app/views/spree/api/{products → v1/products}/new.v1.rabl +0 -0
  74. data/app/views/spree/api/{products → v1/products}/product.v1.rabl +0 -0
  75. data/app/views/spree/api/{products → v1/products}/show.v1.rabl +3 -3
  76. data/app/views/spree/api/{promotions → v1/promotions}/handler.v1.rabl +0 -0
  77. data/app/views/spree/api/{promotions → v1/promotions}/show.v1.rabl +0 -0
  78. data/app/views/spree/api/{properties → v1/properties}/index.v1.rabl +0 -0
  79. data/app/views/spree/api/{properties → v1/properties}/new.v1.rabl +0 -0
  80. data/app/views/spree/api/{properties → v1/properties}/show.v1.rabl +0 -0
  81. data/app/views/spree/api/{return_authorizations → v1/return_authorizations}/index.v1.rabl +0 -0
  82. data/app/views/spree/api/{return_authorizations → v1/return_authorizations}/new.v1.rabl +0 -0
  83. data/app/views/spree/api/{return_authorizations → v1/return_authorizations}/show.v1.rabl +0 -0
  84. data/app/views/spree/api/{shared → v1/shared}/stock_location_required.v1.rabl +0 -0
  85. data/app/views/spree/api/{shipments → v1/shipments}/big.v1.rabl +7 -7
  86. data/app/views/spree/api/{shipments → v1/shipments}/cannot_ready_shipment.v1.rabl +0 -0
  87. data/app/views/spree/api/{shipments → v1/shipments}/mine.v1.rabl +1 -1
  88. data/app/views/spree/api/{shipments → v1/shipments}/show.v1.rabl +3 -3
  89. data/app/views/spree/api/{shipments → v1/shipments}/small.v1.rabl +3 -3
  90. data/app/views/spree/api/{shipping_rates → v1/shipping_rates}/show.v1.rabl +0 -0
  91. data/app/views/spree/api/{states → v1/states}/index.v1.rabl +0 -0
  92. data/app/views/spree/api/{states → v1/states}/show.v1.rabl +0 -0
  93. data/app/views/spree/api/{stock_items → v1/stock_items}/index.v1.rabl +1 -1
  94. data/app/views/spree/api/{stock_items → v1/stock_items}/show.v1.rabl +1 -1
  95. data/app/views/spree/api/{stock_locations → v1/stock_locations}/index.v1.rabl +1 -1
  96. data/app/views/spree/api/{stock_locations → v1/stock_locations}/show.v1.rabl +0 -0
  97. data/app/views/spree/api/{stock_movements → v1/stock_movements}/index.v1.rabl +1 -1
  98. data/app/views/spree/api/{stock_movements → v1/stock_movements}/show.v1.rabl +1 -1
  99. data/app/views/spree/api/{stores → v1/stores}/index.v1.rabl +0 -0
  100. data/app/views/spree/api/{stores → v1/stores}/show.v1.rabl +0 -0
  101. data/app/views/spree/api/{taxonomies → v1/taxonomies}/index.v1.rabl +1 -1
  102. data/app/views/spree/api/{taxonomies → v1/taxonomies}/jstree.rabl +0 -0
  103. data/app/views/spree/api/{taxonomies → v1/taxonomies}/nested.v1.rabl +1 -1
  104. data/app/views/spree/api/{taxonomies → v1/taxonomies}/new.v1.rabl +0 -0
  105. data/app/views/spree/api/{taxonomies → v1/taxonomies}/show.v1.rabl +4 -4
  106. data/app/views/spree/api/{taxons → v1/taxons}/index.v1.rabl +1 -1
  107. data/app/views/spree/api/{taxons → v1/taxons}/jstree.rabl +0 -0
  108. data/app/views/spree/api/{taxons → v1/taxons}/new.v1.rabl +0 -0
  109. data/app/views/spree/api/{taxons → v1/taxons}/show.v1.rabl +0 -0
  110. data/app/views/spree/api/v1/taxons/taxons.v1.rabl +5 -0
  111. data/app/views/spree/api/{users → v1/users}/index.v1.rabl +1 -1
  112. data/app/views/spree/api/{users → v1/users}/new.v1.rabl +0 -0
  113. data/app/views/spree/api/{users → v1/users}/show.v1.rabl +2 -2
  114. data/app/views/spree/api/{variants → v1/variants}/big.v1.rabl +1 -1
  115. data/app/views/spree/api/{variants → v1/variants}/index.v1.rabl +1 -1
  116. data/app/views/spree/api/{variants → v1/variants}/new.v1.rabl +0 -0
  117. data/app/views/spree/api/{variants → v1/variants}/show.v1.rabl +1 -1
  118. data/app/views/spree/api/{variants → v1/variants}/small.v1.rabl +1 -1
  119. data/app/views/spree/api/{zones → v1/zones}/index.v1.rabl +1 -1
  120. data/app/views/spree/api/{zones → v1/zones}/show.v1.rabl +0 -0
  121. data/config/routes.rb +85 -88
  122. data/spec/controllers/spree/api/base_controller_spec.rb +14 -42
  123. data/spec/controllers/spree/api/{addresses_controller_spec.rb → v1/addresses_controller_spec.rb} +1 -1
  124. data/spec/controllers/spree/api/{checkouts_controller_spec.rb → v1/checkouts_controller_spec.rb} +51 -3
  125. data/spec/controllers/spree/api/{classifications_controller_spec.rb → v1/classifications_controller_spec.rb} +2 -2
  126. data/spec/controllers/spree/api/{countries_controller_spec.rb → v1/countries_controller_spec.rb} +1 -1
  127. data/spec/controllers/spree/api/{credit_cards_controller_spec.rb → v1/credit_cards_controller_spec.rb} +1 -1
  128. data/spec/controllers/spree/api/{images_controller_spec.rb → v1/images_controller_spec.rb} +23 -1
  129. data/spec/controllers/spree/api/{inventory_units_controller_spec.rb → v1/inventory_units_controller_spec.rb} +1 -1
  130. data/spec/controllers/spree/api/{line_items_controller_spec.rb → v1/line_items_controller_spec.rb} +2 -2
  131. data/spec/controllers/spree/api/{option_types_controller_spec.rb → v1/option_types_controller_spec.rb} +9 -3
  132. data/spec/controllers/spree/api/{option_values_controller_spec.rb → v1/option_values_controller_spec.rb} +8 -2
  133. data/spec/controllers/spree/api/{orders_controller_spec.rb → v1/orders_controller_spec.rb} +29 -8
  134. data/spec/controllers/spree/api/{payments_controller_spec.rb → v1/payments_controller_spec.rb} +2 -2
  135. data/spec/controllers/spree/api/{product_properties_controller_spec.rb → v1/product_properties_controller_spec.rb} +1 -1
  136. data/spec/controllers/spree/api/{products_controller_spec.rb → v1/products_controller_spec.rb} +5 -5
  137. data/spec/controllers/spree/api/{promotion_application_spec.rb → v1/promotion_application_spec.rb} +2 -2
  138. data/spec/controllers/spree/api/{promotions_controller_spec.rb → v1/promotions_controller_spec.rb} +1 -1
  139. data/spec/controllers/spree/api/{properties_controller_spec.rb → v1/properties_controller_spec.rb} +1 -1
  140. data/spec/controllers/spree/api/{return_authorizations_controller_spec.rb → v1/return_authorizations_controller_spec.rb} +1 -1
  141. data/spec/controllers/spree/api/{shipments_controller_spec.rb → v1/shipments_controller_spec.rb} +4 -4
  142. data/spec/controllers/spree/api/{states_controller_spec.rb → v1/states_controller_spec.rb} +14 -10
  143. data/spec/controllers/spree/api/{stock_items_controller_spec.rb → v1/stock_items_controller_spec.rb} +1 -1
  144. data/spec/controllers/spree/api/{stock_locations_controller_spec.rb → v1/stock_locations_controller_spec.rb} +1 -1
  145. data/spec/controllers/spree/api/{stock_movements_controller_spec.rb → v1/stock_movements_controller_spec.rb} +1 -1
  146. data/spec/controllers/spree/api/{stores_controller_spec.rb → v1/stores_controller_spec.rb} +1 -1
  147. data/spec/controllers/spree/api/{taxonomies_controller_spec.rb → v1/taxonomies_controller_spec.rb} +1 -1
  148. data/spec/controllers/spree/api/{taxons_controller_spec.rb → v1/taxons_controller_spec.rb} +1 -1
  149. data/spec/controllers/spree/api/{unauthenticated_products_controller_spec.rb → v1/unauthenticated_products_controller_spec.rb} +1 -1
  150. data/spec/controllers/spree/api/{users_controller_spec.rb → v1/users_controller_spec.rb} +1 -1
  151. data/spec/controllers/spree/api/{variants_controller_spec.rb → v1/variants_controller_spec.rb} +3 -3
  152. data/spec/controllers/spree/api/{zones_controller_spec.rb → v1/zones_controller_spec.rb} +7 -5
  153. data/spec/requests/rabl_cache_spec.rb +2 -2
  154. data/spec/requests/ransackable_attributes_spec.rb +6 -6
  155. data/spec/requests/version_spec.rb +19 -0
  156. data/spree_api.gemspec +1 -1
  157. metadata +188 -185
  158. data/CHANGELOG.md +0 -3
  159. data/app/controllers/spree/api/addresses_controller.rb +0 -43
  160. data/app/controllers/spree/api/checkouts_controller.rb +0 -98
  161. data/app/controllers/spree/api/classifications_controller.rb +0 -18
  162. data/app/controllers/spree/api/countries_controller.rb +0 -23
  163. data/app/controllers/spree/api/credit_cards_controller.rb +0 -25
  164. data/app/controllers/spree/api/images_controller.rb +0 -47
  165. data/app/controllers/spree/api/inventory_units_controller.rb +0 -52
  166. data/app/controllers/spree/api/line_items_controller.rb +0 -69
  167. data/app/controllers/spree/api/option_types_controller.rb +0 -49
  168. data/app/controllers/spree/api/option_values_controller.rb +0 -58
  169. data/app/controllers/spree/api/orders_controller.rb +0 -130
  170. data/app/controllers/spree/api/payments_controller.rb +0 -80
  171. data/app/controllers/spree/api/product_properties_controller.rb +0 -72
  172. data/app/controllers/spree/api/products_controller.rb +0 -124
  173. data/app/controllers/spree/api/promotions_controller.rb +0 -26
  174. data/app/controllers/spree/api/properties_controller.rb +0 -70
  175. data/app/controllers/spree/api/return_authorizations_controller.rb +0 -69
  176. data/app/controllers/spree/api/shipments_controller.rb +0 -149
  177. data/app/controllers/spree/api/states_controller.rb +0 -38
  178. data/app/controllers/spree/api/stock_items_controller.rb +0 -75
  179. data/app/controllers/spree/api/stock_locations_controller.rb +0 -50
  180. data/app/controllers/spree/api/stock_movements_controller.rb +0 -43
  181. data/app/controllers/spree/api/stores_controller.rb +0 -55
  182. data/app/controllers/spree/api/taxonomies_controller.rb +0 -64
  183. data/app/controllers/spree/api/taxons_controller.rb +0 -93
  184. data/app/controllers/spree/api/users_controller.rb +0 -56
  185. data/app/controllers/spree/api/variants_controller.rb +0 -73
  186. data/app/controllers/spree/api/zones_controller.rb +0 -49
  187. data/app/models/spree/option_value_decorator.rb +0 -9
  188. data/app/views/spree/api/option_types/index.v1.rabl +0 -3
  189. data/app/views/spree/api/option_values/index.v1.rabl +0 -3
  190. data/app/views/spree/api/taxons/taxons.v1.rabl +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04fc31f7217d5cc17609c6c56fe26b8cf1a388a8
4
- data.tar.gz: 60dec0893bfa0a8931848250d3a0cc8c29174370
3
+ metadata.gz: 619c0d865d3f6eb5fdbd68c17860e2e7a260452f
4
+ data.tar.gz: 15a80886dd571611fede3f469fd5bec77424eb5d
5
5
  SHA512:
6
- metadata.gz: 92672928882abad9f58df26a51156774bb39ee6f2d45f028ad416df711fa9bf4b19a52fe8ac8ea1d6d8982c98ed8b7a381f6424b942dcdb86406a40f39ae34ab
7
- data.tar.gz: 2ded41e4383416864502cbbdd047966a397c2988f64ea0cd9e2dac1718baffe591ad11a5cf554b3f90105a67b87238f7beabcececb40549078382a999ffe0dc3
6
+ metadata.gz: bc0caf79058b10c7b724e0b5f8870b0dd4b93db16b1f48bfe4a1db7dc353eb9bdfb5c49d5bb5328e0dc50e1931cb233b3db1687be25af259956c1accb7831540
7
+ data.tar.gz: ae5ba6a9dce7d870b644f07967aa548b778f72fe7f2117e37a697506de91a97039c5dd8e4b1e3f009d38d2ea56acb183688b880b49f6d127fc0a5bbcc7a8caba
@@ -9,15 +9,14 @@ module Spree
9
9
 
10
10
  attr_accessor :current_api_user
11
11
 
12
- class_attribute :error_notifier
13
-
14
12
  before_action :set_content_type
15
13
  before_action :load_user
16
14
  before_action :authorize_for_order, if: Proc.new { order_token.present? }
17
15
  before_action :authenticate_user
18
16
  before_action :load_user_roles
19
17
 
20
- rescue_from Exception, with: :error_during_processing
18
+ rescue_from ActionController::ParameterMissing, with: :error_during_processing
19
+ rescue_from ActiveRecord::RecordInvalid, with: :error_during_processing
21
20
  rescue_from ActiveRecord::RecordNotFound, with: :not_found
22
21
  rescue_from CanCan::AccessDenied, with: :unauthorized
23
22
  rescue_from Spree::Core::GatewayError, with: :gateway_error
@@ -62,24 +61,20 @@ module Spree
62
61
  end
63
62
 
64
63
  def authenticate_user
65
- unless @current_api_user
66
- if requires_authentication? && api_key.blank? && order_token.blank?
67
- render "spree/api/errors/must_specify_api_key", :status => 401 and return
68
- elsif order_token.blank? && (requires_authentication? || api_key.present?)
69
- render "spree/api/errors/invalid_api_key", :status => 401 and return
70
- else
71
- # An anonymous user
72
- @current_api_user = Spree.user_class.new
73
- end
64
+ return if @current_api_user
65
+
66
+ if requires_authentication? && api_key.blank? && order_token.blank?
67
+ render "spree/api/errors/must_specify_api_key", status: 401 and return
68
+ elsif order_token.blank? && (requires_authentication? || api_key.present?)
69
+ render "spree/api/errors/invalid_api_key", status: 401 and return
70
+ else
71
+ # An anonymous user
72
+ @current_api_user = Spree.user_class.new
74
73
  end
75
74
  end
76
75
 
77
76
  def load_user_roles
78
- @current_user_roles = if @current_api_user
79
- @current_api_user.spree_roles.pluck(:name)
80
- else
81
- []
82
- end
77
+ @current_user_roles = @current_api_user ? @current_api_user.spree_roles.pluck(:name) : []
83
78
  end
84
79
 
85
80
  def unauthorized
@@ -90,10 +85,11 @@ module Spree
90
85
  Rails.logger.error exception.message
91
86
  Rails.logger.error exception.backtrace.join("\n")
92
87
 
93
- error_notifier.call(exception, self) if error_notifier
88
+ unprocessable_entity(exception.message)
89
+ end
94
90
 
95
- render text: { exception: exception.message }.to_json,
96
- status: 422 and return
91
+ def unprocessable_entity(message)
92
+ render text: { exception: message }.to_json, status: 422
97
93
  end
98
94
 
99
95
  def gateway_error(exception)
@@ -115,7 +111,7 @@ module Spree
115
111
 
116
112
  def invalid_resource!(resource)
117
113
  @resource = resource
118
- render "spree/api/errors/invalid_resource", :status => 422
114
+ render "spree/api/errors/invalid_resource", status: 422
119
115
  end
120
116
 
121
117
  def api_key
@@ -140,6 +136,9 @@ module Spree
140
136
  unless params[:show_deleted]
141
137
  scope = scope.not_deleted
142
138
  end
139
+ unless params[:show_discontinued]
140
+ scope = scope.not_discontinued
141
+ end
143
142
  else
144
143
  scope = Product.accessible_by(current_ability, :read).active.includes(*product_includes)
145
144
  end
@@ -152,7 +151,7 @@ module Spree
152
151
  end
153
152
 
154
153
  def product_includes
155
- [ :option_types, :taxons, product_properties: :property, variants: variants_associations, master: variants_associations ]
154
+ [:option_types, :taxons, product_properties: :property, variants: variants_associations, master: variants_associations]
156
155
  end
157
156
 
158
157
  def order_id
@@ -0,0 +1,46 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class AddressesController < Spree::Api::BaseController
5
+ before_action :find_order
6
+
7
+ def show
8
+ authorize! :read, @order, order_token
9
+ @address = find_address
10
+ respond_with(@address)
11
+ end
12
+
13
+ def update
14
+ authorize! :update, @order, order_token
15
+ @address = find_address
16
+
17
+ if @address.update_attributes(address_params)
18
+ respond_with(@address, :default_template => :show)
19
+ else
20
+ invalid_resource!(@address)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def address_params
27
+ params.require(:address).permit(permitted_address_attributes)
28
+ end
29
+
30
+ def find_order
31
+ @order = Spree::Order.find_by!(number: order_id)
32
+ end
33
+
34
+ def find_address
35
+ if @order.bill_address_id == params[:id].to_i
36
+ @order.bill_address
37
+ elsif @order.ship_address_id == params[:id].to_i
38
+ @order.ship_address
39
+ else
40
+ raise CanCan::AccessDenied
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,103 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class CheckoutsController < Spree::Api::BaseController
5
+ before_action :associate_user, only: :update
6
+ before_action :load_order_with_lock, only: [:next, :advance, :update]
7
+
8
+ include Spree::Core::ControllerHelpers::Auth
9
+ include Spree::Core::ControllerHelpers::Order
10
+ # This before_action comes from Spree::Core::ControllerHelpers::Order
11
+ skip_before_action :set_current_order
12
+
13
+ def next
14
+ authorize! :update, @order, order_token
15
+ @order.next!
16
+ respond_with(@order, default_template: 'spree/api/v1/orders/show', status: 200)
17
+ rescue StateMachines::InvalidTransition
18
+ respond_with(@order, default_template: 'spree/api/v1/orders/could_not_transition', status: 422)
19
+ end
20
+
21
+ def advance
22
+ authorize! :update, @order, order_token
23
+ while @order.next; end
24
+ respond_with(@order, default_template: 'spree/api/v1/orders/show', status: 200)
25
+ end
26
+
27
+ def update
28
+ authorize! :update, @order, order_token
29
+
30
+ if @order.update_from_params(params, permitted_checkout_attributes, request.headers.env)
31
+ if current_api_user.has_spree_role?('admin') && user_id.present?
32
+ @order.associate_user!(Spree.user_class.find(user_id))
33
+ end
34
+
35
+ return if after_update_attributes
36
+
37
+ if @order.completed? || @order.next
38
+ state_callback(:after)
39
+ respond_with(@order, default_template: 'spree/api/v1/orders/show')
40
+ else
41
+ respond_with(@order, default_template: 'spree/api/v1/orders/could_not_transition', status: 422)
42
+ end
43
+ else
44
+ invalid_resource!(@order)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def user_id
51
+ params[:order][:user_id] if params[:order]
52
+ end
53
+
54
+ def nested_params
55
+ map_nested_attributes_keys Order, params[:order] || {}
56
+ end
57
+
58
+ # Should be overriden if you have areas of your checkout that don't match
59
+ # up to a step within checkout_steps, such as a registration step
60
+ def skip_state_validation?
61
+ false
62
+ end
63
+
64
+ def load_order(lock = false)
65
+ @order = Spree::Order.lock(lock).find_by!(number: params[:id])
66
+ raise_insufficient_quantity and return if @order.insufficient_stock_lines.present?
67
+ @order.state = params[:state] if params[:state]
68
+ state_callback(:before)
69
+ end
70
+
71
+ def load_order_with_lock
72
+ load_order(true)
73
+ end
74
+
75
+ def raise_insufficient_quantity
76
+ respond_with(@order, default_template: 'spree/api/v1/orders/insufficient_quantity')
77
+ end
78
+
79
+ def state_callback(before_or_after = :before)
80
+ method_name = :"#{before_or_after}_#{@order.state}"
81
+ send(method_name) if respond_to?(method_name, true)
82
+ end
83
+
84
+ def after_update_attributes
85
+ if nested_params && nested_params[:coupon_code].present?
86
+ handler = PromotionHandler::Coupon.new(@order).apply
87
+
88
+ if handler.error.present?
89
+ @coupon_message = handler.error
90
+ respond_with(@order, default_template: 'spree/api/v1/orders/could_not_apply_coupon')
91
+ return true
92
+ end
93
+ end
94
+ false
95
+ end
96
+
97
+ def order_id
98
+ super || params[:id]
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,20 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ClassificationsController < Spree::Api::BaseController
5
+ def update
6
+ authorize! :update, Product
7
+ authorize! :update, Taxon
8
+ classification = Spree::Classification.find_by(
9
+ product_id: params[:product_id],
10
+ taxon_id: params[:taxon_id]
11
+ )
12
+ # Because position we get back is 0-indexed.
13
+ # acts_as_list is 1-indexed.
14
+ classification.insert_at(params[:position].to_i + 1)
15
+ render nothing: true
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class CountriesController < Spree::Api::BaseController
5
+ skip_before_action :authenticate_user
6
+
7
+ def index
8
+ @countries = Country.accessible_by(current_ability, :read).ransack(params[:q]).result.
9
+ order('name ASC').
10
+ page(params[:page]).per(params[:per_page])
11
+ country = Country.order("updated_at ASC").last
12
+ if stale?(country)
13
+ respond_with(@countries)
14
+ end
15
+ end
16
+
17
+ def show
18
+ @country = Country.accessible_by(current_ability, :read).find(params[:id])
19
+ respond_with(@country)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class CreditCardsController < Spree::Api::BaseController
5
+ before_action :user
6
+
7
+ def index
8
+ @credit_cards = user
9
+ .credit_cards
10
+ .accessible_by(current_ability, :read)
11
+ .with_payment_profile
12
+ .ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
13
+ respond_with(@credit_cards)
14
+ end
15
+
16
+ private
17
+
18
+ def user
19
+ if params[:user_id].present?
20
+ @user ||= Spree::user_class.accessible_by(current_ability, :read).find(params[:user_id])
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ImagesController < Spree::Api::BaseController
5
+ def index
6
+ @images = scope.images.accessible_by(current_ability, :read)
7
+ respond_with(@images)
8
+ end
9
+
10
+ def show
11
+ @image = Image.accessible_by(current_ability, :read).find(params[:id])
12
+ respond_with(@image)
13
+ end
14
+
15
+ def create
16
+ authorize! :create, Image
17
+ @image = scope.images.new(image_params)
18
+ if @image.save
19
+ respond_with(@image, status: 201, default_template: :show)
20
+ else
21
+ invalid_resource!(@image)
22
+ end
23
+ end
24
+
25
+ def update
26
+ @image = scope.images.accessible_by(current_ability, :update).find(params[:id])
27
+ if @image.update_attributes(image_params)
28
+ respond_with(@image, default_template: :show)
29
+ else
30
+ invalid_resource!(@image)
31
+ end
32
+ end
33
+
34
+ def destroy
35
+ @image = scope.images.accessible_by(current_ability, :destroy).find(params[:id])
36
+ @image.destroy
37
+ respond_with(@image, status: 204)
38
+ end
39
+
40
+ private
41
+
42
+ def image_params
43
+ params.require(:image).permit(permitted_image_attributes)
44
+ end
45
+
46
+ def scope
47
+ if params[:product_id]
48
+ Spree::Product.friendly.find(params[:product_id])
49
+ elsif params[:variant_id]
50
+ Spree::Variant.find(params[:variant_id])
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,54 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class InventoryUnitsController < Spree::Api::BaseController
5
+ before_action :prepare_event, only: :update
6
+
7
+ def show
8
+ @inventory_unit = inventory_unit
9
+ respond_with(@inventory_unit)
10
+ end
11
+
12
+ def update
13
+ authorize! :update, inventory_unit.order
14
+
15
+ inventory_unit.transaction do
16
+ if inventory_unit.update_attributes(inventory_unit_params)
17
+ fire
18
+ render :show, :status => 200
19
+ else
20
+ invalid_resource!(inventory_unit)
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def inventory_unit
28
+ @inventory_unit ||= InventoryUnit.accessible_by(current_ability, :read).find(params[:id])
29
+ end
30
+
31
+ def prepare_event
32
+ return unless @event = params[:fire]
33
+
34
+ can_event = "can_#{@event}?"
35
+
36
+ unless inventory_unit.respond_to?(can_event) &&
37
+ inventory_unit.send(can_event)
38
+ render :text => { :exception => "cannot transition to #{@event}" }.to_json,
39
+ :status => 200
40
+ false
41
+ end
42
+ end
43
+
44
+ def fire
45
+ inventory_unit.send("#{@event}!") if @event
46
+ end
47
+
48
+ def inventory_unit_params
49
+ params.require(:inventory_unit).permit(permitted_inventory_unit_attributes)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end