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
...
@@ -8,14 +8,13 @@ class ApplicationController < ActionController::Base
private
private
def
set_cart
def
set_cart
@cart
=
Cart
.
find
(
session
[
:cart_id
])
@session
=
session
rescue
ActiveRecord
::
RecordNotFound
if
current_user
if
current_user
@
cart
=
Cart
.
create
(
user_id:
current_user
.
id
)
@
user_id
=
current_user
.
id
else
else
@
cart
=
Cart
.
create
()
@
user_id
=
"guess"
end
end
session
[
:cart_id
]
=
@cart
.
id
@session
[
@user_id
]
||=
{}
end
end
def
configure_permitted_parameters
def
configure_permitted_parameters
...
...
app/controllers/cart_products_controller.rb
View file @
ba3ecacb
class
CartProductsController
<
ApplicationController
class
CartProductsController
<
ApplicationController
before_action
:set_cart
,
only:
[
:create
]
before_action
:set_cart
,
only:
[
:create
]
before_action
:check_quantity?
,
only:
[
:create
]
def
create
def
create
product
=
Product
.
find
(
params
[
:product_id
])
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
|
respond_to
do
|
format
|
if
@cart_product
.
save
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
format
.
html
{
redirect_to
@cart_product
.
cart
}
notice:
'Products add to cart'
}
format
.
json
{
render
json:
@cart_product
,
format
.
json
{
head
:no_content
}
status: :created
,
location:
@cart_product
}
end
else
else
format
.
html
{
render
action:
"new"
}
respond_to
do
|
format
|
format
.
json
{
render
json:
@cart_product
.
errors
,
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
status: :unprocessable_entity
}
notice:
'Errors: Quantity'
}
format
.
json
{
head
:no_content
}
end
end
end
end
end
end
def
destroy
def
destroy
@cart_product
=
CartProduct
.
find
(
params
[
:id
])
session
[
params
[
:id
]].
delete
(
params
[
:product_id
])
@cart_product
.
destroy
redirect_to
cart_path
(
id:
params
[
:id
])
redirect_to
@cart_product
.
cart
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
end
end
app/controllers/carts_controller.rb
View file @
ba3ecacb
class
CartsController
<
ApplicationController
class
CartsController
<
ApplicationController
before_action
:find_card
,
only:
[
:update
]
#before_action :find_card, only: [ :create ]
def
show
#before_action :check_phone, only: [ :create ]
@cart
=
Cart
.
find
(
session
[
:cart_id
])
total_price
=
@cart
.
total_price
def
update
@cart
.
update
(
total_price:
total_price
)
end
end
def
edit
def
new
@cart
=
Cart
.
find
(
session
[
:cart_id
])
@cart
=
Cart
.
new
end
end
def
update
def
create
@cart
=
Cart
.
find
(
session
[
:cart_id
])
total
=
0
@user
=
User
.
find
(
@cart
.
user_id
)
@user
.
update
(
address:
params
[
'cart'
][
'address'
],
phone:
params
[
'cart'
][
'phone'
])
@cart
=
Cart
.
new
(
cart_params
)
@cart
.
update
(
cart_params
)
@cart
.
save
@cart
.
update
(
status:
"checkout"
)
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
OrderNotifier
.
received
(
@cart
).
deliver
session
[
:cart_id
]
=
nil
respond_to
do
|
format
|
respond_to
do
|
format
|
format
.
html
{
redirect_to
products_path
,
format
.
html
{
redirect_to
products_path
,
notice:
'Email to send'
}
notice:
'Email to send'
}
format
.
json
{
head
:no_content
}
format
.
json
{
head
:no_content
}
end
end
end
session
[
user_id
]
=
nil
else
def
destroy
@cart
.
destroy
if
@cart
.
id
==
session
[
:cart_id
]
session
[
:cart_id
]
=
nil
respond_to
do
|
format
|
respond_to
do
|
format
|
format
.
html
{
redirect_to
produc
ts_path
,
format
.
html
{
redirect_to
car
ts_path
,
notice:
'Your cart is currently empty
'
}
notice:
'Errors
'
}
format
.
json
{
head
:no_content
}
format
.
json
{
head
:no_content
}
end
end
end
end
end
private
def
destroy
def
cart_params
session
[
params
[
:id
]]
=
nil
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
redirect_to
cart_path
(
params
[
:id
]
)
end
end
def
find_card
def
index
@cart
=
Cart
.
find
(
params
[
:id
])
if
current_user
user_id
=
current_user
.
id
else
user_id
=
'guess'
end
redirect_to
cart_path
(
user_id
)
end
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
}
private
end
def
remove_product_from_cart
(
product_id
)
def
cart_params
product_id
=
product_id
.
to_s
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
cart_products_hash
.
delete
(
product_id
)
end
end
def
cart_hash
def
find_card
@
session
[
'cart'
]
@
cart
=
Cart
.
find
(
params
[
:id
])
end
end
def
c
art_products_hash
def
c
heck_phone
@session
[
'cart'
][
'products'
]
params
[
:phone
].
is_a?
end
end
end
end
app/controllers/products_controller.rb
View file @
ba3ecacb
...
@@ -11,9 +11,13 @@ class ProductsController < ApplicationController
...
@@ -11,9 +11,13 @@ class ProductsController < ApplicationController
@categories
=
Category
.
all
@categories
=
Category
.
all
end
end
def
new
end
private
private
def
find_product
def
find_product
@product
=
Product
.
find
(
params
[
:id
])
@product
=
Product
.
find
(
params
[
:id
])
end
end
end
end
app/models/cart.rb
View file @
ba3ecacb
class
Cart
<
ActiveRecord
::
Base
class
Cart
<
ActiveRecord
::
Base
has_many
:cart_products
,
dependent: :destroy
has_many
:cart_products
,
dependent: :destroy
def
add_product
(
product_id
,
price
)
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
current_item
=
cart_products
.
find_by
(
product_id:
product_id
)
validates
:email
,
presence:
true
,
length:
{
maximum:
255
},
if
current_item
format:
{
with:
VALID_EMAIL_REGEX
}
current_item
.
number
+=
1
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
else
current_item
=
cart_products
.
build
(
product_id:
product_id
,
price:
price
)
return
false
end
end
current_item
end
end
def
total_price
cart_products
.
to_a
.
sum
{
|
item
|
item
.
total_price
}
end
end
end
app/views/carts/
edit
.html.erb
→
app/views/carts/
new
.html.erb
View file @
ba3ecacb
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-md-6 col-md-offset-3"
>
<div
class=
"col-md-6 col-md-offset-3"
>
<h1>
Đăng ký thông tin nhận hàng
</h1>
<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
.
label
:full_name
%>
<%=
f
.
text_field
:full_name
,
class:
'form-control'
%>
<%=
f
.
text_field
:full_name
,
class:
'form-control'
%>
...
...
app/views/carts/show.html.erb
View file @
ba3ecacb
<%
provide
(
:title
,
"Your Carts"
)
%>
<%
provide
(
:title
,
"Your Carts"
)
%>
<%
if
notice
%>
<p
id=
"notice"
>
<%=
notice
%>
</p>
<%
end
%>
<h2>
Your Cart
</h2>
<h2>
Your Cart
</h2>
<table
class=
"table table-hover"
>
<table
class=
"table table-hover"
>
<%
if
session
[
params
[
:id
]]
!=
nil
&&
session
[
params
[
:id
]]
!=
{}
%>
<thead>
<thead>
<tr>
<tr>
<th>
Product name
</th>
<th>
Product name
</th>
...
@@ -14,30 +12,34 @@
...
@@ -14,30 +12,34 @@
</tr>
</tr>
</thead>
</thead>
<tbody>
<tbody>
<%
@cart
.
cart_products
.
each
do
|
item
|
%>
<%
total
=
0
%>
<%
session
[
params
[
:id
]].
each
do
|
key
,
value
|
%>
<tr>
<tr>
<td>
<%=
item
.
product
.
name
%>
</td>
<td>
<%=
Product
.
find
(
key
).
name
%>
<td>
<%=
number_to_currency
(
item
.
product
.
price
/
100.000
)
%>
</td>
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.00
)
%>
</td>
<td>
<%=
item
.
number
%>
</td>
<td>
<%=
value
%>
</td>
<td>
<%=
number_to_currency
((
item
.
product
.
price
*
item
.
number
)
/
100.000
)
%>
</td>
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
)
%>
</td>
<td
>
<%
total
+=
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
%>
<%=
link_to
'Delete'
,
cart_product_path
(
id:
item
.
id
),
data:
{
confirm:
'Are you sure?'
},
method: :delete
,
<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?'
}
%>
|
data:
{
confirm:
'Are you sure?'
}
%>
</td>
<%=
link_to
'Update'
,
"#"
%>
</td>
</tr>
</tr>
<%
end
%>
<%
end
%>
<tr>
<tr>
<td
colspan=
"3"
>
Total:
</td>
<td>
Total
</td>
<td>
<%=
number_to_currency
(
@cart
.
total_price
/
100.000
)
%>
</td>
<td></td>
<td></td>
<td>
<%=
number_to_currency
(
total
)
%>
</td>
</tr>
</tr>
<tr>
<tr>
<td>
<%=
link_to
'Back'
,
products_path
,
class:
"btn btn-danger"
%>
</td>
<td>
<%=
link_to
'Back'
,
products_path
,
class:
"btn btn-danger"
%>
</td>
<td>
</td>
<td>
</td>
<td>
<%=
button_to
'Empty cart'
,
@cart
,
method: :delete
,
data:
{
confirm:
'Are you sure?'
},
class:
"btn btn-danger"
%>
</td>
<td>
<%=
button_to
'Empty cart'
,
cart_path
(
id:
params
[
:id
])
,
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>
<td>
<%=
link_to
'Checkout'
,
new_cart_path
,
class:
"btn btn-danger"
%>
</td>
</tr
>
<td>
</td
>
</tr>
</tr>
</tbody>
</tbody>
<%
else
%>
<h1>
Cart Empty
</h1>
<%
end
%>
</table>
</table>
\ No newline at end of file
app/views/layouts/_header.html.erb
View file @
ba3ecacb
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
<li>
<li>
<%=
link_to
"Categories"
,
categories_path
%>
<%=
link_to
"Categories"
,
categories_path
%>
</li>
</li>
<li>
<%=
link_to
"Carts"
,
"#"
%>
</li>
<li>
<%=
link_to
"Carts"
,
carts_path
%>
</li>
</ul>
</ul>
<ul
class=
"nav navbar-nav navbar-right"
>
<ul
class=
"nav navbar-nav navbar-right"
>
<%
if
user_signed_in?
%>
<%
if
user_signed_in?
%>
...
...
app/views/order_notifier/shipped.html.erb
View file @
ba3ecacb
<h1>
OrderNotifier#shipped
</h1>
<h1>
OrderNotifier#shipped
</h1>
<p>
<p>
<%=
@greeting
%>
,
find me in app/views/order_notifier/shipped.html.erb
<%=
@greeting
%>
,
45
</p>
</p>
app/views/products/_product.html.erb
View file @
ba3ecacb
...
@@ -6,9 +6,14 @@
...
@@ -6,9 +6,14 @@
<h3
title=
"
<%=
product
.
name
%>
"
>
<%=
truncate
(
product
.
name
,
length:
25
)
%>
</h3>
<h3
title=
"
<%=
product
.
name
%>
"
>
<%=
truncate
(
product
.
name
,
length:
25
)
%>
</h3>
</div>
</div>
<p><b>
Price:
</b>
<%=
number_to_currency
(
product
.
price
/
100.000
)
%>
</p>
<p><b>
Price:
</b>
<%=
number_to_currency
(
product
.
price
/
100.000
)
%>
</p>
<p>
Quanlity:
<%=
text_field
:number
,
class:
'form-control'
%>
<%=
form_tag
cart_products_path
do
%>
<%=
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"
%>
<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>
</p>
<%
end
%>
<%=
link_to
"More Info"
,
product
,
:class
=>
"btn btn-default"
%>
</div>
</div>
</div>
</div>
</div>
</div>
app/views/static_pages/home.html.erb
View file @
ba3ecacb
...
@@ -5,4 +5,5 @@
...
@@ -5,4 +5,5 @@
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
</p>
</p>
<p>
<%=
link_to
"Sign up now!"
,
new_user_registration_path
,
class:
"btn btn-lg btn-primary"
%>
</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>
</header>
config/routes.rb
View file @
ba3ecacb
Rails
.
application
.
routes
.
draw
do
Rails
.
application
.
routes
.
draw
do
devise_for
:users
get
'carts/index'
get
'carts/index'
devise_for
:users
root
to:
"static_pages#home"
root
to:
"static_pages#home"
get
'help'
=>
'static_pages#help'
get
'help'
=>
'static_pages#help'
...
@@ -11,6 +11,7 @@ Rails.application.routes.draw do
...
@@ -11,6 +11,7 @@ Rails.application.routes.draw do
resources
:products
resources
:products
resources
:carts
resources
:carts
resources
:cart_products
,
only:
[
:create
,
:destroy
]
resources
:cart_products
,
only:
[
:create
,
:destroy
]
resources
:admins
# The priority is based upon order of creation: first created -> highest priority.
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# 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
...
@@ -3,7 +3,7 @@ class CreateCarts < ActiveRecord::Migration
create_table
:carts
do
|
t
|
create_table
:carts
do
|
t
|
t
.
integer
:user_id
t
.
integer
:user_id
t
.
decimal
:total_price
,
:default
=>
0
t
.
decimal
:total_price
,
:default
=>
0
t
.
string
:status
,
:default
=>
"Prosess"
t
.
string
:status
t
.
string
:full_name
t
.
string
:full_name
t
.
integer
:phone
t
.
integer
:phone
t
.
string
:email
t
.
string
:email
...
...
db/schema.rb
View file @
ba3ecacb
...
@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
...
@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
create_table
"carts"
,
force: :cascade
do
|
t
|
create_table
"carts"
,
force: :cascade
do
|
t
|
t
.
integer
"user_id"
,
limit:
4
t
.
integer
"user_id"
,
limit:
4
t
.
decimal
"total_price"
,
precision:
10
,
default:
0
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
.
string
"full_name"
,
limit:
255
t
.
integer
"phone"
,
limit:
4
t
.
integer
"phone"
,
limit:
4
t
.
string
"email"
,
limit:
255
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