ecom 0.1.1 → 0.2.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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/app/controllers/ecom/cart_controller.rb +28 -38
  4. data/app/controllers/ecom/products_controller.rb +1 -1
  5. data/app/models/ecom/category.rb +2 -0
  6. data/app/models/ecom/line_item.rb +3 -6
  7. data/app/models/ecom/product.rb +4 -3
  8. data/app/models/ecom/purchase.rb +12 -3
  9. data/app/models/ecom/user.rb +1 -1
  10. data/app/uploaders/ecom/image_uploader.rb +2 -2
  11. data/app/views/ecom/cart/show.html.erb +8 -9
  12. data/app/views/ecom/products/_form.html.erb +4 -4
  13. data/app/views/ecom/products/index.html.erb +27 -9
  14. data/app/views/ecom/products/show.html.erb +7 -10
  15. data/config/initializers/devise.rb +2 -2
  16. data/config/routes.rb +6 -11
  17. data/lib/ecom/engine.rb +1 -1
  18. data/lib/ecom/version.rb +1 -1
  19. data/lib/generators/ecom/install_generator.rb +2 -0
  20. data/test/controllers/ecom/cart_controller_test.rb +13 -0
  21. data/test/controllers/ecom/products_controller_test.rb +52 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/config/manifest.js +5 -0
  24. data/test/dummy/app/assets/javascripts/application.js +13 -0
  25. data/test/dummy/app/assets/javascripts/cable.js +13 -0
  26. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  27. data/test/dummy/app/channels/application_cable/channel.rb +4 -0
  28. data/test/dummy/app/channels/application_cable/connection.rb +4 -0
  29. data/test/dummy/app/controllers/application_controller.rb +3 -0
  30. data/test/dummy/app/helpers/application_helper.rb +2 -0
  31. data/test/dummy/app/jobs/application_job.rb +2 -0
  32. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  33. data/test/dummy/app/models/application_record.rb +3 -0
  34. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  36. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  37. data/test/dummy/bin/bundle +3 -0
  38. data/test/dummy/bin/rails +4 -0
  39. data/test/dummy/bin/rake +4 -0
  40. data/test/dummy/bin/setup +34 -0
  41. data/test/dummy/bin/update +29 -0
  42. data/test/dummy/config.ru +5 -0
  43. data/test/dummy/config/application.rb +15 -0
  44. data/test/dummy/config/boot.rb +5 -0
  45. data/test/dummy/config/cable.yml +9 -0
  46. data/test/dummy/config/database.yml +25 -0
  47. data/test/dummy/config/environment.rb +5 -0
  48. data/test/dummy/config/environments/development.rb +54 -0
  49. data/test/dummy/config/environments/production.rb +86 -0
  50. data/test/dummy/config/environments/test.rb +42 -0
  51. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/dummy/config/initializers/inflections.rb +16 -0
  57. data/test/dummy/config/initializers/mime_types.rb +4 -0
  58. data/test/dummy/config/initializers/new_framework_defaults.rb +24 -0
  59. data/test/dummy/config/initializers/session_store.rb +3 -0
  60. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  61. data/test/dummy/config/locales/en.yml +23 -0
  62. data/test/dummy/config/mongoid.yml +143 -0
  63. data/test/dummy/config/puma.rb +47 -0
  64. data/test/dummy/config/routes.rb +3 -0
  65. data/test/dummy/config/secrets.yml +22 -0
  66. data/test/dummy/config/spring.rb +6 -0
  67. data/test/dummy/db/development.sqlite3 +0 -0
  68. data/test/dummy/log/development.log +8 -0
  69. data/test/dummy/public/404.html +67 -0
  70. data/test/dummy/public/422.html +67 -0
  71. data/test/dummy/public/500.html +66 -0
  72. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  73. data/test/dummy/public/apple-touch-icon.png +0 -0
  74. data/test/dummy/public/favicon.ico +0 -0
  75. data/test/ecom_test.rb +7 -0
  76. data/test/fixtures/ecom/categories.yml +7 -0
  77. data/test/fixtures/ecom/line_items.yml +9 -0
  78. data/test/fixtures/ecom/products.yml +13 -0
  79. data/test/fixtures/ecom/purchases.yml +11 -0
  80. data/test/fixtures/ecom/users.yml +11 -0
  81. data/test/integration/navigation_test.rb +8 -0
  82. data/test/models/ecom/category_test.rb +9 -0
  83. data/test/models/ecom/line_item_test.rb +9 -0
  84. data/test/models/ecom/product_test.rb +9 -0
  85. data/test/models/ecom/purchase_test.rb +9 -0
  86. data/test/models/ecom/user_test.rb +9 -0
  87. data/test/test_helper.rb +20 -0
  88. metadata +156 -19
  89. data/app/assets/javascripts/ecom/categories.js +0 -2
  90. data/app/assets/stylesheets/ecom/categories.css +0 -4
  91. data/app/controllers/ecom/categories_controller.rb +0 -62
  92. data/app/helpers/ecom/categories_helper.rb +0 -4
  93. data/app/views/ecom/categories/_form.html.erb +0 -22
  94. data/app/views/ecom/categories/edit.html.erb +0 -6
  95. data/app/views/ecom/categories/index.html.erb +0 -27
  96. data/app/views/ecom/categories/new.html.erb +0 -5
  97. data/app/views/ecom/categories/show.html.erb +0 -9
  98. data/config/initializers/line_items.rb +0 -1
  99. data/config/initializers/product.rb +0 -1
  100. data/config/initializers/purchase.rb +0 -1
  101. data/config/locales/en.yml +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0f8f75b165ff36e90775be93346597239fdd9c9
