solidus_api_v2 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +38 -0
  5. data/Gemfile +13 -0
  6. data/LICENSE +26 -0
  7. data/README.md +98 -0
  8. data/Rakefile +37 -0
  9. data/app/controllers/concerns/spree/api/v2/renderable.rb +51 -0
  10. data/app/controllers/spree/api/v2/base_controller.rb +31 -0
  11. data/app/controllers/spree/api/v2/children_controller.rb +29 -0
  12. data/app/controllers/spree/api/v2/countries_controller.rb +21 -0
  13. data/app/controllers/spree/api/v2/images_controller.rb +29 -0
  14. data/app/controllers/spree/api/v2/line_items_controller.rb +36 -0
  15. data/app/controllers/spree/api/v2/option_types_controller.rb +31 -0
  16. data/app/controllers/spree/api/v2/option_values_controller.rb +29 -0
  17. data/app/controllers/spree/api/v2/orders_controller.rb +11 -0
  18. data/app/controllers/spree/api/v2/prices_controller.rb +27 -0
  19. data/app/controllers/spree/api/v2/products_controller.rb +37 -0
  20. data/app/controllers/spree/api/v2/states_controller.rb +29 -0
  21. data/app/controllers/spree/api/v2/taxonomies_controller.rb +21 -0
  22. data/app/controllers/spree/api/v2/taxons_controller.rb +40 -0
  23. data/app/controllers/spree/api/v2/variants_controller.rb +35 -0
  24. data/app/models/spree/base_decorator.rb +5 -0
  25. data/app/models/spree/image_decorator.rb +3 -0
  26. data/app/models/spree/price_decorator.rb +3 -0
  27. data/app/models/spree/state_decorator.rb +3 -0
  28. data/app/serializers/spree/address_serializer.rb +9 -0
  29. data/app/serializers/spree/base_serializer.rb +13 -0
  30. data/app/serializers/spree/country_serializer.rb +7 -0
  31. data/app/serializers/spree/error_serializer.rb +52 -0
  32. data/app/serializers/spree/image_serializer.rb +13 -0
  33. data/app/serializers/spree/line_item_serializer.rb +13 -0
  34. data/app/serializers/spree/option_type_serializer.rb +8 -0
  35. data/app/serializers/spree/option_value_serializer.rb +8 -0
  36. data/app/serializers/spree/order_serializer.rb +21 -0
  37. data/app/serializers/spree/price_serializer.rb +7 -0
  38. data/app/serializers/spree/product_serializer.rb +12 -0
  39. data/app/serializers/spree/role_serializer.rb +5 -0
  40. data/app/serializers/spree/state_serializer.rb +7 -0
  41. data/app/serializers/spree/store_serializer.rb +6 -0
  42. data/app/serializers/spree/taxon_serializer.rb +16 -0
  43. data/app/serializers/spree/taxonomy_serializer.rb +7 -0
  44. data/app/serializers/spree/user_serializer.rb +5 -0
  45. data/app/serializers/spree/variant_serializer.rb +12 -0
  46. data/bin/rails +7 -0
  47. data/circle.yml +13 -0
  48. data/config/locales/en.yml +25 -0
  49. data/config/routes.rb +53 -0
  50. data/docs/.nojekyll +0 -0
  51. data/docs/Dockerfile +12 -0
  52. data/docs/Gemfile +13 -0
  53. data/docs/README.md +12 -0
  54. data/docs/Rakefile +9 -0
  55. data/docs/config.rb +39 -0
  56. data/docs/font-selection.json +148 -0
  57. data/docs/source/fonts/slate.eot +0 -0
  58. data/docs/source/fonts/slate.svg +14 -0
  59. data/docs/source/fonts/slate.ttf +0 -0
  60. data/docs/source/fonts/slate.woff +0 -0
  61. data/docs/source/fonts/slate.woff2 +0 -0
  62. data/docs/source/images/logo.png +0 -0
  63. data/docs/source/images/navbar.png +0 -0
  64. data/docs/source/includes/_authentication.md +61 -0
  65. data/docs/source/includes/_countries.md +133 -0
  66. data/docs/source/includes/_errors.md +17 -0
  67. data/docs/source/includes/_filtering.md +11 -0
  68. data/docs/source/includes/_images.md +201 -0
  69. data/docs/source/includes/_line_items.md +137 -0
  70. data/docs/source/includes/_option_types.md +267 -0
  71. data/docs/source/includes/_option_values.md +227 -0
  72. data/docs/source/includes/_orders.md +75 -0
  73. data/docs/source/includes/_pagination.md +10 -0
  74. data/docs/source/includes/_prices.md +188 -0
  75. data/docs/source/includes/_products.md +403 -0
  76. data/docs/source/includes/_states.md +96 -0
  77. data/docs/source/includes/_taxonomies.md +325 -0
  78. data/docs/source/includes/_taxons.md +414 -0
  79. data/docs/source/includes/_variants.md +430 -0
  80. data/docs/source/index.md +53 -0
  81. data/docs/source/javascripts/all.js +4 -0
  82. data/docs/source/javascripts/all_nosearch.js +3 -0
  83. data/docs/source/javascripts/app/_lang.js +162 -0
  84. data/docs/source/javascripts/app/_search.js +74 -0
  85. data/docs/source/javascripts/app/_toc.js +55 -0
  86. data/docs/source/javascripts/lib/_energize.js +169 -0
  87. data/docs/source/javascripts/lib/_imagesloaded.min.js +7 -0
  88. data/docs/source/javascripts/lib/_jquery.highlight.js +108 -0
  89. data/docs/source/javascripts/lib/_jquery.tocify.js +1042 -0
  90. data/docs/source/javascripts/lib/_jquery_ui.js +566 -0
  91. data/docs/source/javascripts/lib/_lunr.js +1910 -0
  92. data/docs/source/layouts/layout.erb +102 -0
  93. data/docs/source/stylesheets/_icon-font.scss +38 -0
  94. data/docs/source/stylesheets/_normalize.css +427 -0
  95. data/docs/source/stylesheets/_syntax.scss.erb +27 -0
  96. data/docs/source/stylesheets/_variables.scss +109 -0
  97. data/docs/source/stylesheets/print.css.scss +142 -0
  98. data/docs/source/stylesheets/screen.css.scss +622 -0
  99. data/lib/solidus_api_v2.rb +5 -0
  100. data/lib/spree_api_v2/engine.rb +21 -0
  101. data/lib/spree_api_v2.rb +4 -0
  102. data/solidus_api_v2.gemspec +36 -0
  103. data/spec/controllers/spree/api/v2/children_controller_spec.rb +28 -0
  104. data/spec/controllers/spree/api/v2/countries_controller_spec.rb +25 -0
  105. data/spec/controllers/spree/api/v2/images_controller_spec.rb +93 -0
  106. data/spec/controllers/spree/api/v2/line_items_controller_spec.rb +86 -0
  107. data/spec/controllers/spree/api/v2/option_types_controller_spec.rb +73 -0
  108. data/spec/controllers/spree/api/v2/option_values_controller_spec.rb +88 -0
  109. data/spec/controllers/spree/api/v2/orders_controller_spec.rb +15 -0
  110. data/spec/controllers/spree/api/v2/prices_controller_spec.rb +55 -0
  111. data/spec/controllers/spree/api/v2/products_controller_spec.rb +95 -0
  112. data/spec/controllers/spree/api/v2/states_controller_spec.rb +42 -0
  113. data/spec/controllers/spree/api/v2/taxonomies_controller_spec.rb +31 -0
  114. data/spec/controllers/spree/api/v2/taxons_controller_spec.rb +54 -0
  115. data/spec/controllers/spree/api/v2/variants_controller_spec.rb +108 -0
  116. data/spec/models/spree/base_decorator_spec.rb +9 -0
  117. data/spec/models/spree/price_decorator_spec.rb +3 -0
  118. data/spec/serializers/spree/address_serializer_spec.rb +35 -0
  119. data/spec/serializers/spree/country_serializer_spec.rb +27 -0
  120. data/spec/serializers/spree/error_serializer_spec.rb +116 -0
  121. data/spec/serializers/spree/image_serializer_spec.rb +30 -0
  122. data/spec/serializers/spree/line_item_serializer_spec.rb +40 -0
  123. data/spec/serializers/spree/option_type_serializer_spec.rb +27 -0
  124. data/spec/serializers/spree/option_value_serializer_spec.rb +29 -0
  125. data/spec/serializers/spree/order_serializer_spec.rb +69 -0
  126. data/spec/serializers/spree/price_serializer_spec.rb +28 -0
  127. data/spec/serializers/spree/product_serializer_spec.rb +47 -0
  128. data/spec/serializers/spree/role_serializer_spec.rb +17 -0
  129. data/spec/serializers/spree/state_serializer_spec.rb +25 -0
  130. data/spec/serializers/spree/store_serializer_spec.rb +25 -0
  131. data/spec/serializers/spree/taxon_serializer_spec.rb +44 -0
  132. data/spec/serializers/spree/taxonomy_serializer_spec.rb +27 -0
  133. data/spec/serializers/spree/user_serializer_spec.rb +17 -0
  134. data/spec/serializers/spree/variant_serializer_spec.rb +55 -0
  135. data/spec/spec_helper.rb +51 -0
  136. data/spec/support/shoulda_matchers.rb +6 -0
  137. data/spree_api_v2.gemspec +36 -0
  138. metadata +437 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9a655640cb100a9789b9bb3405ffd13bc3d18ea
