spree_api_v1 4.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +189 -0
  3. data/.gitignore +23 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +24 -0
  6. data/CHANGELOG.md +3 -0
  7. data/CODE_OF_CONDUCT.md +22 -0
  8. data/Gemfile +59 -0
  9. data/LICENSE +26 -0
  10. data/README.md +62 -0
  11. data/Rakefile +15 -0
  12. data/app/controllers/spree/api/v1/addresses_controller.rb +46 -0
  13. data/app/controllers/spree/api/v1/base_controller.rb +174 -0
  14. data/app/controllers/spree/api/v1/checkouts_controller.rb +106 -0
  15. data/app/controllers/spree/api/v1/classifications_controller.rb +21 -0
  16. data/app/controllers/spree/api/v1/countries_controller.rb +22 -0
  17. data/app/controllers/spree/api/v1/credit_cards_controller.rb +26 -0
  18. data/app/controllers/spree/api/v1/customer_returns_controller.rb +25 -0
  19. data/app/controllers/spree/api/v1/images_controller.rb +58 -0
  20. data/app/controllers/spree/api/v1/inventory_units_controller.rb +54 -0
  21. data/app/controllers/spree/api/v1/line_items_controller.rb +70 -0
  22. data/app/controllers/spree/api/v1/option_types_controller.rb +60 -0
  23. data/app/controllers/spree/api/v1/option_values_controller.rb +62 -0
  24. data/app/controllers/spree/api/v1/orders_controller.rb +160 -0
  25. data/app/controllers/spree/api/v1/payments_controller.rb +82 -0
  26. data/app/controllers/spree/api/v1/product_properties_controller.rb +73 -0
  27. data/app/controllers/spree/api/v1/products_controller.rb +131 -0
  28. data/app/controllers/spree/api/v1/promotions_controller.rb +30 -0
  29. data/app/controllers/spree/api/v1/properties_controller.rb +70 -0
  30. data/app/controllers/spree/api/v1/reimbursements_controller.rb +25 -0
  31. data/app/controllers/spree/api/v1/return_authorizations_controller.rb +70 -0
  32. data/app/controllers/spree/api/v1/shipments_controller.rb +196 -0
  33. data/app/controllers/spree/api/v1/states_controller.rb +36 -0
  34. data/app/controllers/spree/api/v1/stock_items_controller.rb +82 -0
  35. data/app/controllers/spree/api/v1/stock_locations_controller.rb +53 -0
  36. data/app/controllers/spree/api/v1/stock_movements_controller.rb +45 -0
  37. data/app/controllers/spree/api/v1/stores_controller.rb +56 -0
  38. data/app/controllers/spree/api/v1/taxonomies_controller.rb +67 -0
  39. data/app/controllers/spree/api/v1/taxons_controller.rb +100 -0
  40. data/app/controllers/spree/api/v1/users_controller.rb +97 -0
  41. data/app/controllers/spree/api/v1/variants_controller.rb +81 -0
  42. data/app/controllers/spree/api/v1/zones_controller.rb +55 -0
  43. data/app/helpers/spree/api/api_helpers.rb +190 -0
  44. data/app/models/spree/api_v1_configuration.rb +5 -0
  45. data/app/views/spree/api/errors/gateway_error.rabl +2 -0
  46. data/app/views/spree/api/errors/invalid_api_key.rabl +2 -0
  47. data/app/views/spree/api/errors/invalid_resource.rabl +3 -0
  48. data/app/views/spree/api/errors/must_specify_api_key.rabl +2 -0
  49. data/app/views/spree/api/errors/not_found.rabl +2 -0
  50. data/app/views/spree/api/errors/unauthorized.rabl +2 -0
  51. data/app/views/spree/api/v1/addresses/show.rabl +10 -0
  52. data/app/views/spree/api/v1/adjustments/show.rabl +4 -0
  53. data/app/views/spree/api/v1/countries/index.rabl +7 -0
  54. data/app/views/spree/api/v1/countries/show.rabl +5 -0
  55. data/app/views/spree/api/v1/credit_cards/index.rabl +7 -0
  56. data/app/views/spree/api/v1/credit_cards/show.rabl +3 -0
  57. data/app/views/spree/api/v1/customer_returns/index.rabl +7 -0
  58. data/app/views/spree/api/v1/images/index.rabl +4 -0
  59. data/app/views/spree/api/v1/images/new.rabl +3 -0
  60. data/app/views/spree/api/v1/images/show.rabl +6 -0
  61. data/app/views/spree/api/v1/inventory_units/show.rabl +2 -0
  62. data/app/views/spree/api/v1/line_items/new.rabl +3 -0
  63. data/app/views/spree/api/v1/line_items/show.rabl +14 -0
  64. data/app/views/spree/api/v1/option_types/index.rabl +3 -0
  65. data/app/views/spree/api/v1/option_types/new.rabl +3 -0
  66. data/app/views/spree/api/v1/option_types/show.rabl +5 -0
  67. data/app/views/spree/api/v1/option_values/index.rabl +3 -0
  68. data/app/views/spree/api/v1/option_values/new.rabl +3 -0
  69. data/app/views/spree/api/v1/option_values/show.rabl +2 -0
  70. data/app/views/spree/api/v1/orders/address.rabl +0 -0
  71. data/app/views/spree/api/v1/orders/canceled.rabl +0 -0
  72. data/app/views/spree/api/v1/orders/cart.rabl +0 -0
  73. data/app/views/spree/api/v1/orders/complete.rabl +0 -0
  74. data/app/views/spree/api/v1/orders/could_not_apply_coupon.rabl +2 -0
  75. data/app/views/spree/api/v1/orders/could_not_transition.rabl +3 -0
  76. data/app/views/spree/api/v1/orders/index.rabl +7 -0
  77. data/app/views/spree/api/v1/orders/insufficient_quantity.rabl +2 -0
  78. data/app/views/spree/api/v1/orders/invalid_shipping_method.rabl +2 -0
  79. data/app/views/spree/api/v1/orders/mine.rabl +9 -0
  80. data/app/views/spree/api/v1/orders/order.rabl +10 -0
  81. data/app/views/spree/api/v1/orders/payment.rabl +3 -0
  82. data/app/views/spree/api/v1/orders/show.rabl +51 -0
  83. data/app/views/spree/api/v1/payments/credit_over_limit.rabl +2 -0
  84. data/app/views/spree/api/v1/payments/index.rabl +7 -0
  85. data/app/views/spree/api/v1/payments/new.rabl +5 -0
  86. data/app/views/spree/api/v1/payments/show.rabl +2 -0
  87. data/app/views/spree/api/v1/payments/update_forbidden.rabl +2 -0
  88. data/app/views/spree/api/v1/product_properties/index.rabl +7 -0
  89. data/app/views/spree/api/v1/product_properties/new.rabl +2 -0
  90. data/app/views/spree/api/v1/product_properties/show.rabl +2 -0
  91. data/app/views/spree/api/v1/products/index.rabl +9 -0
  92. data/app/views/spree/api/v1/products/new.rabl +3 -0
  93. data/app/views/spree/api/v1/products/product.rabl +1 -0
  94. data/app/views/spree/api/v1/products/show.rabl +36 -0
  95. data/app/views/spree/api/v1/promotions/handler.rabl +5 -0
  96. data/app/views/spree/api/v1/promotions/show.rabl +2 -0
  97. data/app/views/spree/api/v1/properties/index.rabl +7 -0
  98. data/app/views/spree/api/v1/properties/new.rabl +2 -0
  99. data/app/views/spree/api/v1/properties/show.rabl +2 -0
  100. data/app/views/spree/api/v1/reimbursements/index.rabl +7 -0
  101. data/app/views/spree/api/v1/return_authorizations/index.rabl +7 -0
  102. data/app/views/spree/api/v1/return_authorizations/new.rabl +3 -0
  103. data/app/views/spree/api/v1/return_authorizations/show.rabl +2 -0
  104. data/app/views/spree/api/v1/shared/stock_location_required.rabl +2 -0
  105. data/app/views/spree/api/v1/shipments/big.rabl +48 -0
  106. data/app/views/spree/api/v1/shipments/cannot_ready_shipment.rabl +2 -0
  107. data/app/views/spree/api/v1/shipments/mine.rabl +9 -0
  108. data/app/views/spree/api/v1/shipments/show.rabl +32 -0
  109. data/app/views/spree/api/v1/shipments/small.rabl +37 -0
  110. data/app/views/spree/api/v1/shipping_rates/show.rabl +2 -0
  111. data/app/views/spree/api/v1/states/index.rabl +12 -0
  112. data/app/views/spree/api/v1/states/show.rabl +2 -0
  113. data/app/views/spree/api/v1/stock_items/index.rabl +7 -0
  114. data/app/views/spree/api/v1/stock_items/show.rabl +5 -0
  115. data/app/views/spree/api/v1/stock_locations/index.rabl +7 -0
  116. data/app/views/spree/api/v1/stock_locations/show.rabl +8 -0
  117. data/app/views/spree/api/v1/stock_movements/index.rabl +7 -0
  118. data/app/views/spree/api/v1/stock_movements/show.rabl +5 -0
  119. data/app/views/spree/api/v1/stores/index.rabl +4 -0
  120. data/app/views/spree/api/v1/stores/show.rabl +2 -0
  121. data/app/views/spree/api/v1/tags/index.rabl +9 -0
  122. data/app/views/spree/api/v1/taxonomies/index.rabl +7 -0
  123. data/app/views/spree/api/v1/taxonomies/jstree.rabl +7 -0
  124. data/app/views/spree/api/v1/taxonomies/nested.rabl +11 -0
  125. data/app/views/spree/api/v1/taxonomies/new.rabl +3 -0
  126. data/app/views/spree/api/v1/taxonomies/show.rabl +15 -0
  127. data/app/views/spree/api/v1/taxons/index.rabl +10 -0
  128. data/app/views/spree/api/v1/taxons/jstree.rabl +7 -0
  129. data/app/views/spree/api/v1/taxons/new.rabl +3 -0
  130. data/app/views/spree/api/v1/taxons/show.rabl +6 -0
  131. data/app/views/spree/api/v1/taxons/taxons.rabl +5 -0
  132. data/app/views/spree/api/v1/users/index.rabl +7 -0
  133. data/app/views/spree/api/v1/users/new.rabl +3 -0
  134. data/app/views/spree/api/v1/users/show.rabl +11 -0
  135. data/app/views/spree/api/v1/variants/big.rabl +14 -0
  136. data/app/views/spree/api/v1/variants/index.rabl +9 -0
  137. data/app/views/spree/api/v1/variants/new.rabl +2 -0
  138. data/app/views/spree/api/v1/variants/show.rabl +3 -0
  139. data/app/views/spree/api/v1/variants/small.rabl +18 -0
  140. data/app/views/spree/api/v1/zones/index.rabl +7 -0
  141. data/app/views/spree/api/v1/zones/show.rabl +6 -0
  142. data/config/initializers/rabl.rb +9 -0
  143. data/config/routes.rb +123 -0
  144. data/lib/spree/api_v1/controller_setup.rb +19 -0
  145. data/lib/spree/api_v1/engine.rb +27 -0
  146. data/lib/spree/api_v1/factories.rb +6 -0
  147. data/lib/spree/api_v1/responders/rabl_template.rb +28 -0
  148. data/lib/spree/api_v1/responders.rb +11 -0
  149. data/lib/spree/api_v1/testing_support/helpers.rb +44 -0
  150. data/lib/spree/api_v1/testing_support/setup.rb +16 -0
  151. data/lib/spree/api_v1/version.rb +9 -0
  152. data/lib/spree/api_v1.rb +9 -0
  153. data/lib/spree_api_v1.rb +3 -0
  154. data/script/rails +9 -0
  155. data/spec/fixtures/files/icon_256x256.jpg +0 -0
  156. data/spec/fixtures/thinking-cat.jpg +0 -0
  157. data/spree_api_v1.gemspec +26 -0
  158. metadata +261 -0