4
- data.tar.gz: 1890e78e5944a22db2a28d7235e8a7bda07a7060
3
+ metadata.gz: 8e773c6d0c4f07a4fd326b4b97b2ee2a8ab48ea9
4
+ data.tar.gz: 4cfeb568f4718a92662ea853121b45fba44b9819
5
5
  SHA512:
6
- metadata.gz: 3494b5e6fb2990fb2a22bde69c439103534440531b6007dfffbd9e8e2f6bad75ba27d8479515c44d0f4f1c3f9eae24baf5b3e5359914bcc9040fa08983363193
7
- data.tar.gz: 3cc55de3046e1a04c1ac9418cce9e7c6997542f05e12c815a49549e738803286c4f651913bbfe313a960b07901ee072fd185bd352bdaa857db12f60720b2bb50
6
+ metadata.gz: 79954042e9e99a688fc15c8dfe4f866a4bedf3e50df9449fc2696f18a22b3c7d7688da10c7c9c7a413ab3a7e878c77a0fd96a38b17d9987970a0ce7e5912907b
7
+ data.tar.gz: abafa335e15d90c46f98f99e0b702d0775e86a461420da5ff33e81de9f649bec438d42c0a251efc79066f2ff8cc078e92157bf061d48a907196cf44c048565cc
data/Rakefile CHANGED
@@ -14,6 +14,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
17
19
 
18
20
 
19
21
  load 'rails/tasks/statistics.rake'
@@ -2,46 +2,36 @@ require_dependency "ecom/application_controller"
2
2
 
3
3
  module Ecom
4
4
  class CartController < ApplicationController
5
- before_filter :authenticate_user!
6
- before_filter :get_cart_value
5
+ def add
6
+ @cart.save
7
+ session[:cart_id] = @cart.id
8
+ product = Product.find(params[:id])
9
+ item = LineItem.new
10
+ item.make_items(@cart.id, product.id, product.base_price)
11
+ @cart.recalculate_price!
12
+ flash[:notice] = "Product Added to Cart"
13
+ redirect_to cart_path
14
+ end
15
+ def remove
16
+ item = @cart.line_items.find(params[:id])
17
+ item.destroy
18
+ @cart.recalculate_price!
19
+ flash[:notice] = "Product Deleted from Cart"
20
+ redirect_to cart_path
21
+ end
7
22
 
8
- def add
9
- @cart.save
10
- session[:cart_id] = @cart.id
11
- product = Product.find(params[:id])
12
- item = LineItem.new
13
- item.make_items(@cart.id, product.id, product.base_price)
14
- @cart.recalculate_price!
15
- flash[:notice] = "Product Added to Cart"
16
- redirect_to cart_path
17
- end
18
-
19
- def remove
20
- item = @cart.line_items.find(params[:id])
21
- item.destroy
22
- @cart.recalculate_price!
23
- flash[:notice] = "Product Deleted from Cart"
24
- redirect_to cart_path
25
- end
26
-
27
- def checkout
28
- @cart.checkout!
29
- session.delete(:cart_id)
30
- flash[:notice] = "Thank your for the Order! We will e-mail you with the shipping info."
31
- redirect_to root_path
32
- end
33
-
34
- protected
35
-
36
- def get_cart_value
37
- if session[:cart_id].nil?
38
- @cart = Purchase.create
39
- session[:cart_id] = @cart.id
40
- @cart
41
- else
42
- Purchase.find(session[:cart_id])
23
+ protected
24
+
25
+ def get_cart_value
26
+ if session[:cart_id].nil?
27
+ @cart = Purchase.create
28
+ session[:cart_id] = @cart.id
29
+ @cart
30
+ else
31
+ @cart = Purchase.find(session[:cart_id])
32
+ end
43
33
  end
