goca-spree-api 3.1.14.rails.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE +26 -0
- data/Rakefile +16 -0
- data/app/controllers/spree/api/base_controller.rb +167 -0
- data/app/controllers/spree/api/v1/addresses_controller.rb +46 -0
- data/app/controllers/spree/api/v1/checkouts_controller.rb +103 -0
- data/app/controllers/spree/api/v1/classifications_controller.rb +20 -0
- data/app/controllers/spree/api/v1/countries_controller.rb +24 -0
- data/app/controllers/spree/api/v1/credit_cards_controller.rb +27 -0
- data/app/controllers/spree/api/v1/images_controller.rb +56 -0
- data/app/controllers/spree/api/v1/inventory_units_controller.rb +54 -0
- data/app/controllers/spree/api/v1/line_items_controller.rb +70 -0
- data/app/controllers/spree/api/v1/option_types_controller.rb +51 -0
- data/app/controllers/spree/api/v1/option_values_controller.rb +60 -0
- data/app/controllers/spree/api/v1/orders_controller.rb +148 -0
- data/app/controllers/spree/api/v1/payments_controller.rb +83 -0
- data/app/controllers/spree/api/v1/product_properties_controller.rb +70 -0
- data/app/controllers/spree/api/v1/products_controller.rb +126 -0
- data/app/controllers/spree/api/v1/promotions_controller.rb +28 -0
- data/app/controllers/spree/api/v1/properties_controller.rb +72 -0
- data/app/controllers/spree/api/v1/return_authorizations_controller.rb +71 -0
- data/app/controllers/spree/api/v1/shipments_controller.rb +163 -0
- data/app/controllers/spree/api/v1/states_controller.rb +37 -0
- data/app/controllers/spree/api/v1/stock_items_controller.rb +77 -0
- data/app/controllers/spree/api/v1/stock_locations_controller.rb +52 -0
- data/app/controllers/spree/api/v1/stock_movements_controller.rb +45 -0
- data/app/controllers/spree/api/v1/stores_controller.rb +57 -0
- data/app/controllers/spree/api/v1/taxonomies_controller.rb +66 -0
- data/app/controllers/spree/api/v1/taxons_controller.rb +95 -0
- data/app/controllers/spree/api/v1/users_controller.rb +60 -0
- data/app/controllers/spree/api/v1/variants_controller.rb +75 -0
- data/app/controllers/spree/api/v1/zones_controller.rb +51 -0
- data/app/helpers/spree/api/api_helpers.rb +173 -0
- data/app/models/spree/api_configuration.rb +5 -0
- data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
- data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
- data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
- data/app/views/spree/api/v1/addresses/show.v1.rabl +10 -0
- data/app/views/spree/api/v1/adjustments/show.v1.rabl +4 -0
- data/app/views/spree/api/v1/config/money.v1.rabl +2 -0
- data/app/views/spree/api/v1/config/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/countries/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/countries/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/credit_cards/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/credit_cards/show.v1.rabl +3 -0
- data/app/views/spree/api/v1/images/index.v1.rabl +4 -0
- data/app/views/spree/api/v1/images/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/images/show.v1.rabl +6 -0
- data/app/views/spree/api/v1/inventory_units/show.rabl +2 -0
- data/app/views/spree/api/v1/line_items/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/line_items/show.v1.rabl +15 -0
- data/app/views/spree/api/v1/option_types/index.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_types/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_types/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/option_values/index.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_values/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_values/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/address.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/canceled.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/cart.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/complete.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/could_not_apply_coupon.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/could_not_transition.v1.rabl +3 -0
- data/app/views/spree/api/v1/orders/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/orders/invalid_shipping_method.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/mine.v1.rabl +9 -0
- data/app/views/spree/api/v1/orders/order.v1.rabl +10 -0
- data/app/views/spree/api/v1/orders/payment.v1.rabl +3 -0
- data/app/views/spree/api/v1/orders/show.v1.rabl +51 -0
- data/app/views/spree/api/v1/payments/credit_over_limit.v1.rabl +2 -0
- data/app/views/spree/api/v1/payments/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/payments/new.v1.rabl +5 -0
- data/app/views/spree/api/v1/payments/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/payments/update_forbidden.v1.rabl +2 -0
- data/app/views/spree/api/v1/product_properties/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/product_properties/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/product_properties/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/products/index.v1.rabl +9 -0
- data/app/views/spree/api/v1/products/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/products/product.v1.rabl +1 -0
- data/app/views/spree/api/v1/products/show.v1.rabl +32 -0
- data/app/views/spree/api/v1/promotions/handler.v1.rabl +5 -0
- data/app/views/spree/api/v1/promotions/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/properties/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/properties/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/properties/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/return_authorizations/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/return_authorizations/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/return_authorizations/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/shared/stock_location_required.v1.rabl +2 -0
- data/app/views/spree/api/v1/shipments/big.v1.rabl +48 -0
- data/app/views/spree/api/v1/shipments/cannot_ready_shipment.v1.rabl +2 -0
- data/app/views/spree/api/v1/shipments/mine.v1.rabl +9 -0
- data/app/views/spree/api/v1/shipments/show.v1.rabl +32 -0
- data/app/views/spree/api/v1/shipments/small.v1.rabl +37 -0
- data/app/views/spree/api/v1/shipping_rates/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/states/index.v1.rabl +14 -0
- data/app/views/spree/api/v1/states/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/stock_items/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_items/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/stock_locations/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_locations/show.v1.rabl +8 -0
- data/app/views/spree/api/v1/stock_movements/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_movements/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/stores/index.v1.rabl +4 -0
- data/app/views/spree/api/v1/stores/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/taxonomies/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/taxonomies/jstree.rabl +8 -0
- data/app/views/spree/api/v1/taxonomies/nested.v1.rabl +11 -0
- data/app/views/spree/api/v1/taxonomies/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/taxonomies/show.v1.rabl +15 -0
- data/app/views/spree/api/v1/taxons/index.v1.rabl +12 -0
- data/app/views/spree/api/v1/taxons/jstree.rabl +8 -0
- data/app/views/spree/api/v1/taxons/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/taxons/show.v1.rabl +6 -0
- data/app/views/spree/api/v1/taxons/taxons.v1.rabl +5 -0
- data/app/views/spree/api/v1/users/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/users/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/users/show.v1.rabl +10 -0
- data/app/views/spree/api/v1/variants/big.v1.rabl +14 -0
- data/app/views/spree/api/v1/variants/index.v1.rabl +9 -0
- data/app/views/spree/api/v1/variants/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/variants/show.v1.rabl +3 -0
- data/app/views/spree/api/v1/variants/small.v1.rabl +17 -0
- data/app/views/spree/api/v1/zones/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/zones/show.v1.rabl +6 -0
- data/config/initializers/metal_load_paths.rb +1 -0
- data/config/locales/en.yml +27 -0
- data/config/routes.rb +128 -0
- data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
- data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
- data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
- data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +7 -0
- data/lib/spree/api/controller_setup.rb +20 -0
- data/lib/spree/api/engine.rb +35 -0
- data/lib/spree/api/responders/rabl_template.rb +30 -0
- data/lib/spree/api/responders.rb +11 -0
- data/lib/spree/api/testing_support/caching.rb +10 -0
- data/lib/spree/api/testing_support/helpers.rb +44 -0
- data/lib/spree/api/testing_support/setup.rb +16 -0
- data/lib/spree/api.rb +10 -0
- data/lib/spree_api.rb +2 -0
- data/script/rails +9 -0
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spree_api.gemspec +20 -0
- metadata +221 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '017008a6e01cd473ce0ae66f63d77d694d567dac'
|
4
|
+
data.tar.gz: 50c0d18d0e5fb4d58eb8f64dd03641edd6deceeb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2b8942053b8005824019e90b6985789d2e1dc1d7b891e8120b7fa54a06513c1ab7a682bdb38c91f3a64a8eb179e8a8a01e9bd3b2a79c9066c66b5cbeebecfa1
|
7
|
+
data.tar.gz: 0115a87496fd609a4a6e01fe4aeda894355bc18fc38fbaca3653de03c7a130a9c525712c307cd59858618c2e4e8001989e2b925a29b1a9743153e6ea95648032
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2007-2015, Spree Commerce, Inc. and other contributors
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name Spree nor the names of its contributors may be used to
|
13
|
+
endorse or promote products derived from this software without specific
|
14
|
+
prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'spree/testing_support/common_rake'
|
6
|
+
require 'rails/all'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
|
12
|
+
desc "Generates a dummy app for testing"
|
13
|
+
task :test_app do
|
14
|
+
ENV['LIB_NAME'] = 'spree/api'
|
15
|
+
Rake::Task['common:test_app'].invoke
|
16
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require_dependency 'spree/api/controller_setup'
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Api
|
5
|
+
class BaseController < ActionController::Base
|
6
|
+
include Spree::Api::ControllerSetup
|
7
|
+
include Spree::Core::ControllerHelpers::Store
|
8
|
+
include Spree::Core::ControllerHelpers::StrongParameters
|
9
|
+
|
10
|
+
attr_accessor :current_api_user
|
11
|
+
|
12
|
+
before_action :set_content_type
|
13
|
+
before_action :load_user
|
14
|
+
before_action :authorize_for_order, if: Proc.new { order_token.present? }
|
15
|
+
before_action :authenticate_user
|
16
|
+
before_action :load_user_roles
|
17
|
+
|
18
|
+
rescue_from ActionController::ParameterMissing, with: :error_during_processing
|
19
|
+
rescue_from ActiveRecord::RecordInvalid, with: :error_during_processing
|
20
|
+
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
21
|
+
rescue_from CanCan::AccessDenied, with: :unauthorized
|
22
|
+
rescue_from Spree::Core::GatewayError, with: :gateway_error
|
23
|
+
|
24
|
+
helper Spree::Api::ApiHelpers
|
25
|
+
|
26
|
+
def map_nested_attributes_keys(klass, attributes)
|
27
|
+
nested_keys = klass.nested_attributes_options.keys
|
28
|
+
attributes.inject({}) do |h, (k,v)|
|
29
|
+
key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
|
30
|
+
h[key] = v
|
31
|
+
h
|
32
|
+
end.with_indifferent_access
|
33
|
+
end
|
34
|
+
|
35
|
+
# users should be able to set price when importing orders via api
|
36
|
+
def permitted_line_item_attributes
|
37
|
+
if @current_user_roles.include?("admin")
|
38
|
+
super + [:price, :variant_id, :sku]
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def content_type
|
45
|
+
case params[:format]
|
46
|
+
when "json"
|
47
|
+
"application/json; charset=utf-8"
|
48
|
+
when "xml"
|
49
|
+
"text/xml; charset=utf-8"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def set_content_type
|
56
|
+
headers["Content-Type"] = content_type
|
57
|
+
end
|
58
|
+
|
59
|
+
def load_user
|
60
|
+
@current_api_user = Spree.user_class.find_by(spree_api_key: api_key.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def authenticate_user
|
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
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def load_user_roles
|
77
|
+
@current_user_roles = @current_api_user ? @current_api_user.spree_roles.pluck(:name) : []
|
78
|
+
end
|
79
|
+
|
80
|
+
def unauthorized
|
81
|
+
render "spree/api/errors/unauthorized", status: 401 and return
|
82
|
+
end
|
83
|
+
|
84
|
+
def error_during_processing(exception)
|
85
|
+
Rails.logger.error exception.message
|
86
|
+
Rails.logger.error exception.backtrace.join("\n")
|
87
|
+
|
88
|
+
unprocessable_entity(exception.message)
|
89
|
+
end
|
90
|
+
|
91
|
+
def unprocessable_entity(message)
|
92
|
+
render text: { exception: message }.to_json, status: 422
|
93
|
+
end
|
94
|
+
|
95
|
+
def gateway_error(exception)
|
96
|
+
@order.errors.add(:base, exception.message)
|
97
|
+
invalid_resource!(@order)
|
98
|
+
end
|
99
|
+
|
100
|
+
def requires_authentication?
|
101
|
+
Spree::Api::Config[:requires_authentication]
|
102
|
+
end
|
103
|
+
|
104
|
+
def not_found
|
105
|
+
render "spree/api/errors/not_found", status: 404 and return
|
106
|
+
end
|
107
|
+
|
108
|
+
def current_ability
|
109
|
+
Spree::Ability.new(current_api_user)
|
110
|
+
end
|
111
|
+
|
112
|
+
def invalid_resource!(resource)
|
113
|
+
@resource = resource
|
114
|
+
render "spree/api/errors/invalid_resource", status: 422
|
115
|
+
end
|
116
|
+
|
117
|
+
def api_key
|
118
|
+
request.headers["X-Spree-Token"] || params[:token]
|
119
|
+
end
|
120
|
+
helper_method :api_key
|
121
|
+
|
122
|
+
def order_token
|
123
|
+
request.headers["X-Spree-Order-Token"] || params[:order_token]
|
124
|
+
end
|
125
|
+
|
126
|
+
def find_product(id)
|
127
|
+
product_scope.friendly.find(id.to_s)
|
128
|
+
rescue ActiveRecord::RecordNotFound
|
129
|
+
product_scope.find(id)
|
130
|
+
end
|
131
|
+
|
132
|
+
def product_scope
|
133
|
+
if @current_user_roles.include?("admin")
|
134
|
+
scope = Product.with_deleted.accessible_by(current_ability, :read).includes(*product_includes)
|
135
|
+
|
136
|
+
unless params[:show_deleted]
|
137
|
+
scope = scope.not_deleted
|
138
|
+
end
|
139
|
+
unless params[:show_discontinued]
|
140
|
+
scope = scope.not_discontinued
|
141
|
+
end
|
142
|
+
else
|
143
|
+
scope = Product.accessible_by(current_ability, :read).active.includes(*product_includes)
|
144
|
+
end
|
145
|
+
|
146
|
+
scope
|
147
|
+
end
|
148
|
+
|
149
|
+
def variants_associations
|
150
|
+
[{ option_values: :option_type }, :default_price, :images]
|
151
|
+
end
|
152
|
+
|
153
|
+
def product_includes
|
154
|
+
[:option_types, :taxons, product_properties: :property, variants: variants_associations, master: variants_associations]
|
155
|
+
end
|
156
|
+
|
157
|
+
def order_id
|
158
|
+
params[:order_id] || params[:checkout_id] || params[:order_number]
|
159
|
+
end
|
160
|
+
|
161
|
+
def authorize_for_order
|
162
|
+
@order = Spree::Order.find_by(number: order_id)
|
163
|
+
authorize! :read, @order, order_token
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -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
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class LineItemsController < Spree::Api::BaseController
|
5
|
+
class_attribute :line_item_options
|
6
|
+
|
7
|
+
self.line_item_options = []
|
8
|
+
|
9
|
+
def create
|
10
|
+
variant = Spree::Variant.find(params[:line_item][:variant_id])
|
11
|
+
@line_item = order.contents.add(
|
12
|
+
variant,
|
13
|
+
params[:line_item][:quantity] || 1,
|
14
|
+
line_item_params[:options] || {}
|
15
|
+
)
|
16
|
+
|
17
|
+
if @line_item.errors.empty?
|
18
|
+
respond_with(@line_item, status: 201, default_template: :show)
|
19
|
+
else
|
20
|
+
invalid_resource!(@line_item)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def update
|
25
|
+
@line_item = find_line_item
|
26
|
+
if @order.contents.update_cart(line_items_attributes)
|
27
|
+
@line_item.reload
|
28
|
+
respond_with(@line_item, default_template: :show)
|
29
|
+
else
|
30
|
+
invalid_resource!(@line_item)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy
|
35
|
+
@line_item = find_line_item
|
36
|
+
@order.contents.remove_line_item(@line_item)
|
37
|
+
respond_with(@line_item, status: 204)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def order
|
42
|
+
@order ||= Spree::Order.includes(:line_items).find_by!(number: order_id)
|
43
|
+
authorize! :update, @order, order_token
|
44
|
+
end
|
45
|
+
|
46
|
+
def find_line_item
|
47
|
+
id = params[:id].to_i
|
48
|
+
order.line_items.detect { |line_item| line_item.id == id } or
|
49
|
+
raise ActiveRecord::RecordNotFound
|
50
|
+
end
|
51
|
+
|
52
|
+
def line_items_attributes
|
53
|
+
{line_items_attributes: {
|
54
|
+
id: params[:id],
|
55
|
+
quantity: params[:line_item][:quantity],
|
56
|
+
options: line_item_params[:options] || {}
|
57
|
+
}}
|
58
|
+
end
|
59
|
+
|
60
|
+
def line_item_params
|
61
|
+
params.require(:line_item).permit(
|
62
|
+
:quantity,
|
63
|
+
:variant_id,
|
64
|
+
options: line_item_options
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|