Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
dhp-venshop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Hoang Phuc Do
dhp-venshop
Commits
4f37530b
Commit
4f37530b
authored
Jun 15, 2017
by
Hoang Phuc Do
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow user input product quantity
parent
f0d57a97
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
178 additions
and
122 deletions
+178
-122
app/assets/stylesheets/main_style.scss
+54
-0
app/assets/stylesheets/skin_14.scss
+3
-49
app/controllers/line_items_controller.rb
+24
-6
app/controllers/orders_controller.rb
+1
-1
app/controllers/products_controller.rb
+2
-2
app/helpers/carts_helper.rb
+4
-4
app/helpers/line_items_helper.rb
+7
-0
app/views/products/_form.html.erb
+9
-2
app/views/products/_product.html.erb
+35
-14
app/views/products/_product_grid.html.erb
+5
-3
app/views/products/_product_list.html.erb
+23
-0
app/views/products/show.html.erb
+2
-34
app/views/static_pages/index.html.erb
+2
-6
db/migrate/20170615035827_add_quantity_to_products.rb
+5
-0
db/schema.rb
+2
-1
No files found.
app/assets/stylesheets/main_style.scss
View file @
4f37530b
...
...
@@ -947,6 +947,60 @@ html .featured-box-primary .box-content {
/*
* Product details
*/
@media
(
max-width
:
767px
)
{
.product-img-box
{
margin-bottom
:
25px
;
}
}
.product-img-box
img
{
display
:
block
;
width
:
100%
;
height
:
auto
;
}
.product-img-box
.product-img-wrapper
{
padding
:
3px
;
border
:
1px
solid
#ddd
;
border-radius
:
0
;
}
.product-img-box-wrapper
{
position
:
relative
;
margin-bottom
:
10px
;
}
.product-details-box
.product-name
{
margin
:
15px
0
;
font-size
:
28px
;
font-weight
:
600
;
line-height
:
1
;
color
:
#555
;
}
.product-details-box
.product-short-desc
{
padding
:
0
0
10px
;
border-bottom
:
1px
solid
#ebebeb
;
}
.product-details-box
.product-short-desc
p
{
font-size
:
14px
;
line-height
:
1
.65
;
margin
:
0
0
20px
;
}
.product-details-box
.product-detail-info
{
padding-bottom
:
20px
;
margin-top
:
20px
;
border-bottom
:
1px
solid
#ebebeb
;
}
.product-details-box
.product-detail-info
.product-price-box
{
margin
:
0
0
20px
;
}
.product-details-box
.product-detail-info
.product-price-box
.product-price
{
font-size
:
33px
;
line-height
:
1
;
color
:
#000
;
}
.product-details-box
.product-detail-info
.availability
{
margin
:
0
0
10px
;
font-size
:
14px
;
color
:
#777
;
font-weight
:
400
;
}
.product-details-box
.product-detail-qty
{
display
:
inline-block
;
vertical-align
:
middle
;
...
...
app/assets/stylesheets/skin_14.scss
View file @
4f37530b
...
...
@@ -69,61 +69,15 @@ html .btn-primary:active:focus {
.btn
{
border-radius
:
0
;
}
.font-weight-semibold
{
font-weight
:
600
!
important
;
}
.featured-box
.box-content
{
border-radius
:
0
;
}
.product-essential
{
margin-bottom
:
50px
;
}
@media
(
max-width
:
767px
)
{
.product-img-box
{
margin-bottom
:
25px
;
}
}
.product-img-box
img
{
display
:
block
;
width
:
100%
;
height
:
auto
;
}
.product-img-box
.product-img-wrapper
{
padding
:
3px
;
border
:
1px
solid
#ddd
;
border-radius
:
0
;
}
.product-img-box-wrapper
{
position
:
relative
;
margin-bottom
:
10px
;
}
.product-details-box
.product-name
{
margin
:
15px
0
;
font-size
:
28px
;
font-weight
:
600
;
line-height
:
1
;
color
:
#555
;
}
.product-details-box
.product-short-desc
{
padding
:
0
0
10px
;
border-bottom
:
1px
solid
#ebebeb
;
}
.product-details-box
.product-short-desc
p
{
font-size
:
14px
;
line-height
:
1
.65
;
margin
:
0
0
20px
;
}
.product-details-box
.product-detail-info
{
padding-bottom
:
20px
;
margin-top
:
20px
;
border-bottom
:
1px
solid
#ebebeb
;
}
.product-details-box
.product-detail-info
.product-price-box
{
margin
:
0
0
20px
;
}
.product-details-box
.product-detail-info
.product-price-box
.product-price
{
font-size
:
33px
;
line-height
:
1
;
color
:
#000
;
}
#header
.header-body
{
border-top
:
none
;
background-color
:
transparent
;
...
...
app/controllers/line_items_controller.rb
View file @
4f37530b
class
LineItemsController
<
ApplicationController
include
CartsHelper
include
LineItemsHelper
before_action
:set_cart
,
only:
[
:create
,
:update
,
:destroy
]
# We do
n
not render line item details page
# We do not render line item details page
def
show
redirect_to
product_url
(
params
[
:id
])
end
...
...
@@ -11,20 +12,32 @@ class LineItemsController < ApplicationController
# POST /line_items
def
create
product
=
Product
.
find
(
params
[
:product_id
])
add_to_cart
(
product
.
id
.
to_s
,
params
[
:product_quantity
].
to_i
)
product_is_in_stock
=
product_is_in_stock?
(
product
,
get_line_item_quantity
(
product
.
id
,
params
[
:product_quantity
]))
add_to_cart
(
product
.
id
,
params
[
:product_quantity
])
if
product_is_in_stock
respond_to
do
|
format
|
format
.
html
{
redirect_to
cart_index_url
,
notice:
"
#{
product
.
title
}
was sucessfully added to your cart"
}
format
.
js
if
product_is_in_stock
format
.
html
{
redirect_to
cart_index_url
,
notice:
"
#{
product
.
title
}
was sucessfully added to your cart"
}
format
.
js
else
format
.
html
{
redirect_to
cart_index_url
,
notice:
"
#{
product
.
title
}
is out of stock"
}
end
end
end
# PATCH/PUT /line_items/1
def
update
update_cart_item
(
*
update_params
.
to_h
.
values
)
update_arg
=
update_params
.
to_h
.
values
product
=
Product
.
find
(
update_arg
.
first
)
unless
product_is_in_stock?
(
product
,
update_arg
.
last
)
return
respond_to
do
|
format
|
format
.
html
{
redirect_to
cart_index_url
,
notice:
"
#{
product
.
title
}
is out of stock"
}
end
end
update_cart_item
(
*
update_arg
)
respond_to
do
|
format
|
format
.
html
{
redirect_to
cart_index_url
}
format
.
html
{
redirect_to
cart_index_url
,
notice:
"
#{
product
.
title
}
was sucessfully updated"
}
end
end
...
...
@@ -44,4 +57,8 @@ class LineItemsController < ApplicationController
def
update_params
params
.
permit
(
:id
,
:qty_input
)
end
def
product_is_in_stock?
(
product
,
quantity
)
product
.
quantity
>=
quantity
.
to_i
end
end
\ No newline at end of file
app/controllers/orders_controller.rb
View file @
4f37530b
...
...
@@ -14,7 +14,7 @@ class OrdersController < ApplicationController
# POST /orders
def
create
@order
=
current_user
.
orders
.
create
@order
=
current_user
.
orders
.
new
create_line_items_for_order
(
@order
)
respond_to
do
|
format
|
...
...
app/controllers/products_controller.rb
View file @
4f37530b
...
...
@@ -47,8 +47,8 @@ class ProductsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def
product_params
params
.
require
(
:product
).
permit
(
:title
,
:sku
,
:price
,
:
description
,
:category_id
,
:image_url
)
params
.
require
(
:product
).
permit
(
:title
,
:sku
,
:price
,
:
quantity
,
:
description
,
:
category_id
,
:image_url
)
end
# Is current user own current editing product?
...
...
app/helpers/carts_helper.rb
View file @
4f37530b
...
...
@@ -5,11 +5,11 @@ module CartsHelper
end
def
add_to_cart
(
product_id
,
quantity
)
return
if
quantity
.
zero?
if
@cart
.
key?
(
product_id
)
@cart
[
product_id
][
:quantity
.
to_s
]
+=
quantity
return
if
quantity
.
to_i
.
zero?
if
@cart
.
key?
(
product_id
.
to_s
)
@cart
[
product_id
.
to_s
][
:quantity
.
to_s
]
+=
quantity
.
to_i
else
@cart
[
product_id
]
=
{
quantity:
quantity
}
@cart
[
product_id
.
to_s
]
=
{
quantity:
quantity
.
to_i
}
end
end
...
...
app/helpers/line_items_helper.rb
View file @
4f37530b
...
...
@@ -6,4 +6,10 @@ module LineItemsHelper
quantity:
product_attrs
[
'quantity'
].
to_i
}
end
end
def
get_line_item_quantity
(
product_id
,
new_quantity
=
0
)
return
new_quantity
.
to_i
if
@cart
.
empty?
||
!
@cart
.
key?
(
product_id
.
to_s
)
old_quantity
=
@cart
[
product_id
.
to_s
][
:quantity
]
||
@cart
[
product_id
.
to_s
][
:quantity
.
to_s
]
new_quantity
.
to_i
+
old_quantity
end
end
\ No newline at end of file
app/views/products/_form.html.erb
View file @
4f37530b
...
...
@@ -12,14 +12,21 @@
<div
class=
"col-md-6"
>
<div
class=
"form-group"
>
<%=
f
.
label
:sku
%>
<%=
f
.
text_field
:sku
,
class:
"form-control"
,
step: :any
%>
<%=
f
.
text_field
:sku
,
class:
"form-control"
%>
</div>
</div>
<div
class=
"col-md-6"
>
<div
class=
"form-group"
>
<%=
f
.
label
:price
%>
<%=
f
.
number_field
:price
,
class:
"form-control"
%>
<%=
f
.
number_field
:price
,
step: :any
,
class:
"form-control"
%>
</div>
</div>
<div
class=
"col-md-12"
>
<div
class=
"form-group"
>
<%=
f
.
label
:quantity
%>
<%=
f
.
number_field
:quantity
,
class:
"form-control"
%>
</div>
</div>
...
...
app/views/products/_product.html.erb
View file @
4f37530b
<li
class=
"product-
<%=
product
.
id
%>
"
>
<div
class=
"product product-list"
>
<figure
class=
"product-image-area"
>
<%=
link_to
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
)),
product_path
(
product
)
%>
</figure>
<div
class=
"product-details-area"
>
<h2
class=
"product-name"
>
<%=
link_to
product
.
title
,
product_url
(
product
)
%>
</h2>
<div
class=
"product-short-desc"
>
<%=
product
.
description
%>
<div
class=
"product-view"
>
<div
class=
"product-essential"
>
<div
class=
"row"
>
<div
class=
"product-img-box col-sm-5"
>
<div
class=
"product-img-box-wrapper"
>
<div
class=
"product-img-wrapper"
>
<%=
image_tag
(
get_product_thumbnail
(
product
,
715
,
952
))
%>
</div>
</div>
</div>
<div
class=
"product-price-box"
>
<span
class=
"product-price"
>
<%=
number_to_currency
(
product
.
price
)
%>
</span>
<div
class=
"product-details-box col-sm-7"
>
<h1
class=
"product-name"
>
<%=
product
.
title
%>
</h1>
<div
class=
"product-short-desc"
>
<%=
product
.
description
%>
</div>
<div
class=
"product-detail-info"
>
<div
class=
"product-price-box"
>
<span
class=
"product-price"
>
<%=
number_to_currency
(
product
.
price
)
%>
</span>
</div>
<p
class=
"availability"
>
<span
class=
"font-weight-semibold"
>
Availability:
</span>
<%=
product
.
quantity
%>
</p>
</div>
<div
class=
"product-actions"
>
<%=
form_tag
line_items_path
(
product_id:
product
),
remote:
true
do
%>
<div
class=
"product-detail-qty"
>
<%=
number_field_tag
:product_quantity
,
1
,
min:
0
,
class:
'vertical-spinner'
%>
</div>
<%=
submit_tag
'Add to cart'
,
class:
'addtocart'
%>
<%
end
%>
</div>
</div>
</div>
</div>
</
li
>
</
div
>
\ No newline at end of file
app/views/products/_
recommende
d.html.erb
→
app/views/products/_
product_gri
d.html.erb
View file @
4f37530b
<%
@recommended_products
.
each
do
|
product
|
%>
<div
class=
"products-grid columns
<%=
columns
%>
"
>
<%
products
.
each
do
|
product
|
%>
<li
class=
"product-
<%=
product
.
id
%>
"
>
<div
class=
"product"
>
<figure
class=
"product-image-area"
>
...
...
@@ -14,4 +15,5 @@
</div>
</div>
</li>
<%
end
%>
\ No newline at end of file
<%
end
%>
</div>
\ No newline at end of file
app/views/products/_product_list.html.erb
0 → 100644
View file @
4f37530b
<ul
class=
"products-list"
>
<%
products
.
each
do
|
product
|
%>
<li
class=
"product-
<%=
product
.
id
%>
"
>
<div
class=
"product product-list"
>
<figure
class=
"product-image-area"
>
<%=
link_to
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
)),
product_path
(
product
)
%>
</figure>
<div
class=
"product-details-area"
>
<h2
class=
"product-name"
>
<%=
link_to
product
.
title
,
product_url
(
product
)
%>
</h2>
<div
class=
"product-short-desc"
>
<%=
product
.
description
%>
</div>
<div
class=
"product-price-box"
>
<span
class=
"product-price"
>
<%=
number_to_currency
(
product
.
price
)
%>
</span>
</div>
</div>
</div>
</li>
<%
end
%>
</ul>
\ No newline at end of file
app/views/products/show.html.erb
View file @
4f37530b
<div
class=
"container"
>
<div
class=
"product-view"
>
<div
class=
"product-essential"
>
<div
class=
"row"
>
<div
class=
"product-img-box col-sm-5"
>
<div
class=
"product-img-box-wrapper"
>
<div
class=
"product-img-wrapper"
>
<%=
image_tag
(
get_product_thumbnail
(
@product
,
715
,
952
))
%>
</div>
</div>
</div>
<div
class=
"product-details-box col-sm-7"
>
<h1
class=
"product-name"
>
<%=
@product
.
title
%>
</h1>
<div
class=
"product-short-desc"
>
<%=
@product
.
description
%>
</div>
<div
class=
"product-detail-info"
>
<div
class=
"product-price-box"
>
<span
class=
"product-price"
>
<%=
number_to_currency
(
@product
.
price
)
%>
</span>
</div>
</div>
<div
class=
"product-actions"
>
<%=
form_tag
line_items_path
(
product_id:
@product
),
remote:
true
do
%>
<div
class=
"product-detail-qty"
>
<%=
number_field_tag
:product_quantity
,
1
,
min:
0
,
class:
'vertical-spinner'
%>
</div>
<%=
submit_tag
'Add to cart'
,
class:
'addtocart'
%>
<%
end
%>
</div>
</div>
</div>
</div>
</div>
<%=
render
@product
%>
</div>
\ No newline at end of file
app/views/static_pages/index.html.erb
View file @
4f37530b
...
...
@@ -5,16 +5,12 @@
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<span>
Recommended Items
</span>
</h2>
<div
class=
"products-grid columns3"
>
<%=
render
'products/recommended'
%>
</div>
<%=
render
partial:
'products/product_grid'
,
locals:
{
products:
@recommended_products
,
columns:
3
}
%>
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<span>
Newest Items
</span>
</h2>
<ul
class=
"products-list"
>
<%=
render
@latest_products
%>
</ul>
<%=
render
partial:
'products/product_list'
,
locals:
{
products:
@latest_products
}
%>
<div
class=
"toolbar-bottom"
>
<div
class=
"toolbar"
>
...
...
db/migrate/20170615035827_add_quantity_to_products.rb
0 → 100644
View file @
4f37530b
class
AddQuantityToProducts
<
ActiveRecord
::
Migration
[
5.1
]
def
change
add_column
:products
,
:quantity
,
:integer
,
default:
1
end
end
db/schema.rb
View file @
4f37530b
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2017061
3012113
)
do
ActiveRecord
::
Schema
.
define
(
version:
2017061
5035827
)
do
create_table
"categories"
,
force: :cascade
,
options:
"ENGINE=InnoDB DEFAULT CHARSET=utf8"
do
|
t
|
t
.
string
"title"
...
...
@@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 20170613012113) do
t
.
datetime
"updated_at"
,
null:
false
t
.
string
"image_url"
t
.
bigint
"user_id"
t
.
integer
"quantity"
,
default:
1
t
.
index
[
"category_id"
],
name:
"index_products_on_category_id"
t
.
index
[
"user_id"
],
name:
"index_products_on_user_id"
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment