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
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
179 additions
and
124 deletions
+179
-124
app/controllers/application_controller.rb
+4
-5
app/controllers/cart_products_controller.rb
+41
-14
app/controllers/carts_controller.rb
+54
-46
app/controllers/products_controller.rb
+4
-0
app/models/cart.rb
+19
-10
app/views/carts/new.html.erb
+1
-1
app/views/carts/show.html.erb
+39
-38
app/views/layouts/_header.html.erb
+1
-1
app/views/order_notifier/shipped.html.erb
+1
-1
app/views/products/_product.html.erb
+8
-3
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
+3
-3
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?
respond_to
do
|
format
|
add_product_to_cart
(
product
.
id
.
to_i
,
params
[
:quantity
].
to_i
)
if
@cart_product
.
save
respond_to
do
|
format
|
format
.
html
{
redirect_to
@cart_product
.
cart
}
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
format
.
json
{
render
json:
@cart_product
,
notice:
'Products add to cart'
}
status: :created
,
location:
@cart_product
}
format
.
json
{
head
:no_content
}
else
end
format
.
html
{
render
action:
"new"
}
else
format
.
json
{
render
json:
@cart_product
.
errors
,
respond_to
do
|
format
|
status: :unprocessable_entity
}
format
.
html
{
redirect_to
cart_path
(
id:
@user_id
),
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
OrderNotifier
.
received
(
@cart
).
deliver
@cart
.
update
(
user_id:
current_user
.
id
,
status:
"Checkout"
)
session
[
:cart_id
]
=
nil
user_id
=
current_user
.
id
respond_to
do
|
format
|
else
format
.
html
{
redirect_to
products_path
,
@cart
.
update
(
user_id:
""
,
status:
"Checkout"
)
notice:
'Email to send'
}
user_id
=
'guess'
format
.
json
{
head
:no_content
}
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
respond_to
do
|
format
|
format
.
html
{
redirect_to
products_path
,
notice:
'Email to send'
}
format
.
json
{
head
:no_content
}
end
session
[
user_id
]
=
nil
else
respond_to
do
|
format
|
format
.
html
{
redirect_to
carts_path
,
notice:
'Errors'
}
format
.
json
{
head
:no_content
}
end
end
end
end
end
def
destroy
def
destroy
@cart
.
destroy
if
@cart
.
id
==
session
[
:cart_id
]
session
[
params
[
:id
]]
=
nil
session
[
:cart_id
]
=
nil
redirect_to
cart_path
(
params
[
:id
])
respond_to
do
|
format
|
end
format
.
html
{
redirect_to
products_path
,
notice:
'Your cart is currently empty'
}
def
index
format
.
json
{
head
:no_content
}
if
current_user
user_id
=
current_user
.
id
else
user_id
=
'guess'
end
end
redirect_to
cart_path
(
user_id
)
end
end
private
private
def
cart_params
def
cart_params
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
params
.
require
(
:cart
).
permit
(
:full_name
,
:email
,
:address
,
:phone
)
end
end
...
@@ -44,27 +71,8 @@ class CartsController < ApplicationController
...
@@ -44,27 +71,8 @@ class CartsController < ApplicationController
@cart
=
Cart
.
find
(
params
[
:id
])
@cart
=
Cart
.
find
(
params
[
:id
])
end
end
def
add_product_to_cart
(
product_id
,
quantity
)
def
check_phone
quantity
||=
1
params
[
:phone
].
is_a?
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
def
remove_product_from_cart
(
product_id
)
product_id
=
product_id
.
to_s
cart_products_hash
.
delete
(
product_id
)
end
def
cart_hash
@session
[
'cart'
]
end
def
cart_products_hash
@session
[
'cart'
][
'products'
]
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
}
else
validates
:address
,
presence:
true
,
length:
{
maximum:
1000
}
current_item
=
cart_products
.
build
(
product_id:
product_id
,
price:
price
)
end
before_save
:downcase_email
current_item
def
downcase_email
self
.
email
=
email
.
downcase
end
end
def
total_price
def
check_phone
cart_products
.
to_a
.
sum
{
|
item
|
item
.
total_price
}
if
self
.
phone
.
is_a
return
true
else
return
false
end
end
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"
>
<thead>
<%
if
session
[
params
[
:id
]]
!=
nil
&&
session
[
params
[
:id
]]
!=
{}
%>
<tr>
<thead>
<th>
Product name
</th>
<tr>
<th>
Price
</th>
<th>
Product name
</th>
<th>
Quantity
</th>
<th>
Price
</th>
<th>
Total price
</th>
<th>
Quantity
</th>
<th>
Delete
</th>
<th>
Total price
</th>
</tr>
<th>
Delete
</th>
</thead>
</tr>
<tbody>
</thead>
<%
@cart
.
cart_products
.
each
do
|
item
|
%>
<tbody>
<tr>
<%
total
=
0
%>
<td>
<%=
item
.
product
.
name
%>
</td>
<%
session
[
params
[
:id
]].
each
do
|
key
,
value
|
%>
<td>
<%=
number_to_currency
(
item
.
product
.
price
/
100.000
)
%>
</td>
<tr>
<td>
<%=
item
.
number
%>
</td>
<td>
<%=
Product
.
find
(
key
).
name
%>
<td>
<%=
number_to_currency
((
item
.
product
.
price
*
item
.
number
)
/
100.000
)
%>
</td>
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.00
)
%>
</td>
<td
>
<td>
<%=
value
%>
</td>
<%=
link_to
'Delete'
,
cart_product_path
(
id:
item
.
id
),
data:
{
confirm:
'Are you sure?'
},
method: :delete
,
<td>
<%=
number_to_currency
(
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
)
%>
</td>
data:
{
confirm:
'Are you sure?'
}
%>
|
<%
total
+=
Product
.
find
(
key
).
price
/
100.000
*
value
.
to_f
%>
<%=
link_to
'Update'
,
"#"
%>
<td>
<%=
link_to
'Delete'
,
cart_product_path
(
product_id:
key
,
id:
params
[
:id
]),
data:
{
confirm:
'Are you sure?'
},
method: :delete
,
</td>
data:
{
confirm:
'Are you sure?'
}
%>
</td>
</tr>
</tr>
<%
end
%>
<tr>
<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_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
%>
<%
end
%>
<tr>
<td
colspan=
"3"
>
Total:
</td>
<td>
<%=
number_to_currency
(
@cart
.
total_price
/
100.000
)
%>
</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>
</tr>
</tbody>
</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>
</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>
</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,13 +25,13 @@ ActiveRecord::Schema.define(version: 20150727094623) do
...
@@ -25,13 +25,13 @@ 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
t
.
text
"address"
,
limit:
65535
t
.
text
"address"
,
limit:
65535
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
end
create_table
"categories"
,
force: :cascade
do
|
t
|
create_table
"categories"
,
force: :cascade
do
|
t
|
...
...
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