kms_shop 0.1.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.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +41 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/kms_shop_manifest.js +2 -0
  6. data/app/assets/javascripts/kms_shop/application.js +15 -0
  7. data/app/assets/javascripts/kms_shop/application/controllers/order_controller.coffee.erb +27 -0
  8. data/app/assets/javascripts/kms_shop/application/routes.coffee.erb +17 -0
  9. data/app/assets/javascripts/templates/help/shop_endpoints.html.slim +81 -0
  10. data/app/assets/javascripts/templates/help/shop_variables.html.slim +45 -0
  11. data/app/assets/javascripts/templates/orders/index.html.slim +62 -0
  12. data/app/assets/stylesheets/kms_shop/application.css +15 -0
  13. data/app/controllers/concerns/kms/current_cart.rb +22 -0
  14. data/app/controllers/kms/public/cart_controller.rb +21 -0
  15. data/app/controllers/kms/public/checkout_controller.rb +24 -0
  16. data/app/controllers/kms/public/pages_controller_decorator.rb +4 -0
  17. data/app/controllers/kms/shop/orders_controller.rb +30 -0
  18. data/app/models/kms/cart.rb +14 -0
  19. data/app/models/kms/customer.rb +5 -0
  20. data/app/models/kms/line_item.rb +10 -0
  21. data/app/models/kms/order.rb +10 -0
  22. data/app/serializers/kms/cart_serializer.rb +6 -0
  23. data/app/serializers/kms/line_item_serializer.rb +13 -0
  24. data/app/serializers/kms/order_serializer.rb +11 -0
  25. data/app/services/kms/cart_finalizer.rb +17 -0
  26. data/app/services/kms/cart_populator.rb +34 -0
  27. data/config/initializers/externals.rb +1 -0
  28. data/config/initializers/help.rb +1 -0
  29. data/config/initializers/resources.rb +1 -0
  30. data/config/locales/en.yml +48 -0
  31. data/config/locales/ru.yml +48 -0
  32. data/config/routes.rb +9 -0
  33. data/db/migrate/20170104092456_create_kms_carts.rb +8 -0
  34. data/db/migrate/20170104093823_create_kms_line_items.rb +14 -0
  35. data/db/migrate/20170110130010_create_kms_orders.rb +10 -0
  36. data/db/migrate/20170118132726_create_kms_customers.rb +11 -0
  37. data/db/migrate/20170118141901_add_customer_id_to_orders.rb +6 -0
  38. data/lib/drops/kms/cart_drop.rb +10 -0
  39. data/lib/drops/kms/line_item_drop.rb +17 -0
  40. data/lib/generators/kms_shop/install/install_generator.rb +15 -0
  41. data/lib/kms/shop/engine.rb +19 -0
  42. data/lib/kms/shop/version.rb +5 -0
  43. data/lib/kms_shop.rb +6 -0
  44. data/spec/controllers/kms/public/cart_controller_spec.rb +37 -0
  45. data/spec/controllers/kms/public/checkout_controller_spec.rb +29 -0
  46. data/spec/controllers/kms/shop/orders_controller_spec.rb +47 -0
  47. data/spec/drops/kms/cart_drop_spec.rb +25 -0
  48. data/spec/factories/carts.rb +10 -0
  49. data/spec/factories/customers.rb +7 -0
  50. data/spec/factories/line_items.rb +7 -0
  51. data/spec/factories/orders.rb +6 -0
  52. data/spec/factories/products.rb +6 -0
  53. data/spec/factories/users.rb +7 -0
  54. data/spec/factories/variants.rb +7 -0
  55. data/spec/internal/Rakefile +2 -0
  56. data/spec/internal/config/database.yml +7 -0
  57. data/spec/internal/config/database.yml.travis +7 -0
  58. data/spec/internal/config/initializers/devise.rb +257 -0
  59. data/spec/internal/config/routes.rb +3 -0
  60. data/spec/internal/db/schema.rb +100 -0
  61. data/spec/internal/log/test.log +44132 -0
  62. data/spec/internal/public/favicon.ico +0 -0
  63. data/spec/models/kms/cart_spec.rb +21 -0
  64. data/spec/models/kms/customer_spec.rb +7 -0
  65. data/spec/models/kms/line_item_spec.rb +20 -0
  66. data/spec/models/kms/order_spec.rb +16 -0
  67. data/spec/services/kms/cart_finalizer_spec.rb +38 -0
  68. data/spec/services/kms/cart_populator_spec.rb +39 -0
  69. data/spec/spec_helper.rb +32 -0
  70. data/spec/support/controller_macros.rb +11 -0
  71. data/spec/support/request_helpers.rb +7 -0
  72. metadata +238 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32ebe31498458213250d098184a0e9cfda1f53c1