44
- end
45
34
 
35
+
46
36
  end
47
37
  end
@@ -56,7 +56,7 @@ module Ecom
56
56
 
57
57
  # Only allow a trusted parameter "white list" through.
58
58
  def product_params
59
- params.require(:product).permit(:name, :description, :size, :base_price)
59
+ params.require(:product).permit(:name, :description, :base_price, :size)
60
60
  end
61
61
  end
62
62
  end
@@ -2,5 +2,7 @@ module Ecom
2
2
  class Category
3
3
  include Mongoid::Document
4
4
  field :title, type: String
5
+
6
+ has_many :products
5
7
  end
6
8
  end
@@ -10,12 +10,9 @@ module Ecom
10
10
  belongs_to :purchase
11
11
  belongs_to :product
12
12
 
13
- def make_items(purchase_id, product_id, price)
14
- item = LineItem.new
15
- item.purchase_id = purchase_id
16
- item.product_id = product_id
17
- item.price = price
18
- item.save
13
+ def self.make_items(purchase_id, product_id, price)
14
+
15
+ LineItem.create(purchase_id: purchase_id, product_id: product_id, price: price)
19
16
  end
20
17
  end
21
18
  end
@@ -6,10 +6,11 @@ module Ecom
6
6
  field :name, type: String
7
7
  field :description, type: String
8
8
  field :base_price, type: Float
9
- field :sku, type: String
9
+ field :size, type: String
10
+ slug :name, history: true
10
11
 
11
- slug :name, history: true
12
- belongs_to :category
12
+ belongs_to: category
13
+ has_many :line_items
13
14
  mount_uploader :image, ImageUploader
14
15
  end
15
16
  end
@@ -1,13 +1,22 @@
1
1
  module Ecom
2
2
  class Purchase
3
3
  include Mongoid::Document
4
+ include Mongoid::MultiParameterAttributes
4
5
  include Mongoid::Timestamps
5
-
6
+
6
7
  field :user_id, type: String
7
- field :checked_out_at, type: Time
8
+ field :check_out_at, type: Time
8
9
  field :total_price, type: Float
10
+
11
+ has_many :line_items, :dependent => destroy
12
+ belongs_to :user
9
13
 
10
- has_many :line_items, :dependent => :destroy
14
+ def recalculate_price!
15
+
16
+ self.total_price = line_items.inject(0,0){|sum, line_item| sum += line_item.price}
17
+
18
+ save!
19
+ end
11
20
 
12
21
  end
13
22
  end
@@ -35,6 +35,6 @@ module Ecom
35
35
  # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
36
36
  # field :unlock_token, type: String # Only if unlock strategy is :email or :both
37
37
  # field :locked_at, type: Time
38
- has_many :purchases, :dependent => :destroy
38
+ has_many :purchases, :dependent => :destroy
39
39
  end
40
40
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
-
3
- class ImageUploader < CarrierWave::Uploader::Base
2
+ module Ecom
3
+ class ImageUploader < CarrierWave::Uploader::Base
4
4
 
5
5
  # Include RMagick or MiniMagick support:
6
6
  # include CarrierWave::RMagick
@@ -1,19 +1,19 @@
1
1
  <h1>Shopping Cart</h1>
2
-
3
2
  <% unless @cart.line_items.any? %>
4
- <p>You don't have any items in your cart. <%= link_to "Go Add Some", products_path %>
3
+ <p>You don't have any items in your cart. <%= link_to "Go Add
4
+ Some", products_path %>
5
5
  <% end %>
6
-
7
6
  <table width="100%">
8
7
  <tr>
9
- <th>Product</th>
8
+ <th>Print</th>
10
9
  <th>Price</th>
11
10
  </tr>
12
11
  <% for line_item in @cart.line_items %>
13
12
  <tr>
14
13
  <td><%= line_item.product.name %></td>
15
14
  <td><%= number_to_currency line_item.price %></td>
16
- <td><%= link_to "Remove", remove_from_cart_path(line_item), :method => :post %></td>
15
+ <td><%= link_to "Remove", remove_from_cart_path(line_item),
16
+ :method => :post %></td>
17
17
  </tr>
18
18
  <% end %>
19
19
  <tr>
@@ -21,10 +21,9 @@
21
21
  <td><%= number_to_currency @cart.total_price %></td>
22
22
  </tr>
23
23
  </table>
24
-
25
24
  <hr />
26
25
  <%= form_tag checkout_path, :style => "text-align: right" do |f| %>
27
- <%= link_to "Continue Shopping", root_path %>
28
- or
29
- <%= submit_tag "Checkout"%>
26
+ <%= link_to "Continue Shopping", root_path %>
27
+ or
28
+ <%= submit_tag "Checkout" %>
30
29
  <% end %>
@@ -22,13 +22,13 @@
22
22
  </div>
23
23
 
24
24
  <div class="field">
25
- <%= f.label :size %>
26
- <%= f.text_field :size %>
25
+ <%= f.label :base_price %>
26
+ <%= f.text_field :base_price %>
27
27
  </div>
28
28
 
29
29
  <div class="field">
30
- <%= f.label :base_price %>
31
- <%= f.text_field :base_price %>
30
+ <%= f.label :size %>
31
+ <%= f.text_field :size %>
32
32
  </div>
33
33
 
34
34
  <div class="actions">
@@ -1,10 +1,28 @@
1
1
  <div class="row">
2
- <% @products.each do |product| %>
3
- <div class="col-lg-4">
4
- <h3><%=link_to product.name, product %></h3>
5
- <p><%= image_tag product.image.url %></p>
6
- <p><b>Price:</b> <%= product.base_price %></p>
7
- <p><%= product.description %></p>
8
- <p><%=link_to( image_tag("ecom/add-to-cart-button.png"), add_to_cart_path(product.id)) %></p>
9
- </div>
10
- <%end%>
2
+ <% @products.each do |product| %>
3
+ <div class="col-lg-4">
4
+ <h3><%=link_to product.name, product %></h3>
5
+ <p><%= image_tag product.image.url %></p>
6
+ <p><b>Price:</b> <%= product.base_price %></p>
7
+ <p><%= product.description %></p>
8
+ <p><%=link_to( image_tag("ecom/add-to-cart-button.png"),add_to_cart_path(product.id)) %></p>
9
+ </div>
10
+ <%end%>
11
+ app/views/ecom/products/show.html.erb
12
+ <p id="notice"><%= notice %></p>
13
+ <p>
14
+ <h2><%= @product.name %></h2>
15
+ </p>
16
+ <p>
17
+ <strong>Sku:&nbsp;</strong>
18
+ <%= @product.sku %>
19
+ </p>
20
+ <p>
21
+ <strong>Price:&nbsp;</strong>
22
+ <%= @product.base_price %>
23
+ </p>
24
+ <br/>
25
+ <p>
26
+ <%=image_tag @product.image.url %>
27
+ </p>
28
+ <p><%=link_to( image_tag("ecom/add-to-cart-button.png"), add_to_cart_path(@product.id)) %></p>
@@ -1,27 +1,24 @@
1
1
  <p id="notice"><%= notice %></p>
2
2
 
3
3
  <p>
4
- <h2><%= @product.name %></h2>
4
+ <strong>Name:</strong>
5
+ <%= @product.name %>
5
6
  </p>
6
7
 
7
8
  <p>
8
- <strong>Sku:&nbsp;</strong>
9
- <%= @product.sku %>
9
+ <strong>Description:</strong>
10
+ <%= @product.description %>
10
11
  </p>
11
12
 
12
13
  <p>
13
- <strong>Price:&nbsp;</strong>
14
+ <strong>Base price:</strong>
14
15
  <%= @product.base_price %>
15
16
  </p>
16
17
 
17
- <br/>
18
18
  <p>
19
- <%=image_tag @product.image.url %>
19
+ <strong>Size:</strong>
20
+ <%= @product.size %>
20
21
  </p>
21
22
 
22
- <p><%=link_to( image_tag("ecom/add-to-cart-button.png"), add_to_cart_path(@product.id)) %></p>
23
-
24
- <!--<%if user_signed_in? %>
25
23
  <%= link_to 'Edit', edit_product_path(@product) %> |
26
24
  <%= link_to 'Back', products_path %>
27
- <% end %>-->
@@ -6,7 +6,7 @@ Devise.setup do |config|
6
6
  # confirmation, reset password and unlock tokens in the database.
7
7
  # Devise will use the `secret_key_base` as its `secret_key`
8
8
  # by default. You can change it below and use your own secret key.
