Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
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
0
Merge Requests
0
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Nguyen Quoc Kien
venshop
Commits
ba3ecacb
Commit
ba3ecacb
authored
Jul 29, 2015
by
Nguyen Quoc Kien
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cars: finish
parent
43e93f27
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
150 additions
and
93 deletions
+150
-93
app/controllers/application_controller.rb
+4
-5
app/controllers/cart_products_controller.rb
+39
-12
app/controllers/carts_controller.rb
+48
-40
app/controllers/products_controller.rb
+4
-0
app/models/cart.rb
+18
-9
app/views/carts/new.html.erb
+1
-1
app/views/carts/show.html.erb
+22
-19
app/views/layouts/_header.html.erb
+1
-1
app/views/order_notifier/shipped.html.erb
+1
-1
app/views/products/_product.html.erb
+7
-2
app/views/static_pages/home.html.erb
+1
-0
config/routes.rb
+2
-1
db/migrate/20150724081823_create_carts.rb
+1
-1
db/schema.rb
+1
-1
No files found.
app/controllers/application_controller.rb
View file @
ba3ecacb
...
...
@@ -8,14 +8,13 @@ class ApplicationController < ActionController::Base
private
def
set_cart
@cart
=
Cart
.
find
(
session
[
:cart_id
])
rescue
ActiveRecord
::
RecordNotFound
@session
=
session
if
current_user
@
cart
=
Cart
.
create
(
user_id:
current_user
.
id
)
@
user_id
=
current_user
.
id
else
@
cart
=
Cart
.
create
()
@
user_id
=
"guess"
end
session
[
:cart_id
]
=
@cart
.
id
@session
[
@user_id
]
||=
{}
end
def
configure_permitted_parameters
...
...
app/controllers/cart_products_controller.rb
View file @
ba3ecacb
class
CartProductsController
<
ApplicationController
before_action
:set_cart
,
only:
[
:create
]
before_action
:check_quantity?
,
only:
[
:create
]
def
create
product
=
Product
.
find
(
params
[
:product_id
])
@cart_product
=
@cart
.
add_product
(
product
.
id
,
product
.
price
)
if
check_quantity?
add_product_to_cart
(
product
.
id
.
to_i
,
params
[
:quantity
].
to_i
)
respond_to
do
|
format
|
if
@cart_product
.
save
format
.
html
{
redirect_to
@cart_product
.
cart
}
format
.
json
{
render
json:
@cart_product
,
status: :created
,
location:
@cart_product
}
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
notice:
'Products add to cart'
}
format
.
json
{
head
:no_content
}
end
else
format
.
html
{
render
action:
"new"
}
format
.
json
{
render
json:
@cart_product
.
errors
,
status: :unprocessable_entity
}
respond_to
do
|
format
|
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
notice:
'Errors: Quantity'
}
format
.
json
{
head
:no_content
}
end
end
end
def
destroy
@cart_product
=
CartProduct
.
find
(
params
[
:id
])
@cart_product
.
destroy
redirect_to
@cart_product
.
cart
session
[
params
[
:id
]].
delete
(
params
[
:product_id
])
redirect_to
cart_path
(
id:
params
[
:id
])
end
private
def
add_product_to_cart
(
product_id
,
number
)
number
||=
1
i
=
0
@session
[
@user_id
].
each
do
|
key
,
value
|
if
(
key
==
product_id
.
to_s
)
@session
[
@user_id
][
key
]
=
number
+
value
i
=
1
break
end
end
if
(
i
==
0
)
@session
[
@user_id
][
product_id
]
=
number
end
end
def
check_quantity?
if
(
params
[
:quantity
].
to_i
&&
params
[
:quantity
].
to_i
>
0
)
return
true
else
false
end
end
end
app/controllers/carts_controller.rb
View file @
ba3ecacb
class
CartsController
<
ApplicationController
before_action
:find_card
,
only:
[
:update
]
def
show
@cart
=
Cart
.
find
(
session
[
:cart_id
])
total_price
=
@cart
.
total_price
@cart
.
update
(
total_price:
total_price
)
#before_action :find_card, only: [ :create ]
#before_action :check_phone, only: [ :create ]
def
update
end
def
edit
@cart
=
Cart
.
find
(
session
[
:cart_id
])
def
new
@cart
=
Cart
.
new
end
def
update
@cart
=
Cart
.
find
(
session
[
:cart_id
])
@user
=
User
.
find
(
@cart
.
user_id
)
@user
.
update
(
address:
params
[
'cart'
][
'address'
],
phone:
params
[
'cart'
][
'phone'
])
@cart
.
update
(
cart_params
)
@cart
.
update
(
status:
"checkout"
)
def
create
total
=
0
@cart
=
Cart
.
new
(
cart_params
)
@cart
.
save
if
current_user
@cart
.
update
(
user_id:
current_user
.
id
,
status:
"Checkout"
)
user_id
=
current_user
.
id
else
@cart
.
update
(
user_id:
""
,
status:
"Checkout"
)
user_id
=
'guess'
end
if
@cart
.
save
session
[
user_id
].
each
do
|
key
,
value
|
@product
=
Product
.
find
(
key
)
@cart_product
=
CartProduct
.
new
(
cart_id:
@cart
.
id
,
product_id:
key
.
to_i
,
number:
value
.
to_i
,
price:
@product
.
price
)
@cart_product
.
save
total
+=
@product
.
price
*
value
.
to_f
end
@cart
.
update
(
total_price:
total
)
OrderNotifier
.
received
(
@cart
).
deliver
session
[
:cart_id
]
=
nil
respond_to
do
|
format
|
format
.
html
{
redirect_to
products_path
,
notice:
'Email to send'
}
format
.
json
{
head
:no_content
}
end
end
def
destroy
@cart
.
destroy
if
@cart
.
id
==
session
[
:cart_id
]
session
[
:cart_id
]
=
nil
session
[
user_id
]
=
nil
else
respond_to
do
|
format
|
format
.
html
{
redirect_to
produc
ts_path
,
notice:
'Your cart is currently empty
'
}
format
.
html
{
redirect_to
car
ts_path
,
notice:
'Errors
'
}
format
.
json
{
head
:no_content
}
end
end
end
private
def
cart_params
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
def
destroy
session
[
params
[
:id
]]
=
nil
redirect_to
cart_path
(
params
[
:id
]
)
end
def
find_card
@cart
=
Cart
.
find
(
params
[
:id
])
def
index
if
current_user
user_id
=
current_user
.
id
else
user_id
=
'guess'
end
redirect_to
cart_path
(
user_id
)
end
def
add_product_to_cart
(
product_id
,
quantity
)
quantity
||=
1
product_id
=
product_id
.
to_s
current_quantity
=
cart_products_hash
.
fetch
(
product_id
,
{}).
fetch
(
'quantity'
,
0
)
quantity
+=
current_quantity
cart_products_hash
[
product_id
]
=
{
'quantity'
=>
quantity
}
end
private
def
remove_product_from_cart
(
product_id
)
product_id
=
product_id
.
to_s
cart_products_hash
.
delete
(
product_id
)
def
cart_params
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
end
def
cart_hash
@
session
[
'cart'
]
def
find_card
@
cart
=
Cart
.
find
(
params
[
:id
])
end
def
c
art_products_hash
@session
[
'cart'
][
'products'
]
def
c
heck_phone
params
[
:phone
].
is_a?
end
end
app/controllers/products_controller.rb
View file @
ba3ecacb
...
...
@@ -11,9 +11,13 @@ class ProductsController < ApplicationController
@categories
=
Category
.
all
end
def
new
end
private
def
find_product
@product
=
Product
.
find
(
params
[
:id
])
end
end
app/models/cart.rb
View file @
ba3ecacb
class
Cart
<
ActiveRecord
::
Base
has_many
:cart_products
,
dependent: :destroy
def
add_product
(
product_id
,
price
)
current_item
=
cart_products
.
find_by
(
product_id:
product_id
)
if
current_item
current_item
.
number
+=
1
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates
:email
,
presence:
true
,
length:
{
maximum:
255
},
format:
{
with:
VALID_EMAIL_REGEX
}
validates
:full_name
,
presence:
true
,
length:
{
maximum:
50
}
validates
:address
,
presence:
true
,
length:
{
maximum:
1000
}
before_save
:downcase_email
def
downcase_email
self
.
email
=
email
.
downcase
end
def
check_phone
if
self
.
phone
.
is_a
return
true
else
current_item
=
cart_products
.
build
(
product_id:
product_id
,
price:
price
)
return
false
end
current_item
end
def
total_price
cart_products
.
to_a
.
sum
{
|
item
|
item
.
total_price
}
end
end
app/views/carts/
edit
.html.erb
→
app/views/carts/
new
.html.erb
View file @
ba3ecacb
...
...
@@ -3,7 +3,7 @@
<div
class=
"row"
>
<div
class=
"col-md-6 col-md-offset-3"
>
<h1>
Đăng ký thông tin nhận hàng
</h1>
<%=
form_for
(
@cart
)
do
|
f
|
%>
<%=
form_for
@cart
do
|
f
|
%>
<%=
f
.
label
:full_name
%>
<%=
f
.
text_field
:full_name
,
class:
'form-control'
%>
...
...
app/views/carts/show.html.erb
View file @
ba3ecacb
<%
provide
(
:title
,
"Your Carts"
)
%>
<%
if
notice
%>
<p
id=
"notice"
>
<%=
notice
%>
</p>
<%
end
%>
<h2>
Your Cart
</h2>
<table
class=
"table table-hover"
>
<%
if
session
[
params
[
:id
]]
!=
nil
&&
session
[
params
[
:id
]]
!=
{}
%>
<thead>
<tr>
<th>
Product name
</th>
...
...
@@ -14,30 +12,34 @@
</tr>
</thead>
<tbody>
<%
@cart
.
cart_products
.
each
do
|
item
|
%>
<%
total
=
0
%>
<%
session
[
params
[
:id
]].
each
do
|
key
,
value
|
%>
<tr>
<td>
<%=
item
.
product
.
name
%>
</td>
<td>
<%=
number_to_currency
(
item
.
product
.
price
/
100.000
)
%>
</td>
<td>
<%=
item
.
number
%>
</td>
<td>
<%=
number_to_currency
((
item
.
product
.
price
*
item
.
number
)
/
100.000
)
%>
</td>
<td
>
<%=
link_to
'Delete'
,
cart_product_path
(
id:
item
.
id
),
data:
{
confirm:
'Are you sure?'
},
method: :delete
,
data:
{
confirm:
'Are you sure?'
}
%>
|
<%=
link_to
'Update'
,
"#"
%>
</td>
<td>
<%=
Product
.
find
(
key
).
name
%>
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.00
)
%>
</td>
<td>
<%=
value
%>
</td>
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
)
%>
</td>
<%
total
+=
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
%>
<td>
<%=
link_to
'Delete'
,
cart_product_path
(
product_id:
key
,
id:
params
[
:id
]),
data:
{
confirm:
'Are you sure?'
},
method: :delete
,
data:
{
confirm:
'Are you sure?'
}
%>
</td>
</tr>
<%
end
%>
<tr>
<td
colspan=
"3"
>
Total:
</td>
<td>
<%=
number_to_currency
(
@cart
.
total_price
/
100.000
)
%>
</td>
<td>
Total
</td>
<td></td>
<td></td>
<td>
<%=
number_to_currency
(
total
)
%>
</td>
</tr>
<tr>
<td>
<%=
link_to
'Back'
,
products_path
,
class:
"btn btn-danger"
%>
</td>
<td>
</td>
<td>
<%=
button_to
'Empty cart'
,
@cart
,
method: :delete
,
data:
{
confirm:
'Are you sure?'
},
class:
"btn btn-danger"
%>
</td>
<td>
<%=
button_to
'Checkout'
,
edit_cart_path
(
@cart
),
method: :get
,
class:
"btn btn-danger"
%>
</td>
</tr
>
<td>
<%=
button_to
'Empty cart'
,
cart_path
(
id:
params
[
:id
])
,
method: :delete
,
data:
{
confirm:
'Are you sure?'
},
class:
"btn btn-danger"
%>
</td>
<td>
<%=
link_to
'Checkout'
,
new_cart_path
,
class:
"btn btn-danger"
%>
</td>
<td>
</td
>
</tr>
</tbody>
<%
else
%>
<h1>
Cart Empty
</h1>
<%
end
%>
</table>
\ No newline at end of file
app/views/layouts/_header.html.erb
View file @
ba3ecacb
...
...
@@ -17,7 +17,7 @@
<li>
<%=
link_to
"Categories"
,
categories_path
%>
</li>
<li>
<%=
link_to
"Carts"
,
"#"
%>
</li>
<li>
<%=
link_to
"Carts"
,
carts_path
%>
</li>
</ul>
<ul
class=
"nav navbar-nav navbar-right"
>
<%
if
user_signed_in?
%>
...
...
app/views/order_notifier/shipped.html.erb
View file @
ba3ecacb
<h1>
OrderNotifier#shipped
</h1>
<p>
<%=
@greeting
%>
,
find me in app/views/order_notifier/shipped.html.erb
<%=
@greeting
%>
,
45
</p>
app/views/products/_product.html.erb
View file @
ba3ecacb
...
...
@@ -6,9 +6,14 @@
<h3
title=
"
<%=
product
.
name
%>
"
>
<%=
truncate
(
product
.
name
,
length:
25
)
%>
</h3>
</div>
<p><b>
Price:
</b>
<%=
number_to_currency
(
product
.
price
/
100.000
)
%>
</p>
<p>
Quanlity:
<%=
text_field
:number
,
class:
'form-control'
%>
<%=
button_to
"Add To cart"
,
cart_products_path
(
product_id:
product
),
:class
=>
"btn btn-primary"
,
:style
=>
"width: 100px"
%>
<br/>
<%=
link_to
"More Info"
,
product
,
:class
=>
"btn btn-default"
%>
<%=
form_tag
cart_products_path
do
%>
<p>
<%=
hidden_field_tag
:product_id
,
product
.
id
%>
Quantity:
<b>
<%=
number_field_tag
:quantity
,
"1"
,
class:
'form-control'
%>
</b><br/>
<%=
submit_tag
"Add To cart"
,
:class
=>
"btn btn-primary"
,
:style
=>
"width: 100px"
%>
</p>
<%
end
%>
<%=
link_to
"More Info"
,
product
,
:class
=>
"btn btn-default"
%>
</div>
</div>
</div>
app/views/static_pages/home.html.erb
View file @
ba3ecacb
...
...
@@ -5,4 +5,5 @@
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
</p>
<p>
<%=
link_to
"Sign up now!"
,
new_user_registration_path
,
class:
"btn btn-lg btn-primary"
%>
</p>
<p>
<%=
link_to
"Admin"
,
admins_path
,
class:
"btn btn-lg btn-primary"
%>
</p>
</header>
config/routes.rb
View file @
ba3ecacb
Rails
.
application
.
routes
.
draw
do
devise_for
:users
get
'carts/index'
devise_for
:users
root
to:
"static_pages#home"
get
'help'
=>
'static_pages#help'
...
...
@@ -11,6 +11,7 @@ Rails.application.routes.draw do
resources
:products
resources
:carts
resources
:cart_products
,
only:
[
:create
,
:destroy
]
resources
:admins
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
...
...
db/migrate/20150724081823_create_carts.rb
View file @
ba3ecacb
...
...
@@ -3,7 +3,7 @@ class CreateCarts < ActiveRecord::Migration
create_table
:carts
do
|
t
|
t
.
integer
:user_id
t
.
decimal
:total_price
,
:default
=>
0
t
.
string
:status
,
:default
=>
"Prosess"
t
.
string
:status
t
.
string
:full_name
t
.
integer
:phone
t
.
string
:email
...
...
db/schema.rb
View file @
ba3ecacb
...
...
@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
create_table
"carts"
,
force: :cascade
do
|
t
|
t
.
integer
"user_id"
,
limit:
4
t
.
decimal
"total_price"
,
precision:
10
,
default:
0
t
.
string
"status"
,
limit:
255
,
default:
"Prosess"
t
.
string
"status"
,
limit:
255
t
.
string
"full_name"
,
limit:
255
t
.
integer
"phone"
,
limit:
4
t
.
string
"email"
,
limit:
255
...
...
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