4
+ data.tar.gz: b7e501125aaedc4ad1ad5a088f4716135761367c
5
+ SHA512:
6
+ metadata.gz: 708cbaa45168830660519e4fb7ada2bea9c58caa16ce5635146f08c0934b1bdf48d7c6825e99c47a839f787f403c553e85495b7eb33fabd87452f8852057ab2e
7
+ data.tar.gz: 4c45a9248b8646950b9064fac90b41515ea6b831331ecef0096b8c318be24b4b708aa3c89e126a07c4e4a234890d60a45648de800ce3f57b19847ea549f86d5f
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Igor Petrov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # KMS Shop
2
+ [![Build Status](https://travis-ci.org/webgradus/kms_shop.svg?branch=master)](https://travis-ci.org/webgradus/kms_shop)
3
+
4
+ Extends KMS Catalog by adding cart and orders feature.
5
+
6
+ ## Installation
7
+
8
+ 1. Add to Gemfile
9
+
10
+ gem "kms_shop"
11
+ # or for edge version:
12
+ gem "kms_shop", github: "webgradus/kms_shop"
13
+
14
+ 2. Run generator:
15
+
16
+ bundle exec rails g kms_shop:install
17
+
18
+ 3. Copy migrations:
19
+
20
+ bundle exec rails kms_shop:install:migrations
21
+
22
+ 4. Migrate:
23
+
24
+ bundle exec rails db:migrate
25
+
26
+ 5. Recompile assets (if on production):
27
+
28
+ bundle exec rails assets:precompile
29
+
30
+ 6. Restart KMS instance
31
+
32
+ ## Usage
33
+
34
+ Once install please check "Help" page at the bottom of sidebar
35
+
36
+ ## Contributing
37
+
38
+ Please follow [CONTRIBUTING.md](CONTRIBUTING.md).
39
+
40
+ ## License
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'KmsCart'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/internal/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/kms_shop .js
2
+ //= link_directory ../stylesheets/kms_shop .css
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require "./application/routes"
14
+ //= require_tree "../templates"
15
+ //= require_tree "./application/controllers"
@@ -0,0 +1,27 @@
1
+ OrdersController = ($scope, $state, $cookieStore, Restangular, $stateParams, Alertify, ErrorsService) ->
2
+ $scope.store = Restangular.all('orders')
3
+
4
+ $scope.store.getList().then (orders)->
5
+ $scope.orders = orders
6
+
7
+ $scope.update = (data, order, line_item)->
8
+ if line_item
9
+ line_item.quantity = data
10
+ order.cart_attributes = {id: order.cart.id, line_items_attributes: order.line_items}
11
+ order.customer.name = data.name
12
+ order.customer.phone = data.phone
13
+ order.customer.email = data.email
14
+ order.customer_attributes = order.customer
15
+ order.put().then (updated_order)->
16
+ $state.reload()
17
+ ,(response)->
18
+ Alertify.error(ErrorsService.prepareErrorsString(response.data.errors))
19
+
20
+ $scope.destroy = (order)->
21
+ if(confirm('<%= I18n.t(:are_you_sure) %>'))
22
+ order.remove().then ->
23
+ $scope.orders = _.without($scope.orders, order)
24
+
25
+
26
+ angular.module('KMS')
27
+ .controller('OrdersController', ['$scope', '$state', '$cookieStore', 'Restangular', '$stateParams', 'Alertify', 'ErrorsService', OrdersController])
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ angular.module('KMS').config ['$stateProvider', '$urlRouterProvider', ($stateProvider, $urlRouterProvider) ->
4
+
5
+ # Application routes
6
+ $stateProvider
7
+ .state('orders', {
8
+ url: '/kms/orders',
9
+ views:
10
+ "header":
11
+ template: "<%= Kms::Order.model_name.human(count: 1.1) %>"
12
+ "@":
13
+ controller: 'OrdersController',
14
+ controllerAs: 'orders',
15
+ templateUrl: 'orders/index.html'
16
+ })
17
+ ]
@@ -0,0 +1,81 @@
1
+ h4 = I18n.t("liquor_help.endpoints_title")
2
+ p
3
+ ul
4
+ li
5
+ var PUT /cart
6
+ p
7
+ span = I18n.t('liquor_help.endpoints.cart.put.main_description')
8
+ p
9
+ code ng-non-bindable=''
10
+ | &lt;form action="/cart" method="post"&gt;
11
+ br
12
+ | &lt;input type="hidden" name='authenticity_token' value='{{ request.form_authenticity_token }}'&gt;
13
+ br
14
+ | &lt;input type="hidden" name="_method" value="put"&gt;
15
+ br
16
+ | &lt;input type="hidden" name="variant_id" value="{{ product.master.id }}"&gt;
17
+ br
18
+ | &lt;input type="number" name="quantity" min="1" value="1"&gt;
19
+ br
20
+ | &lt;input type="submit" value="Add to cart"&gt;
21
+ br
22
+ | &lt;/form&gt;
23
+ p
24
+ table.table
25
+ tr
26
+ th #{ I18n.t('liquor_help.parameter') } /cart
27
+ th = I18n.t('liquor_help.description')
28
+ tr
29
+ td variant_id
30
+ td = I18n.t('liquor_help.endpoints.cart.put.parameters.variant_id')
31
+ tr
32
+ td quantity
33
+ td = I18n.t('liquor_help.endpoints.cart.put.parameters.quantity')
34
+ li
35
+ var DELETE /cart
36
+ p
37
+ span = I18n.t('liquor_help.endpoints.cart.delete.main_description')
38
+ p
39
+ code ng-non-bindable=''
40
+ | &lt;form action="/cart" method="post"&gt;
41
+ br
42
+ | &lt;input type="hidden" name='authenticity_token' value='{{ request.form_authenticity_token }}'&gt;
43
+ br
44
+ | &lt;input type="hidden" name="_method" value="delete"&gt;
45
+ br
46
+ | &lt;input type="submit" value="Clear cart"&gt;
47
+ br
48
+ | &lt;/form&gt;
49
+ li
50
+ var POST /checkout
51
+ p
52
+ span = I18n.t('liquor_help.endpoints.checkout.post.main_description')
53
+ p
54
+ code ng-non-bindable=''
55
+ | &lt;form action="/checkout" method="post"&gt;
56
+ br
57
+ | &lt;input type="hidden" name='authenticity_token' value='{{ request.form_authenticity_token }}'&gt;
58
+ br
59
+ | &lt;input type="text" name="customer[name]" placeholder="Name" required&gt;
60
+ br
61
+ | &lt;input type="email" name="customer[email]" placeholder="Email"&gt;
62
+ br
63
+ | &lt;input type="text" name="customer[phone]" placeholder="Phone"&gt;
64
+ br
65
+ | &lt;input type="submit" value="Checkout"&gt;
66
+ br
67
+ | &lt;/form&gt;
68
+ p
69
+ table.table
70
+ tr
71
+ th #{ I18n.t('liquor_help.parameter') } /checkout
72
+ th = I18n.t('liquor_help.description')
73
+ tr
74
+ td customer[name]
75
+ td = I18n.t('liquor_help.endpoints.checkout.post.parameters.customer.name')
76
+ tr
77
+ td customer[email]
78
+ td = I18n.t('liquor_help.endpoints.checkout.post.parameters.customer.email')
79
+ tr
80
+ td customer[phone]
81
+ td = I18n.t('liquor_help.endpoints.checkout.post.parameters.customer.phone')
@@ -0,0 +1,45 @@
1
+ h4 = I18n.t("liquor_help.variables_title")
2
+ p
3
+ ul
4
+ li
5
+ var cart
6
+ p
7
+ span = I18n.t('liquor_help.variables.cart.main_description')
8
+ p
9
+ code ng-non-bindable=''
10
+ | {{ cart.total }}
11
+ p
12
+ table.table
13
+ tr
14
+ th #{ I18n.t('liquor_help.property') } Cart
15
+ th = I18n.t('liquor_help.description')
16
+ tr
17
+ td total
18
+ td = I18n.t('liquor_help.variables.cart.properties.total')
19
+ tr
20
+ td line_items
21
+ td = I18n.t('liquor_help.variables.cart.properties.line_items')
22
+ li
23
+ var line_item
24
+ p
25
+ span = I18n.t('liquor_help.variables.line_item.main_description')
26
+ p
27
+ code
28
+ | {% for line_item in: cart.line_items do: %}
29
+ p
30
+ table.table
31
+ tr
32
+ th #{ I18n.t('liquor_help.property') } LineItem
33
+ th = I18n.t('liquor_help.description')
34
+ tr
35
+ td title
36
+ td = I18n.t('liquor_help.variables.line_item.properties.title')
37
+ tr
38
+ td quantity
39
+ td = I18n.t('liquor_help.variables.line_item.properties.quantity')
40
+ tr
41
+ td price
42
+ td = I18n.t('liquor_help.variables.line_item.properties.price')
43
+ tr
44
+ td total
45
+ td = I18n.t('liquor_help.variables.line_item.properties.total')
@@ -0,0 +1,62 @@
1
+ .row
2
+ .col-lg-12
3
+ .widget
4
+ .widget-header
5
+ i.fa.fa-barcode
6
+ = Kms::Order.model_name.human(count: 1.1)
7
+ /a.btn.btn-sm.btn-primary.pull-right ui-sref="products.new"
8
+ /= I18n.t("add_product")
9
+ .widget-body.no-padding
10
+ accordion close-others="true"
11
+ accordion-group is-open="status.open" ng-repeat="order in orders"
12
+ accordion-heading
13
+ i.pull-left.glyphicon ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"
14
+ | &nbsp;
15
+ = Kms::Order.model_name.human
16
+ | &nbsp;{{ order.id }}&nbsp;
17
+ = I18n.t(:with_total)
18
+ | &nbsp;{{ order.total }}
19
+ .pull-right
20
+ | {{ order.created_at | date: 'dd MMM yyyy HH:mm' }}
21
+ .row
22
+ .col-md-12 style="padding-right:0"
23
+ a.btn.btn-danger.pull-right ng-click="destroy(order)"
24
+ = I18n.t(:delete)
25
+ .btn-group.pull-right
26
+ form.form-buttons.form-inline editable-form="" name="editLineItemForm" ng-show="editLineItemForm.$visible" onbeforesave="update($data, order)"
27
+ button.btn.btn-primary ng-disabled="editLineItemForm.$waiting" type="submit"
28
+ span class="glyphicon glyphicon-ok"
29
+ button.btn.btn-default ng-click="editLineItemForm.$cancel()" ng-disabled="editLineItemForm.$waiting" type="button"
30
+ span class="glyphicon glyphicon-remove"
31
+ div ng-show="!editLineItemForm.$visible"
32
+ a.btn.btn-info ng-click="editLineItemForm.$show()" ng-show="!editLineItemForm.$visible"
33
+ = I18n.t(:edit)
34
+ .table-responsive
35
+ table.table.table-bordered
36
+ tbody
37
+ tr ng-repeat="line_item in order.line_items"
38
+ td style="width: 50%"
39
+ | {{ line_item.title }}
40
+ td style="width: 10%"
41
+ | {{ line_item.price }}
42
+ td style="width: 10%"
43
+ a href='#' editable-number="line_item.quantity" e-name="quantity" onbeforesave="update($data, order, line_item)"
44
+ | {{ line_item.quantity }}
45
+ td style="width: 10%"
46
+ | {{ line_item.total }}
47
+ .row
48
+ .col-md-4
49
+ dl
50
+ dt = Kms::Customer.human_attribute_name(:name)
51
+ dd editable-text="order.customer.name" e-name="name" e-form="editLineItemForm"
52
+ | {{ order.customer.name }}
53
+ .col-md-4
54
+ dl
55
+ dt = Kms::Customer.human_attribute_name(:phone)
56
+ dd editable-text="order.customer.phone" e-name="phone" e-form="editLineItemForm"
57
+ | {{ order.customer.phone }}
58
+ .col-md-4
59
+ dl
60
+ dt = Kms::Customer.human_attribute_name(:email)
61
+ dd editable-email="order.customer.email" e-name="email" e-form="editLineItemForm"
62
+ | {{ order.customer.email }}
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,22 @@
1
+ module Kms
2
+ module CurrentCart
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :current_cart
7
+ end
8
+
9
+ protected
10
+
11
+ def current_cart
12
+ return @current_cart if @current_cart
13
+ if session[:cart_id]
14
+ @current_cart = Kms::Cart.find_by(id: session[:cart_id])
15
+ else
16
+ new_cart = Kms::Cart.create
17
+ session[:cart_id] = new_cart.id
18
+ @current_cart = new_cart
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module Kms
2
+ module Public
3
+ class CartController < ActionController::Base
4
+ include Kms::CurrentCart
5
+ protect_from_forgery with: :exception
6
+
7
+ def update
8
+ cart_populator = Kms::CartPopulator.new(current_cart, params)
9
+ if cart_populator.populate
10
+ render json: current_cart, root: false
11
+ else
12
+ render json: {errors: cart_populator.errors}.to_json, status: :unprocessable_entity
13
+ end
14
+ end
15
+
16
+ def destroy
17
+ current_cart.line_items.clear
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Kms
2
+ module Public
3
+ class CheckoutController < ActionController::Base
4
+ include Kms::CurrentCart
5
+ protect_from_forgery with: :exception
6
+
7
+ def create
8
+ cart_finalizer = Kms::CartFinalizer.new(current_cart, customer_params)
9
+ if order = cart_finalizer.finalize
10
+ session[:cart_id] = nil
11
+ render json: order, root: false
12
+ else
13
+ render json: {errors: order || {}}.to_json, status: :unprocessable_entity
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def customer_params
20
+ params.require(:customer).permit(:name, :email, :phone)
21
+ end
22
+ end
23
+ end
24
+ end