9
- config.secret_key = '445c87bc60f1ea7e7b8979fa2502c6049787d48525205befddca18353268faa5c4776a716b2e377abea9b9a348235639d282c6e195abb70204583436ff3c77dd'
9
+ config.secret_key = '229c9ff859780e6bfde773a4b1ff4c592f9ee756072e22881381c3a2a6c7cad37abe31d3ceb451014708d241d15ef7ed0c6476d04cafd9b82b1b4c1c8df481c7'
10
10
 
11
11
  # ==> Mailer Configuration
12
12
  # Configure the e-mail address which will be shown in Devise::Mailer,
@@ -108,7 +108,7 @@ Devise.setup do |config|
108
108
  config.stretches = Rails.env.test? ? 1 : 11
109
109
 
110
110
  # Set up a pepper to generate the hashed password.
111
- # config.pepper = 'd7cfced7eef5bf9a8eb0b958102d062117e759e24fe5bf60bdd768c07ff1acba9135a956bccc56a19da538f495e5d335ca50b401b4f666cda1e38be7462577f0'
111
+ # config.pepper = 'a3333a9e83fb4fe1bca1880ad3e7e50ae1fe968c6f6d552406553fba60da2a1624502abf145eaf97e128e6e70e13d39fea36b81239fb2255514b916055a6bf2b'
112
112
 
113
113
  # Send a notification email when the user's password is changed
114
114
  # config.send_password_change_notification = false
@@ -1,15 +1,10 @@
1
1
  Ecom::Engine.routes.draw do
2
- resources :categories
2
+ get 'cart' => "cart#show"
3
+ post "cart/add/:id" => "cart#add", :as => :add_to_cart
4
+ post "cart/remove/:id" => "cart#remove", :as => :remove_from_cart
3
5
 
4
- devise_for :users, {
5
- :class_name => "Ecom::User",
6
- module: :devise
6
+ devise_for :users,{
7
+ :class_name: "Ecom::User"
8
+ ``module: :devise
7
9
  }
8
- resources :products
9
- root to: "products#index"
10
-
11
- get "cart" => "cart#show"
12
- get "cart/add/:id" => "cart#add", :as => :add_to_cart
13
- post "cart/remove/:id" => "cart#remove", :as => :remove_from_cart
14
- post "cart/checkout" => "cart#checkout", :as => :checkout
15
10
  end
@@ -1,4 +1,4 @@
1
- require 'mongoid'
1
+ require "mongoid"
2
2
  module Ecom
3
3
  class Engine < ::Rails::Engine
4
4
  isolate_namespace Ecom
@@ -1,3 +1,3 @@
1
1
  module Ecom
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -7,4 +7,6 @@ class Ecom::InstallGenerator < ::Rails::Generators::Base
7
7
  route 'mount Ecom::Engine => "/store"'
8
8
  copy_file "../../../../config/locales/en.yml", "config/locales/ecom.en.yml"
9
9
  end
10
+
10
11
  end
12
+
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module Ecom
4
+ class CartControllerTest < ActionDispatch::IntegrationTest
5
+ include Engine.routes.url_helpers
6
+
7
+ test "should get show" do
8
+ get cart_show_url
9
+ assert_response :success
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ module Ecom
4
+ class ProductsControllerTest < ActionDispatch::IntegrationTest
5
+ include Engine.routes.url_helpers
6
+
7
+ setup do
8
+ @product = ecom_products(:one)
9
+ end
10
+
11
+ test "should get index" do
12
+ get products_url
13
+ assert_response :success
14
+ end
15
+
16
+ test "should get new" do
17
+ get new_product_url
18
+ assert_response :success
19
+ end
20
+
21
+ test "should create product" do
22
+ assert_difference('Product.count') do
23
+ post products_url, params: { product: { base_price: @product.base_price, description: @product.description, name: @product.name, size: @product.size } }
24
+ end
25
+
26
+ assert_redirected_to product_url(Product.last)
27
+ end
28
+
29
+ test "should show product" do
30
+ get product_url(@product)
31
+ assert_response :success
32
+ end
33
+
34
+ test "should get edit" do
35
+ get edit_product_url(@product)
36
+ assert_response :success
37
+ end
38
+
39
+ test "should update product" do
40
+ patch product_url(@product), params: { product: { base_price: @product.base_price, description: @product.description, name: @product.name, size: @product.size } }
41
+ assert_redirected_to product_url(@product)
42
+ end
43
+
44
+ test "should destroy product" do
45
+ assert_difference('Product.count', -1) do
46
+ delete product_url(@product)
47
+ end
48
+
49
+ assert_redirected_to products_url
50
+ end
51
+ end
52
+ end