@@ -0,0 +1,36 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class StatesController < Spree::Api::V1::BaseController
5
+ skip_before_action :authenticate_user
6
+
7
+ def index
8
+ @states = scope.ransack(params[:q]).result.includes(:country)
9
+
10
+ if params[:page] || params[:per_page]
11
+ @states = @states.page(params[:page]).per(params[:per_page])
12
+ end
13
+
14
+ state = @states.last
15
+ respond_with(@states) if stale?(state)
16
+ end
17
+
18
+ def show
19
+ @state = scope.find(params[:id])
20
+ respond_with(@state)
21
+ end
22
+
23
+ private
24
+
25
+ def scope
26
+ if params[:country_id]
27
+ @country = Country.accessible_by(current_ability, :show).find(params[:country_id])
28
+ @country.states.accessible_by(current_ability).order('name ASC')
29
+ else
30
+ State.accessible_by(current_ability).order('name ASC')
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,82 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class StockItemsController < Spree::Api::V1::BaseController
5
+ before_action :stock_location, except: [:update, :destroy]
6
+
7
+ def index
8
+ @stock_items = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
9
+ respond_with(@stock_items)
10
+ end
11
+
12
+ def show
13
+ @stock_item = scope.find(params[:id])
14
+ respond_with(@stock_item)
15
+ end
16
+
17
+ def create
18
+ authorize! :create, StockItem
19
+
20
+ count_on_hand = 0
21
+ if params[:stock_item].key?(:count_on_hand)
22
+ count_on_hand = params[:stock_item][:count_on_hand].to_i
23
+ end
24
+
25
+ @stock_item = scope.new(stock_item_params)
26
+ if @stock_item.save
27
+ @stock_item.adjust_count_on_hand(count_on_hand)
28
+ respond_with(@stock_item, status: 201, default_template: :show)
29
+ else
30
+ invalid_resource!(@stock_item)
31
+ end
32
+ end
33
+
34
+ def update
35
+ @stock_item = StockItem.accessible_by(current_ability, :update).find(params[:id])
36
+
37
+ if params[:stock_item].key?(:backorderable)
38
+ @stock_item.backorderable = params[:stock_item][:backorderable]
39
+ @stock_item.save
40
+ end
41
+
42
+ count_on_hand = 0
43
+ if params[:stock_item].key?(:count_on_hand)
44
+ count_on_hand = params[:stock_item][:count_on_hand].to_i
45
+ params[:stock_item].delete(:count_on_hand)
46
+ end
47
+
48
+ updated = params[:stock_item][:force] ? @stock_item.set_count_on_hand(count_on_hand)
49
+ : @stock_item.adjust_count_on_hand(count_on_hand)
50
+
51
+ if updated
52
+ respond_with(@stock_item, status: 200, default_template: :show)
53
+ else
54
+ invalid_resource!(@stock_item)
55
+ end
56
+ end
57
+
58
+ def destroy
59
+ @stock_item = StockItem.accessible_by(current_ability, :destroy).find(params[:id])
60
+ @stock_item.destroy
61
+ respond_with(@stock_item, status: 204)
62
+ end
63
+
64
+ private
65
+
66
+ def stock_location
67
+ render 'spree/api/v1/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
68
+ @stock_location ||= StockLocation.accessible_by(current_ability, :show).find(params[:stock_location_id])
69
+ end
70
+
71
+ def scope
72
+ includes = { variant: [{ option_values: :option_type }, :product] }
73
+ @stock_location.stock_items.accessible_by(current_ability, :show).includes(includes)
74
+ end
75
+
76
+ def stock_item_params
77
+ params.require(:stock_item).permit(permitted_stock_item_attributes)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,53 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class StockLocationsController < Spree::Api::V1::BaseController
5
+ def index
6
+ authorize! :index, StockLocation
7
+ @stock_locations = StockLocation.accessible_by(current_ability).order('name ASC').
8
+ ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
9
+ respond_with(@stock_locations)
10
+ end
11
+
12
+ def show
13
+ respond_with(stock_location)
14
+ end
15
+
16
+ def create
17
+ authorize! :create, StockLocation
18
+ @stock_location = StockLocation.new(stock_location_params)
19
+ if @stock_location.save
20
+ respond_with(@stock_location, status: 201, default_template: :show)
21
+ else
22
+ invalid_resource!(@stock_location)
23
+ end
24
+ end
25
+
26
+ def update
27
+ authorize! :update, stock_location
28
+ if stock_location.update(stock_location_params)
29
+ respond_with(stock_location, status: 200, default_template: :show)
30
+ else
31
+ invalid_resource!(stock_location)
32
+ end
33
+ end
34
+
35
+ def destroy
36
+ authorize! :destroy, stock_location
37
+ stock_location.destroy
38
+ respond_with(stock_location, status: 204)
39
+ end
40
+
41
+ private
42
+
43
+ def stock_location
44
+ @stock_location ||= StockLocation.accessible_by(current_ability, :show).find(params[:id])
45
+ end
46
+
47
+ def stock_location_params
48
+ params.require(:stock_location).permit(permitted_stock_location_attributes)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class StockMovementsController < Spree::Api::V1::BaseController
5
+ before_action :stock_location, except: [:update, :destroy]
6
+
7
+ def index
8
+ authorize! :index, StockMovement
9
+ @stock_movements = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
10
+ respond_with(@stock_movements)
11
+ end
12
+
13
+ def show
14
+ @stock_movement = scope.find(params[:id])
15
+ respond_with(@stock_movement)
16
+ end
17
+
18
+ def create
19
+ authorize! :create, StockMovement
20
+ @stock_movement = scope.new(stock_movement_params)
21
+ if @stock_movement.save
22
+ respond_with(@stock_movement, status: 201, default_template: :show)
23
+ else
24
+ invalid_resource!(@stock_movement)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def stock_location
31
+ render 'spree/api/v1/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
32
+ @stock_location ||= StockLocation.accessible_by(current_ability, :show).find(params[:stock_location_id])
33
+ end
34
+
35
+ def scope
36
+ @stock_location.stock_movements.accessible_by(current_ability, :show)
37
+ end
38
+
39
+ def stock_movement_params
40
+ params.require(:stock_movement).permit(permitted_stock_movement_attributes)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,56 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class StoresController < Spree::Api::V1::BaseController
5
+ before_action :get_store, except: [:index, :create]
6
+
7
+ def index
8
+ authorize! :index, Store
9
+ @stores = Store.accessible_by(current_ability).all
10
+ respond_with(@stores)
11
+ end
12
+
13
+ def create
14
+ authorize! :create, Store
15
+ @store = Store.new(store_params)
16
+ @store.code = params[:store][:code]
17
+ if @store.save
18
+ respond_with(@store, status: 201, default_template: :show)
19
+ else
20
+ invalid_resource!(@store)
21
+ end
22
+ end
23
+
24
+ def update
25
+ authorize! :update, @store
26
+ if @store.update(store_params)
27
+ respond_with(@store, status: 200, default_template: :show)
28
+ else
29
+ invalid_resource!(@store)
30
+ end
31
+ end
32
+
33
+ def show
34
+ authorize! :show, @store
35
+ respond_with(@store)
36
+ end
37
+
38
+ def destroy
39
+ authorize! :destroy, @store
40
+ @store.destroy
41
+ respond_with(@store, status: 204)
42
+ end
43
+
44
+ private
45
+
46
+ def get_store
47
+ @store = Store.find(params[:id])
48
+ end
49
+
50
+ def store_params
51
+ params.require(:store).permit(permitted_store_attributes)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,67 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class TaxonomiesController < Spree::Api::V1::BaseController
5
+ def index
6
+ respond_with(taxonomies)
7
+ end
8
+
9
+ def show
10
+ respond_with(taxonomy)
11
+ end
12
+
13
+ # Because JSTree wants parameters in a *slightly* different format
14
+ def jstree
15
+ show
16
+ end
17
+
18
+ def new; end
19
+
20
+ def create
21
+ authorize! :create, Taxonomy
22
+ @taxonomy = current_store.taxonomies.new(taxonomy_params)
23
+ if @taxonomy.save
24
+ respond_with(@taxonomy, status: 201, default_template: :show)
25
+ else
26
+ invalid_resource!(@taxonomy)
27
+ end
28
+ end
29
+
30
+ def update
31
+ authorize! :update, taxonomy
32
+ if taxonomy.update(taxonomy_params)
33
+ respond_with(taxonomy, status: 200, default_template: :show)
34
+ else
35
+ invalid_resource!(taxonomy)
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ authorize! :destroy, taxonomy
41
+ taxonomy.destroy
42
+ respond_with(taxonomy, status: 204)
43
+ end
44
+
45
+ private
46
+
47
+ def taxonomies
48
+ @taxonomies = Taxonomy.accessible_by(current_ability).order('name').includes(root: :children).
49
+ ransack(params[:q]).result.
50
+ page(params[:page]).per(params[:per_page])
51
+ end
52
+
53
+ def taxonomy
54
+ @taxonomy ||= Taxonomy.accessible_by(current_ability, :show).find(params[:id])
55
+ end
56
+
57
+ def taxonomy_params
58
+ if params[:taxonomy] && !params[:taxonomy].empty?
59
+ params.require(:taxonomy).permit(permitted_taxonomy_attributes)
60
+ else
61
+ {}
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,100 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class TaxonsController < Spree::Api::V1::BaseController
5
+ def index
6
+ @taxons = if taxonomy
7
+ taxonomy.root.children
8
+ elsif params[:ids]
9
+ Spree::Taxon.includes(:children).accessible_by(current_ability).where(id: params[:ids].split(','))
10
+ else
11
+ Spree::Taxon.includes(:children).accessible_by(current_ability).order(:taxonomy_id, :lft)
12
+ end
13
+ @taxons = @taxons.ransack(params[:q]).result
14
+ @taxons = @taxons.page(params[:page]).per(params[:per_page])
15
+ respond_with(@taxons)
16
+ end
17
+
18
+ def show
19
+ @taxon = taxon
20
+ respond_with(@taxon)
21
+ end
22
+
23
+ def jstree
24
+ show
25
+ end
26
+
27
+ def new; end
28
+
29
+ def create
30
+ authorize! :create, Taxon
31
+ @taxon = Spree::Taxon.new(taxon_params)
32
+ @taxon.taxonomy_id = params[:taxonomy_id]
33
+ taxonomy = Spree::Taxonomy.find_by(id: params[:taxonomy_id])
34
+
35
+ if taxonomy.nil?
36
+ @taxon.errors.add(:taxonomy_id, I18n.t('spree.api.invalid_taxonomy_id'))
37
+ invalid_resource!(@taxon) and return
38
+ end
39
+
40
+ @taxon.parent_id = taxonomy.root_id unless params[:taxon][:parent_id]
41
+
42
+ if @taxon.save
43
+ respond_with(@taxon, status: 201, default_template: :show)
44
+ else
45
+ invalid_resource!(@taxon)
46
+ end
47
+ end
48
+
49
+ def update
50
+ authorize! :update, taxon
51
+ if taxon.update(taxon_params)
52
+ respond_with(taxon, status: 200, default_template: :show)
53
+ else
54
+ invalid_resource!(taxon)
55
+ end
56
+ end
57
+
58
+ def destroy
59
+ authorize! :destroy, taxon
60
+ taxon.destroy
61
+ respond_with(taxon, status: 204)
62
+ end
63
+
64
+ def products
65
+ # Returns the products sorted by their position with the classification
66
+ # Products#index does not do the sorting.
67
+ taxon = Spree::Taxon.find(params[:id])
68
+ @products = taxon.products.ransack(params[:q]).result
69
+ @products = @products.page(params[:page]).per(params[:per_page] || 500)
70
+ render 'spree/api/v1/products/index'
71
+ end
72
+
73
+ private
74
+
75
+ def taxonomy
76
+ if params[:taxonomy_id].present?
77
+ @taxonomy ||=
78
+ if defined?(SpreeGlobalize)
79
+ Spree::Taxonomy.includes(:translations, taxons: [:translations]).accessible_by(current_ability, :show).find(params[:taxonomy_id])
80
+ else
81
+ Spree::Taxonomy.accessible_by(current_ability, :show).find(params[:taxonomy_id])
82
+ end
83
+ end
84
+ end
85
+
86
+ def taxon
87
+ @taxon ||= taxonomy.taxons.accessible_by(current_ability, :show).find(params[:id])
88
+ end
89
+
90
+ def taxon_params
91
+ if params[:taxon] && !params[:taxon].empty?
92
+ params.require(:taxon).permit(permitted_taxon_attributes)
93
+ else
94
+ {}
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,97 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class UsersController < Spree::Api::V1::BaseController
5
+ rescue_from Spree::Core::DestroyWithOrdersError, with: :error_during_processing
6
+
7
+ def index
8
+ users
9
+
10
+ if params[:ids]
11
+ load_users_by_ids
12
+ elsif params.dig(:q, :ship_address_firstname_start)
13
+ load_users_by_address
14
+ elsif params.dig(:q, :email_start)
15
+ load_users_by_email
16
+ end
17
+
18
+ prepare_index_response
19
+ respond_with(@users)
20
+ end
21
+
22
+ def users
23
+ @users ||= Spree.user_class.accessible_by(current_ability, :show)
24
+ end
25
+
26
+ def load_users_by_ids
27
+ @users = @users.where(id: params[:ids])
28
+ end
29
+
30
+ def load_users_by_address
31
+ address_params = params[:q][:ship_address_firstname_start] ||
32
+ params[:q][:ship_address_lastname_start] ||
33
+ params[:q][:bill_address_firstname_start] ||
34
+ params[:q][:bill_address_lastname_start]
35
+ @users = @users.with_email_or_address(params[:q][:email_start], address_params)
36
+ end
37
+
38
+ def load_users_by_email
39
+ @users = @users.with_email(params[:q][:email_start])
40
+ end
41
+
42
+ def paginate_users
43
+ @users = @users.page(params[:page]).per(params[:per_page])
44
+ end
45
+
46
+ def prepare_index_response
47
+ paginate_users
48
+ expires_in 15.minutes, public: true
49
+ headers['Surrogate-Control'] = "max-age=#{15.minutes}"
50
+ end
51
+
52
+ def show
53
+ respond_with(user)
54
+ end
55
+
56
+ def new; end
57
+
58
+ def create
59
+ authorize! :create, Spree.user_class
60
+ @user = Spree.user_class.new(user_params)
61
+ if @user.save
62
+ respond_with(@user, status: 201, default_template: :show)
63
+ else
64
+ invalid_resource!(@user)
65
+ end
66
+ end
67
+
68
+ def update
69
+ authorize! :update, user
70
+ if user.update(user_params)
71
+ respond_with(user, status: 200, default_template: :show)
72
+ else
73
+ invalid_resource!(user)
74
+ end
75
+ end
76
+
77
+ def destroy
78
+ authorize! :destroy, user
79
+ user.destroy
80
+ respond_with(user, status: 204)
81
+ end
82
+
83
+ private
84
+
85
+ def user
86
+ @user ||= Spree.user_class.accessible_by(current_ability, :show).find(params[:id])
87
+ end
88
+
89
+ def user_params
90
+ params.require(:user).permit(permitted_user_attributes |
91
+ [bill_address_attributes: permitted_address_attributes,
92
+ ship_address_attributes: permitted_address_attributes])
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,81 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class VariantsController < Spree::Api::V1::BaseController
5
+ before_action :product
6
+
7
+ def create
8
+ authorize! :create, Variant
9
+ @variant = scope.new(variant_params)
10
+ if @variant.save
11
+ respond_with(@variant, status: 201, default_template: :show)
12
+ else
13
+ invalid_resource!(@variant)
14
+ end
15
+ end
16
+
17
+ def destroy
18
+ @variant = scope.accessible_by(current_ability, :destroy).find(params[:id])
19
+ @variant.destroy
20
+ respond_with(@variant, status: 204)
21
+ end
22
+
23
+ # The lazyloaded associations here are pretty much attached to which nodes
24
+ # we render on the view so we better update it any time a node is included
25
+ # or removed from the views.
26
+ def index
27
+ @variants = scope.includes(*variant_includes).for_currency_and_available_price_amount.
28
+ ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
29
+ respond_with(@variants)
30
+ end
31
+
32
+ def new; end
33
+
34
+ def show
35
+ @variant = scope.includes(*variant_includes).find(params[:id])
36
+ respond_with(@variant)
37
+ end
38
+
39
+ def update
40
+ @variant = scope.accessible_by(current_ability, :update).find(params[:id])
41
+ if @variant.update(variant_params)
42
+ respond_with(@variant, status: 200, default_template: :show)
43
+ else
44
+ invalid_resource!(@product)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def product
51
+ if params[:product_id]
52
+ @product ||= Spree::Product.accessible_by(current_ability, :show).
53
+ friendly.find(params[:product_id])
54
+ end
55
+ end
56
+
57
+ def scope
58
+ variants = if @product
59
+ @product.variants_including_master
60
+ else
61
+ Variant
62
+ end
63
+
64
+ if current_ability.can?(:manage, Variant) && params[:show_deleted]
65
+ variants = variants.with_deleted
66
+ end
67
+
68
+ variants.eligible.accessible_by(current_ability)
69
+ end
70
+
71
+ def variant_params
72
+ params.require(:variant).permit(permitted_variant_attributes)
73
+ end
74
+
75
+ def variant_includes
76
+ [{ option_values: :option_type }, :product, :default_price, :images, { stock_items: :stock_location }]
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,55 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ZonesController < Spree::Api::V1::BaseController
5
+ def create
6
+ authorize! :create, Zone
7
+ @zone = Spree::Zone.new(zone_params)
8
+ if @zone.save
9
+ respond_with(@zone, status: 201, default_template: :show)
10
+ else
11
+ invalid_resource!(@zone)
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ authorize! :destroy, zone
17
+ zone.destroy
18
+ respond_with(zone, status: 204)
19
+ end
20
+
21
+ def index
22
+ @zones = Zone.accessible_by(current_ability).order('name ASC').ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
23
+ respond_with(@zones)
24
+ end
25
+
26
+ def show
27
+ respond_with(zone)
28
+ end
29
+
30
+ def update
31
+ authorize! :update, zone
32
+ if zone.update(zone_params)
33
+ respond_with(zone, status: 200, default_template: :show)
34
+ else
35
+ invalid_resource!(zone)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def zone_params
42
+ attrs = params.require(:zone).permit!.to_h
43
+ if attrs[:zone_members]
44
+ attrs[:zone_members_attributes] = attrs.delete(:zone_members)
45
+ end
46
+ attrs
47
+ end
48
+
49
+ def zone
50
+ @zone ||= Spree::Zone.accessible_by(current_ability, :show).find(params[:id])
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end