4
+ data.tar.gz: 2f83156cfb6c658aaea29beb12a4d1619a25bf73
5
+ SHA512:
6
+ metadata.gz: 34999a1b6484f224b0dca93da92d2838683fe11f55c0d966b44c99aa96ac1db8bc1f9658d4a218d4be11870e4517991c9b83f62bac899eec7d341d1f8aaf9def
7
+ data.tar.gz: 0bb5d23198292cf140e9a40cc946295c89902bfdaa1e0b4579d9a965dff2bb7a1f85a06c834f9f4ef6f664dcc5ec427790be83f07cb9b0a9fc3e44dd4a89321f
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ .sass-cache
8
+ coverage
9
+ Gemfile.lock
10
+ tmp
11
+ nbproject
12
+ pkg
13
+ *.swp
14
+ spec/dummy
15
+ build
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ * Calling `super` inside fo `#index` and `#show` actions is now deprecated.
6
+ Since these are methods that are meant to be called, they have been renamed to `#render_collection` and `#render_instance`.
7
+
8
+ [Ben A. Morgan](https://github.com/BenMorganIO)
9
+
10
+ * Added support for [Solidus](https://github.com/solidusio/solidus).
11
+
12
+ [Ben A. Morgan](https://github.com/BenMorganIO)
13
+
14
+ * In some instances, you would like to use the helper methods provided by the Spree V2 API, but unable to extend from the controller.
15
+ The render methods have been moved to a concern.
16
+ If you would like to include them in a controller without having to extend from the `Spree::Api::V2::BaseController`, then:
17
+
18
+ ```ruby
19
+ module Api
20
+ class MyController < Api::BaseController
21
+ # include the concern
22
+ include Spree::Api::V2::Renderable
23
+
24
+ # example usage of rendering all products without calling
25
+ # +render_instance+ but providing page detail information that conforms
26
+ # to the JSON API spec.
27
+ def index
28
+ render json: @products, meta: { page: page_details(@products) }
29
+ end
30
+ end
31
+ end
32
+ ```
33
+
34
+ [Ben A. Morgan](https://github.com/BenMorganIO)
35
+
36
+ ## 0.1.0
37
+
38
+ Initial Release
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ if ENV['SOLIDUS_API'].nil?
4
+ gem 'spree_api', github: 'spree', branch: '3-0-stable'
5
+ gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'
6
+
7
+ gemspec name: 'spree_api_v2'
8
+ else
9
+ gem 'solidus_api', github: 'solidusio/solidus'
10
+ gem 'solidus_auth_devise', github: 'solidusio/solidus_auth_devise'
11
+
12
+ gemspec name: 'solidus_api_v2'
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2015 Ben A. Morgan
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/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Spree::Api::V2
2
+
3
+ [![Circle CI](https://circleci.com/gh/spree-contrib/spree_api_v2/tree/master.svg?style=svg&circle-token=e2f6283b074ca9febcafab729d466ded2334300c)](https://circleci.com/gh/spree-contrib/spree_api_v2/tree/master)
4
+
5
+ The second version of the Spree API that's JSON API compatible.
6
+
7
+ Documentation: [http://spree-contrib.github.io/spree_api_v2](http://spree-contrib.github.io/spree_api_v2)
8
+
9
+ ## Installation
10
+
11
+ Add spree_api_v2 to your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'spree_api_v2', github: 'spree-contrib/spree_api_v2'
15
+ ```
16
+
17
+ ## Road to 1.0
18
+
19
+ ***
20
+ Please note that there actually has been a _lot_ of work done.
21
+ The lack of checked off boxes doesn't give this gem the respect it deserves.
22
+ ***
23
+
24
+ There's several things blocking 1.0.
25
+ Below, you'll see a bullet list of requirements that are both specific to the app, the JSON API spec, and third parties.
26
+
27
+ - [ ] Manage permissions with CanCanCan.
28
+ - [ ] Add support for XML.
29
+ - [ ] Add support for pagination.
30
+ - [x] `page` keyword support for pagination.
31
+ - [ ] `links` document at the root for `self`, `next`, `prev`, `last`, and `first`.
32
+ - [ ] Add support for filtration.
33
+ - [x] Be able to filter onto the main data object.
34
+ - [ ] Be able to filter the included relationships.
35
+ - [ ] Provide JSON API specific error objects. Provide support for:
36
+ - [ ] `id`
37
+ - [ ] `links`
38
+ - [ ] `about`
39
+ - [ ] `status`
40
+ - [ ] `code`
41
+ - [x] `title`
42
+ - [x] `detail`
43
+ - [ ] `source`
44
+ - [x] `pointer`
45
+ - [ ] `parameter`
46
+ - [x] `meta`
47
+ - [ ] Only accept data in the JSON API format.
48
+ - [ ] Respond with header `Content-Type: application/vnd.api+json`.
49
+ - [ ] Add support for sparse fieldsets.
50
+ - [ ] Add support for sorting.
51
+
52
+ | Resource | index | create | show | update | destroy |
53
+ |------------------|:-------:|:-------:|:-------:|:-------:|:-------:|
54
+ | Adjustment | [ ] | [ ] | [ ] | [ ] | [ ] |
55
+ | Address | [ ] | [ ] | [ ] | [ ] | [ ] |
56
+ | Checkout | [ ] | [ ] | [ ] | [ ] | [ ] |
57
+ | Country | [x] | [ ] | [x] | [ ] | [ ] |
58
+ | Image | [x] | [ ] | [x] | [ ] | [ ] |
59
+ | Line Item | [ ] | nw | [ ] | [ ] | [ ] |
60
+ | Option Type | [x] | [ ] | [x] | [ ] | [ ] |
61
+ | Option Value | [x] | [ ] | [x] | [ ] | [ ] |
62
+ | Order | [x] | [ ] | [x] | [ ] | [ ] |
63
+ | Payment | [ ] | [ ] | [ ] | [ ] | [ ] |
64
+ | Price | [x] | [ ] | [x] | [ ] | [ ] |
65
+ | Product | [x] | [ ] | [x] | [ ] | [ ] |
66
+ | Product Property | [ ] | [ ] | [ ] | [ ] | [ ] |
67
+ | Return Auth | [ ] | [ ] | [ ] | [ ] | [ ] |
68
+ | Shipment | [ ] | [ ] | [ ] | [ ] | [ ] |
69
+ | State | [x] | [ ] | [x] | [ ] | [ ] |
70
+ | Stock Location | [ ] | [ ] | [ ] | [ ] | [ ] |
71
+ | Stock Movement | [ ] | [ ] | [ ] | [ ] | [ ] |
72
+ | Stock Item | [ ] | [ ] | [ ] | [ ] | [ ] |
73
+ | Store | [ ] | [ ] | [ ] | [ ] | [ ] |
74
+ | Store Credit | [ ] | [ ] | [ ] | [ ] | [ ] |
75
+ | Tax Category | [ ] | [ ] | [ ] | [ ] | [ ] |
76
+ | Tax Rate | [ ] | [ ] | [ ] | [ ] | [ ] |
77
+ | Taxon | [x] | [ ] | [x] | [ ] | [ ] |
78
+ | Taxonomy | [x] | [ ] | [x] | [ ] | [ ] |
79
+ | User | [x] | [ ] | [x] | [ ] | [ ] |
80
+ | Variant | [x] | [ ] | [x] | [ ] | [ ] |
81
+ | Zone | [ ] | [ ] | [ ] | [ ] | [ ] |
82
+
83
+ ## Testing
84
+
85
+ First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run the specs.
86
+ The dummy app can be regenerated by using `rake test_app`.
87
+
88
+ ```shell
89
+ bundle
90
+ bundle exec rake GEMNAME=spree_api_v2
91
+ bundle exec rake GEMNAME=solidus_api_v2
92
+ ```
93
+
94
+ ## Sponsored by:
95
+
96
+ [![Kabuni](https://assets.kabuni.com/kabuni-secondary-logomark-xlarge.png)](https://kabuni.com?utm_source=SpreeApiV2&utm_medium=sponsor&utm_campaign=opensource)
97
+
98
+ Copyright &copy; 2015 Ben A. Morgan, released under the New BSD License
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks name: ENV['GEMNAME']
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ require 'generators/spree/dummy/dummy_generator'
7
+ require 'generators/spree/install/install_generator'
8
+
9
+ RSpec::Core::RakeTask.new
10
+
11
+ task :default do
12
+ if Dir['spec/dummy'].empty?
13
+ Rake::Task[:test_app].invoke
14
+ Dir.chdir('../../')
15
+ end
16
+ Rake::Task[:spec].invoke
17
+ end
18
+
19
+ desc 'Generates a dummy app for testing'
20
+ task :test_app do
21
+ ENV["RAILS_ENV"] = 'test'
22
+
23
+ Spree::DummyGeneratorHelper.inject_extension_requirements = true
24
+ Spree::DummyGenerator.start %w(--lib_name=spree_api_v2 --quiet)
25
+
26
+ Spree::InstallGenerator.class_eval do
27
+ def config_spree_yml() end
28
+ end
29
+
30
+ Spree::InstallGenerator.start %w(
31
+ --lib_name=spree_api_v2 --auto-accept --migrate=false --seed=false
32
+ --sample=false --quiet --user_class=Spree::LegacyUser
33
+ )
34
+
35
+ puts 'Setting up dummy database...'
36
+ system "bundle exec rake db:drop db:create db:migrate > #{File::NULL}"
37
+ end
@@ -0,0 +1,51 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Renderable
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+
9
+ def render_collection(collection = [])
10
+ collection = collection.where(filter_params).paginate(page_params)
11
+ render json: collection, include: params[:include],
12
+ meta: { page: page_details(collection) }
13
+ end
14
+
15
+ def render_instance(object = {})
16
+ render json: object, include: params[:include]
17
+ end
18
+
19
+ private
20
+
21
+ def error_response(resource)
22
+ Spree::ErrorSerializer.new(resource).as_json
23
+ end
24
+
25
+ def filter_params
26
+ params.fetch(:filter, {}).permit(filter_attributes << :id).transform_values do |value|
27
+ value.split(',')
28
+ end
29
+ end
30
+
31
+ def filter_attributes
32
+ serializer = "Spree::#{controller_name.camelize.singularize}Serializer"
33
+ serializer.constantize._attributes
34
+ end
35
+
36
+ def page_details(collection)
37
+ {
38
+ total_items: collection.total_count,
39
+ total_pages: collection.total_pages,
40
+ number: (page_params[:number] || 1).to_i,
41
+ size: (page_params[:size] || Kaminari.config.default_per_page).to_i
42
+ }
43
+ end
44
+
45
+ def page_params
46
+ params.fetch(:page, {}).permit(:number, :size)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class BaseController < Spree::Api::BaseController
5
+ include Spree::Api::V2::Renderable
6
+
7
+ protected
8
+
9
+ def index(collection = [])
10
+ ActiveSupport::Deprecation.warn <<-TEXT.squish
11
+ Using `super` is now deprecated. We're aiming to removing as much
12
+ meta-programming as possible. Please use the `render_collection`
13
+ method instead. This will be removed in 0.3.0.
14
+ TEXT
15
+
16
+ render_collection collection
17
+ end
18
+
19
+ def show(object = {})
20
+ ActiveSupport::Deprecation.warn <<-TEXT.squish
21
+ Using `super` is now deprecated. We're aiming to removing as much
22
+ meta-programming as possible. Please use the `render_instance`
23
+ method instead. This will be removed in 0.3.0.
24
+ TEXT
25
+
26
+ render_instance object
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class ChildrenController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection children.includes(:products, :taxonomy, :children, :parent)
9
+ end
10
+
11
+ def show
12
+ render_instance children.find(params[:id])
13
+ end
14
+
15
+ private
16
+
17
+ def children
18
+ if params[:taxon_id]
19
+ Spree::Taxon.find(params[:taxon_id])
20
+ end.children
21
+ end
22
+
23
+ def filter_attributes
24
+ Spree::TaxonSerializer._attributes
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class CountriesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection Spree::Country.includes(:states)
9
+ end
10
+
11
+ def show
12
+ if params[:state_id].present?
13
+ render_instance Spree::State.find_country(params[:state_id])
14
+ else
15
+ render_instance Spree::Country.find(params[:id])
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class ImagesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection images.includes(:viewable)
9
+ end
10
+
11
+ def show
12
+ render_instance images.find(params[:id])
13
+ end
14
+
15
+ private
16
+
17
+ def images
18
+ if params[:product_id]
19
+ Spree::Product.find(params[:product_id]).images
20
+ elsif params[:variant_id]
21
+ Spree::Variant.find(params[:variant_id]).images
22
+ else
23
+ Spree::Image
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class LineItemsController < Spree::Api::V2::BaseController
5
+ rescue_from ActiveRecord::RecordNotFound, with: :render_record_not_found_error
6
+ rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid_error
7
+ rescue_from RangeError, with: :render_range_error
8
+
9
+ def create
10
+ variant = Spree::Variant.find(line_item_params[:variant_id])
11
+ order = @current_api_user.orders.find(line_item_params[:order_id])
12
+ line_item = order.contents.add(variant, line_item_params[:quantity])
13
+ render_instance line_item
14
+ end
15
+
16
+ private
17
+
18
+ def line_item_params
19
+ params.require(:line_item).permit(:variant_id, :order_id, :quantity)
20
+ end
21
+
22
+ def render_range_error
23
+ render json: error_response(:quantity_too_high), status: 400
24
+ end
25
+
26
+ def render_record_invalid_error
27
+ render json: error_response(:product_out_of_stock), status: 400
28
+ end
29
+
30
+ def render_record_not_found_error
31
+ render json: error_response(:record_not_found), status: 400
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class OptionTypesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection option_types.includes(:option_values, :products)
9
+ end
10
+
11
+ def show
12
+ if params[:option_value_id]
13
+ render_instance Spree::OptionValue.find(params[:option_value_id]).option_type
14
+ else
15
+ render_instance option_types.find(params[:id])
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def option_types
22
+ if params[:product_id]
23
+ Spree::Product.find(params[:product_id]).option_types
24
+ else
25
+ Spree::OptionType
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class OptionValuesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection option_values.includes(:option_type, :variants)
9
+ end
10
+
11
+ def show
12
+ render_instance option_values.find(params[:id])
13
+ end
14
+
15
+ private
16
+
17
+ def option_values
18
+ if params[:option_type_id]
19
+ Spree::OptionType.find(params[:option_type_id]).option_values
20
+ elsif params[:variant_id]
21
+ Spree::Variant.find(params[:variant_id]).option_values
22
+ else
23
+ Spree::OptionValue
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class OrdersController < Spree::Api::V2::BaseController
5
+ def show
6
+ render_instance @current_api_user.orders.find(params[:id])
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class PricesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user
6
+
7
+ def index
8
+ render_collection params[:variant_id] ? prices : prices.includes(:variant)
9
+ end
10
+
11
+ def show
12
+ render_instance prices.find(params[:id])
13
+ end
14
+
15
+ private
16
+
17
+ def prices
18
+ if params[:variant_id]
19
+ Spree::Variant.find(params[:variant_id]).prices
20
+ else
21
+ Spree::Price
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class ProductsController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection products.includes :variants, :master, :taxons, :option_types, master: :images
9
+ end
10
+
11
+ def show
12
+ if params[:price_id]
13
+ render_instance Spree::Price.find(params[:price_id]).product
14
+ elsif params[:variant_id]
15
+ render_instance Spree::Variant.find(params[:variant_id]).product
16
+ elsif params[:image_id]
17
+ render_instance Spree::Image.variants.find(params[:image_id]).viewable.product
18
+ else
19
+ render_instance products.find(params[:id])
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def products
26
+ if params[:taxon_id].present?
27
+ Spree::Taxon.find(params[:taxon_id]).products
28
+ elsif params[:option_type_id]
29
+ Spree::OptionType.find(params[:option_type_id]).products
30
+ else
31
+ Spree::Product
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class StatesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user
6
+
7
+ def index
8
+ render_collection states
9
+ end
10
+
11
+ def show
12
+ render_instance states.find(params[:id])
13
+ end
14
+
15
+ private
16
+
17
+ def states
18
+ @states ||= begin
19
+ if params[:country_id].present?
20
+ Spree::Country.find(params[:country_id]).states.includes(:country)
21
+ else
22
+ Spree::State.includes(:country)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class TaxonomiesController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection Spree::Taxonomy.includes(:taxons)
9
+ end
10
+
11
+ def show
12
+ if params[:taxon_id].present?
13
+ render_instance Spree::Taxon.find(params[:taxon_id]).taxonomy
14
+ else
15
+ render_instance Spree::Taxonomy.find(params[:id])
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ class TaxonsController < Spree::Api::V2::BaseController
5
+ skip_before_action :authenticate_user, only: [:index, :show]
6
+
7
+ def index
8
+ render_collection taxons
9
+ end
10
+
11
+ def show
12
+ render_instance taxon
13
+ end
14
+
15
+ private
16
+
17
+ def taxon
18
+ if params[:taxon_id].present?
19
+ taxons.includes(parent: taxon_associations).find(params[:taxon_id]).parent
20
+ else
21
+ taxons.find(params[:id])
22
+ end
23
+ end
24
+
25
+ def taxons
26
+ if params[:taxonomy_id].present?
27
+ Spree::Taxonomy.find(params[:taxonomy_id]).taxons
28
+ .includes(*taxon_associations.-([:taxonomy]))
29
+ else
30
+ Spree::Taxon.includes(*taxon_associations)
31
+ end
32
+ end
33
+
34
+ def taxon_associations
35
+ [:products, :taxonomy, :parent]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end