ecom 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -0
- data/app/controllers/ecom/cart_controller.rb +28 -38
- data/app/controllers/ecom/products_controller.rb +1 -1
- data/app/models/ecom/category.rb +2 -0
- data/app/models/ecom/line_item.rb +3 -6
- data/app/models/ecom/product.rb +4 -3
- data/app/models/ecom/purchase.rb +12 -3
- data/app/models/ecom/user.rb +1 -1
- data/app/uploaders/ecom/image_uploader.rb +2 -2
- data/app/views/ecom/cart/show.html.erb +8 -9
- data/app/views/ecom/products/_form.html.erb +4 -4
- data/app/views/ecom/products/index.html.erb +27 -9
- data/app/views/ecom/products/show.html.erb +7 -10
- data/config/initializers/devise.rb +2 -2
- data/config/routes.rb +6 -11
- data/lib/ecom/engine.rb +1 -1
- data/lib/ecom/version.rb +1 -1
- data/lib/generators/ecom/install_generator.rb +2 -0
- data/test/controllers/ecom/cart_controller_test.rb +13 -0
- data/test/controllers/ecom/products_controller_test.rb +52 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/config/manifest.js +5 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/javascripts/cable.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/channels/application_cable/channel.rb +4 -0
- data/test/dummy/app/channels/application_cable/connection.rb +4 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/jobs/application_job.rb +2 -0
- data/test/dummy/app/mailers/application_mailer.rb +4 -0
- data/test/dummy/app/models/application_record.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +34 -0
- data/test/dummy/bin/update +29 -0
- data/test/dummy/config.ru +5 -0
- data/test/dummy/config/application.rb +15 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/cable.yml +9 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +54 -0
- data/test/dummy/config/environments/production.rb +86 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/new_framework_defaults.rb +24 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/mongoid.yml +143 -0
- data/test/dummy/config/puma.rb +47 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config/spring.rb +6 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/log/development.log +8 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/test/dummy/public/apple-touch-icon.png +0 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/ecom_test.rb +7 -0
- data/test/fixtures/ecom/categories.yml +7 -0
- data/test/fixtures/ecom/line_items.yml +9 -0
- data/test/fixtures/ecom/products.yml +13 -0
- data/test/fixtures/ecom/purchases.yml +11 -0
- data/test/fixtures/ecom/users.yml +11 -0
- data/test/integration/navigation_test.rb +8 -0
- data/test/models/ecom/category_test.rb +9 -0
- data/test/models/ecom/line_item_test.rb +9 -0
- data/test/models/ecom/product_test.rb +9 -0
- data/test/models/ecom/purchase_test.rb +9 -0
- data/test/models/ecom/user_test.rb +9 -0
- data/test/test_helper.rb +20 -0
- metadata +156 -19
- data/app/assets/javascripts/ecom/categories.js +0 -2
- data/app/assets/stylesheets/ecom/categories.css +0 -4
- data/app/controllers/ecom/categories_controller.rb +0 -62
- data/app/helpers/ecom/categories_helper.rb +0 -4
- data/app/views/ecom/categories/_form.html.erb +0 -22
- data/app/views/ecom/categories/edit.html.erb +0 -6
- data/app/views/ecom/categories/index.html.erb +0 -27
- data/app/views/ecom/categories/new.html.erb +0 -5
- data/app/views/ecom/categories/show.html.erb +0 -9
- data/config/initializers/line_items.rb +0 -1
- data/config/initializers/product.rb +0 -1
- data/config/initializers/purchase.rb +0 -1
- data/config/locales/en.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e773c6d0c4f07a4fd326b4b97b2ee2a8ab48ea9
|
4
|
+
data.tar.gz: 4cfeb568f4718a92662ea853121b45fba44b9819
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79954042e9e99a688fc15c8dfe4f866a4bedf3e50df9449fc2696f18a22b3c7d7688da10c7c9c7a413ab3a7e878c77a0fd96a38b17d9987970a0ce7e5912907b
|
7
|
+
data.tar.gz: abafa335e15d90c46f98f99e0b702d0775e86a461420da5ff33e81de9f649bec438d42c0a251efc79066f2ff8cc078e92157bf061d48a907196cf44c048565cc
|
data/Rakefile
CHANGED
@@ -2,46 +2,36 @@ require_dependency "ecom/application_controller"
|
|
2
2
|
|
3
3
|
module Ecom
|
4
4
|
class CartController < ApplicationController
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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, :
|
59
|
+
params.require(:product).permit(:name, :description, :base_price, :size)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
data/app/models/ecom/category.rb
CHANGED
@@ -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
|
-
|
15
|
-
|
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
|
data/app/models/ecom/product.rb
CHANGED
@@ -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 :
|
9
|
+
field :size, type: String
|
10
|
+
slug :name, history: true
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
belongs_to: category
|
13
|
+
has_many :line_items
|
13
14
|
mount_uploader :image, ImageUploader
|
14
15
|
end
|
15
16
|
end
|
data/app/models/ecom/purchase.rb
CHANGED
@@ -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 :
|
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
|
-
|
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
|
data/app/models/ecom/user.rb
CHANGED
@@ -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
|
-
|
38
|
+
has_many :purchases, :dependent => :destroy
|
39
39
|
end
|
40
40
|
end
|
@@ -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
|
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>
|
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),
|
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
|
-
|
28
|
-
|
29
|
-
|
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 :
|
26
|
-
<%= f.text_field :
|
25
|
+
<%= f.label :base_price %>
|
26
|
+
<%= f.text_field :base_price %>
|
27
27
|
</div>
|
28
28
|
|
29
29
|
<div class="field">
|
30
|
-
<%= f.label :
|
31
|
-
<%= f.text_field :
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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: </strong>
|
18
|
+
<%= @product.sku %>
|
19
|
+
</p>
|
20
|
+
<p>
|
21
|
+
<strong>Price: </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
|
-
<
|
4
|
+
<strong>Name:</strong>
|
5
|
+
<%= @product.name %>
|
5
6
|
</p>
|
6
7
|
|
7
8
|
<p>
|
8
|
-
<strong>
|
9
|
-
<%= @product.
|
9
|
+
<strong>Description:</strong>
|
10
|
+
<%= @product.description %>
|
10
11
|
</p>
|
11
12
|
|
12
13
|
<p>
|
13
|
-
<strong>
|
14
|
+
<strong>Base price:</strong>
|
14
15
|
<%= @product.base_price %>
|
15
16
|
</p>
|
16
17
|
|
17
|
-
<br/>
|
18
18
|
<p>
|
19
|
-
|
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 = '
|
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 = '
|
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
|
data/config/routes.rb
CHANGED
@@ -1,15 +1,10 @@
|
|
1
1
|
Ecom::Engine.routes.draw do
|
2
|
-
|
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
|
-
|
6
|
-
|
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
|
data/lib/ecom/engine.rb
CHANGED
data/lib/ecom/version.rb
CHANGED
@@ -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
|