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
Dao Minh Nhut
VenShop
Commits
f67de3d0
Commit
f67de3d0
authored
Aug 04, 2015
by
Dao Minh Nhut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bug
parent
9452905f
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
167 additions
and
125 deletions
+167
-125
app/controllers/admins/users_controller.rb
+2
-4
app/controllers/carts_controller.rb
+28
-15
app/controllers/categories_controller.rb
+7
-2
app/controllers/products_controller.rb
+5
-1
app/models/cart.rb
+2
-3
app/models/category.rb
+0
-3
app/models/product.rb
+2
-2
app/views/admins/products/show.html.erb
+3
-1
app/views/cart_products/index.html.erb
+50
-47
app/views/carts/info.html.erb
+24
-13
app/views/products/index.html.erb
+0
-1
config/routes.rb
+0
-4
db/migrate/20150721070908_create_products.rb
+1
-1
db/migrate/20150804021527_create_carts.rb
+2
-2
db/migrate/20150804082910_change_limit.rb
+14
-0
db/schema.rb
+11
-11
lib/import_amazon.rb
+10
-9
test/controllers/admins/users_controller_test.rb
+2
-2
test/fixtures/carts.yml
+4
-4
No files found.
app/controllers/admins/users_controller.rb
View file @
f67de3d0
class
Admin
::
UsersController
<
ApplicationController
class
Admin
s
::
UsersController
<
ApplicationController
def
show
@users
=
User
.
all
end
end
\ No newline at end of file
end
app/controllers/carts_controller.rb
View file @
f67de3d0
...
...
@@ -7,6 +7,7 @@ class CartsController < ApplicationController
end
def
info
@new_cart
=
Cart
.
new
if
user_signed_in?
@current_user
end
...
...
@@ -14,17 +15,19 @@ class CartsController < ApplicationController
def
create
if
!
session
[
:cart
].
nil?
new_cart
=
Cart
.
new
(
cart_params
)
new_cart
.
total_price
=
calculate_total_price
new_cart
.
status
=
"new cart"
if
user_signed_in?
new_cart
.
user_id
=
current_user
.
id
end
new_cart
.
save
if
!
new_cart
.
id
.
nil?
@new_cart
=
Cart
.
new
(
fullname:
params
[
:cart
][
:fullname
],
email:
params
[
:cart
][
:email
],
address:
params
[
:cart
][
:address
],
phone:
params
[
:cart
][
:phone
],
total_price:
calculate_total_price
,
status:
"new cart"
,
user_id:
cart_user_id
)
#new_cart.total_price = calculate_total_price
#new_cart.status = "new cart"
#if user_signed_in?
# new_cart.user_id = current_user.id
#end
if
@new_cart
.
save
#if !new_cart.id.nil?
session
[
:cart
].
each
do
|
id
,
quantity
|
cart_product
=
CartProduct
.
new
cart_product
.
cart_id
=
new_cart
.
id
cart_product
.
cart_id
=
@
new_cart
.
id
cart_product
.
product_id
=
id
cart_product
.
price
=
Product
.
find_by_id
(
id
).
price
cart_product
.
quantity
=
quantity
...
...
@@ -33,14 +36,16 @@ class CartsController < ApplicationController
flash
[
:success
]
=
"Success!"
Emailer
.
send_email_to
(
cart_params
[
:email
].
to_s
,
session
[
:cart
]).
deliver
session
[
:cart
]
=
nil
redirect_to
cart_path
else
render
:info
end
end
flash
[
:danger
]
=
"Wrong input please input again!"
render
:info
end
def
cart_params
params
.
require
(
:
session
).
permit
(
:fullname
,
:email
,
:address
,
:phone
)
params
.
require
(
:
cart
).
permit
(
:fullname
,
:email
,
:address
,
:phone
)
end
private
...
...
@@ -49,12 +54,20 @@ class CartsController < ApplicationController
total
=
0
if
!
session
[
:cart
].
nil?
session
[
:cart
].
each
do
|
id
,
quantity
|
product
=
Product
.
find_by_id
(
id
)
if
!
product
.
nil?
total
=
total
+
product
.
price
*
quantity
product
=
Product
.
find_by_id
(
id
)
if
!
product
.
nil?
total
=
total
+
product
.
price
*
quantity
end
end
end
end
total
end
def
cart_user_id
user_id
=
""
if
user_signed_in?
user_id
=
current_user
.
id
end
user_id
end
end
app/controllers/categories_controller.rb
View file @
f67de3d0
class
CategoriesController
<
ApplicationController
include
CategoriesHelper
def
show
category
=
Category
.
find
(
params
[
:id
])
@products
=
category
.
products
begin
category
=
Category
.
find
(
params
[
:id
])
@products
=
category
.
products
rescue
redirect_to
root_path
end
end
end
\ No newline at end of file
app/controllers/products_controller.rb
View file @
f67de3d0
...
...
@@ -2,7 +2,11 @@ class ProductsController < ApplicationController
include
CategoriesHelper
include
CartProductsHelper
def
index
@products
=
Product
.
paginate
(
page:
params
[
:page
])
begin
@products
=
Product
.
paginate
(
page:
params
[
:page
])
rescue
@products
=
Product
.
paginate
(
page:
"1"
)
end
end
def
show
begin
...
...
app/models/cart.rb
View file @
f67de3d0
...
...
@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base
has_many
:cart_product
VALID_PHONE_REGEX
=
/\d[0-9]\)*\z/
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_NUMBER_REGEX
=
/\A[+-]?\d+\Z/
validates
:email
,
presence:
true
,
length:
{
maximum:
255
},
format:
{
with:
VALID_EMAIL_REGEX
}
validates
:email
,
presence:
true
,
length:
{
maximum:
100
},
format:
{
with:
VALID_EMAIL_REGEX
}
validates
:phone
,
presence:
true
,
length:
{
maximum:
15
},
format:
{
with:
VALID_PHONE_REGEX
}
validates
:total_price
,
presence:
true
,
format:
{
with:
VALID_NUMBER_REGEX
}
validates
:total_price
,
:numericality
=>
{
:greater_than_or_equal_to
=>
0
,
:less_than_or_equal_to
=>
99999999
}
validates
:fullname
,
presence:
true
,
length:
{
maximum:
50
}
validates
:address
,
presence:
true
,
length:
{
maximum:
1000
}
end
app/models/category.rb
View file @
f67de3d0
class
Category
<
ActiveRecord
::
Base
has_many
:products
def
new
@category
=
Category
.
new
end
end
app/models/product.rb
View file @
f67de3d0
class
Product
<
ActiveRecord
::
Base
belongs_to
:category
has_many
:cart_products
VALID_NUMBER_REGEX
=
/\A[+-]?\d+\Z/
validates
:category_id
,
presence:
true
validates
:name
,
:image
,
presence:
true
,
length:
{
maximum:
1000
}
validates
:description
,
length:
{
maximum:
65535
}
validates
:price
,
:numericality
=>
{
:greater_than_or_equal_to
=>
0
,
:less_than_or_equal_to
=>
9999
}
validates
:price
,
:numericality
=>
{
:greater_than_or_equal_to
=>
0
,
:less_than_or_equal_to
=>
99999999
}
validates
:page
,
:numericality
=>
{
:greater_than_or_equal_to
=>
0
}
end
app/views/admins/products/show.html.erb
View file @
f67de3d0
<%
if
admin_signed_in?
%>
<div
class=
"span12"
>
<h2>
Products
</h2>
<%=
link_to
"Insert Product"
,
insert_product_path
%>
<%=
link_to
"Insert Product"
,
insert_product_path
,
class:
"pull-right"
%>
<table
class=
"table table-striped table-hover"
>
<thead>
<tr>
<th>
Product Name
</th>
<th>
Image
</th>
<th>
Edit
</th>
<th>
Delete
</th>
</tr>
...
...
@@ -14,6 +15,7 @@
<%
@products
.
each
do
|
product
|
%>
<tr>
<td>
<%=
product
.
name
%>
</td>
<td>
<%=
image_tag
product
.
image
,
height:
'60'
,
width:
'60'
%>
</td>
<td>
<%=
link_to
"Edit"
,
"/update_product/
#{
product
.
id
}
"
%>
</td>
<td>
<%=
link_to
"Delete"
,
"/delete_product/
#{
product
.
id
}
"
%>
</td>
</tr>
...
...
app/views/cart_products/index.html.erb
View file @
f67de3d0
...
...
@@ -2,55 +2,59 @@
<div
class=
"span9"
>
<h2>
Shopping Cart
</h2>
<div>
<ul>
<table
class=
"table table-striped table-hover"
>
<thead>
<tr>
<th>
Product Name
</th>
<th>
Quantity
</th>
<th>
Update
</th>
<th>
Delete
</th>
<th>
Price
</th>
<th>
Total
</th>
</tr>
</thead>
<tbody>
<%
total
=
0
%>
<%
if
!
@cart_product
.
nil?
%>
<%
@cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
total
=
total
+
product
.
price
*
quantity
.
to_i
%>
<tr>
<td>
<%=
link_to
truncate
(
product
.
name
,
length
:
20
),
"/products/
#{
product
.
id
}
"
%>
</td>
<form
action=
"/cart_product/update"
>
<td><input
name=
"new_quantity"
min=
"1"
max=
"100"
type=
"number"
class=
"span1"
value=
<%=
quantity
%>
/
></td>
<td><input
type=
"submit"
value=
"Update"
/><input
type=
"hidden"
name=
"id"
value=
"
<%=
product
.
id
%>
"
/></td>
</form>
<td><form
action=
"/cart_product/remove"
><input
type=
"submit"
value=
"Delete"
/><input
type=
"hidden"
name=
"id"
value=
"
<%=
product
.
id
%>
"
/></form></td>
<td>
<%=
(
product
.
price
/
100
.
to_f
).
to_s
+
"$"
%>
</td>
<td>
<%=
((
product
.
price
*
quantity
)
/
100
.
to_f
).
to_s
+
"$"
%>
</td>
</tr>
<%
if
!
session
[
:cart
].
nil?
&&
!
session
[
:cart
].
empty?
%>
<div>
<ul>
<table
class=
"table table-striped table-hover"
>
<thead>
<tr>
<th>
Product Name
</th>
<th>
Quantity
</th>
<th>
Update
</th>
<th>
Delete
</th>
<th>
Price
</th>
<th>
Total
</th>
</tr>
</thead>
<tbody>
<%
total
=
0
%>
<%
if
!
@cart_product
.
nil?
%>
<%
@cart_product
.
each
do
|
id
,
quantity
|
%>
<%
product
=
Product
.
find_by_id
(
id
)
%>
<%
if
!
product
.
nil?
%>
<%
total
=
total
+
product
.
price
*
quantity
.
to_i
%>
<tr>
<td>
<%=
link_to
truncate
(
product
.
name
,
length
:
20
),
"/products/
#{
product
.
id
}
"
%>
</td>
<form
action=
"/cart_product/update"
>
<td><input
name=
"new_quantity"
min=
"1"
max=
"100"
type=
"number"
class=
"span1"
value=
<%=
quantity
%>
/
></td>
<td><input
type=
"submit"
value=
"Update"
/><input
type=
"hidden"
name=
"id"
value=
"
<%=
product
.
id
%>
"
/></td>
</form>
<td><form
action=
"/cart_product/remove"
><input
type=
"submit"
value=
"Delete"
/><input
type=
"hidden"
name=
"id"
value=
"
<%=
product
.
id
%>
"
/></form></td>
<td>
<%=
(
product
.
price
/
100
.
to_f
).
to_s
+
"$"
%>
</td>
<td>
<%=
((
product
.
price
*
quantity
)
/
100
.
to_f
).
to_s
+
"$"
%>
</td>
</tr>
<%
end
%>
<%
end
%>
<%
else
%>
<h2>
Shopping Cart Empty
</h2>
<%
end
%>
<%
else
%>
<h2>
Shopping Cart Empty
</h2>
<%
end
%>
</tbody>
</table>
</ul>
</div>
</tbody>
</table>
</ul>
</div>
<dl
class=
"dl-horizontal pull-right"
>
<dt>
Sub-total:
</dt>
<dd>
<%=
(
total
/
100
.
to_f
).
to_s
+
"$"
%>
</dd>
<dt>
Total:
</dt>
<dd>
<%=
(
total
/
100
.
to_f
).
to_s
+
"$"
%>
</dd>
</dl>
<div
class=
"clearfix"
></div>
<dl
class=
"dl-horizontal pull-right"
>
<dt>
Sub-total:
</dt>
<dd>
<%=
(
total
/
100
.
to_f
).
to_s
+
"$"
%>
</dd>
<dt>
Total:
</dt>
<dd>
<%=
(
total
/
100
.
to_f
).
to_s
+
"$"
%>
</dd>
</dl>
<div
class=
"clearfix"
></div>
<%=
link_to
"Check Out"
,
"/cart"
,
class:
"btn btn-success pull-right"
%>
<%=
link_to
"Delete All"
,
'/cart_product/clear'
,
class:
"btn btn-lg btn-danger pull-right"
%>
<%
else
%>
<h1>
Your Cart is Empty!
</h1>
<%
end
%>
<%=
link_to
"Continue Shopping "
,
root_path
,
class:
"btn btn-primary"
%>
<%=
link_to
"Check Out"
,
"/cart"
,
class:
"btn btn-success pull-right"
%>
<%=
link_to
"Delete All"
,
'/cart_product/clear'
,
class:
"btn btn-lg btn-danger pull-right"
%>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div>
\ No newline at end of file
app/views/carts/info.html.erb
View file @
f67de3d0
...
...
@@ -32,25 +32,36 @@
<dd>
<%=
number_to_currency
(
@total
/
100
.
to_f
,
:unit
=>
'$'
)
%>
</dd>
</dl></div><br><br><br>
<div
align=
"center"
>
<%
flash
.
each
do
|
message_type
,
message
|
%>
<div
class=
"alert alert-
<%=
message_type
%>
"
>
<%=
message
%>
</div>
<%
if
@new_cart
.
errors
.
any?
%>
<div
id=
"error_explanation"
>
<div
class=
"alert alert-danger"
>
The form contains
<%=
pluralize
(
@new_cart
.
errors
.
count
,
"error"
)
%>
.
</div>
<ul>
<%
@new_cart
.
errors
.
full_messages
.
each
do
|
msg
|
%>
<li>
<%=
msg
%>
</li>
<%
end
%>
</ul>
</div>
<%
end
%>
<div>
<%=
form_for
(
@new_cart
,
url:
create_cart_path
)
do
|
f
|
%>
<%=
f
.
label
:email
%>
<%
if
user_signed_in?
%>
<%
email
=
@current_user
.
email
%>
<%
end
%>
<%=
form_for
(
:session
,
url:
create_cart_path
)
do
|
f
|
%>
<%=
f
.
label
:email
%>
<%=
f
.
email_field
:email
,
value:
email
,
class:
'form-control'
%>
<%=
f
.
label
:fullname
%>
<%=
f
.
text_field
:fullname
,
class:
'form-control'
%>
<%=
f
.
label
:address
%>
<%=
f
.
text_field
:address
,
class:
'form-control'
%>
<%=
f
.
label
:phone
%>
<%=
f
.
text_field
:phone
,
class:
'form-control'
%>
</br>
<%=
f
.
submit
"Submit"
,
class:
"btn btn-primary"
%>
<%
else
%>
<%=
f
.
email_field
:email
,
class:
'form-control'
%>
<%
end
%>
<%=
f
.
label
:fullname
%>
<%=
f
.
text_field
:fullname
,
class:
'form-control'
%>
<%=
f
.
label
:address
%>
<%=
f
.
text_field
:address
,
class:
'form-control'
%>
<%=
f
.
label
:phone
%>
<%=
f
.
text_field
:phone
,
class:
'form-control'
%>
</br>
<%=
f
.
submit
"Submit"
,
class:
"btn btn-primary"
%>
<%
end
%>
</div>
<%
else
%>
<h1>
Your Cart is Empty. Thank you for order
</h1>
...
...
app/views/products/index.html.erb
View file @
f67de3d0
<%=
render
'categories/view'
%>
<div
class=
"span9"
>
<div
class=
"hero-unit"
>
<h1
class=
""
>
Ventura Trainee
</h1>
<p
class=
""
>
Ruby on Rails
</p>
...
...
config/routes.rb
View file @
f67de3d0
VenShop
::
Application
.
routes
.
draw
do
namespace
:admins
do
get
'users/show'
end
namespace
:admins
do
get
'users/new'
end
...
...
db/migrate/20150721070908_create_products.rb
View file @
f67de3d0
...
...
@@ -4,7 +4,7 @@ class CreateProducts < ActiveRecord::Migration
t
.
string
:name
t
.
string
:image
t
.
integer
:price
t
.
string
:description
,
limit:
65535
t
.
string
:description
t
.
integer
:category_id
t
.
timestamps
null:
false
...
...
db/migrate/20150
724080644
_create_carts.rb
→
db/migrate/20150
804021527
_create_carts.rb
View file @
f67de3d0
class
CreateCarts
<
ActiveRecord
::
Migration
def
change
create_table
:carts
do
|
t
|
t
.
integer
:
U
ser_id
t
.
integer
:
u
ser_id
t
.
integer
:total_price
t
.
string
:status
t
.
string
:fullname
t
.
integer
:phone
t
.
string
:phone
t
.
string
:address
t
.
string
:email
...
...
db/migrate/20150804082910_change_limit.rb
0 → 100644
View file @
f67de3d0
class
ChangeLimit
<
ActiveRecord
::
Migration
def
change
change_column
:products
,
:price
,
:integer
,
:limit
=>
5
change_column
:products
,
:description
,
:string
,
:limit
=>
20000
change_column
:cart_products
,
:cart_id
,
:integer
,
:limit
=>
5
change_column
:cart_products
,
:product_id
,
:integer
,
:limit
=>
5
change_column
:cart_products
,
:quantity
,
:integer
,
:limit
=>
5
change_column
:cart_products
,
:price
,
:integer
,
:limit
=>
5
change_column
:carts
,
:user_id
,
:integer
,
:limit
=>
5
change_column
:carts
,
:total_price
,
:integer
,
:limit
=>
5
change_column
:products
,
:name
,
:string
,
:limit
=>
1000
change_column
:products
,
:category_id
,
:integer
,
:limit
=>
5
end
end
db/schema.rb
View file @
f67de3d0
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
20150
730075349
)
do
ActiveRecord
::
Schema
.
define
(
version:
20150
804082910
)
do
create_table
"admins"
,
force: :cascade
do
|
t
|
t
.
string
"email"
,
limit:
255
,
default:
""
,
null:
false
...
...
@@ -32,20 +32,20 @@ ActiveRecord::Schema.define(version: 20150730075349) do
add_index
"admins"
,
[
"reset_password_token"
],
name:
"index_admins_on_reset_password_token"
,
unique:
true
,
using: :btree
create_table
"cart_products"
,
force: :cascade
do
|
t
|
t
.
integer
"cart_id"
,
limit:
4
t
.
integer
"product_id"
,
limit:
4
t
.
integer
"quantity"
,
limit:
4
t
.
integer
"price"
,
limit:
4
t
.
integer
"cart_id"
,
limit:
8
t
.
integer
"product_id"
,
limit:
8
t
.
integer
"quantity"
,
limit:
8
t
.
integer
"price"
,
limit:
8
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
create_table
"carts"
,
force: :cascade
do
|
t
|
t
.
integer
"
User_id"
,
limit:
4
t
.
integer
"total_price"
,
limit:
4
t
.
integer
"
user_id"
,
limit:
8
t
.
integer
"total_price"
,
limit:
8
t
.
string
"status"
,
limit:
255
t
.
string
"fullname"
,
limit:
255
t
.
integer
"phone"
,
limit:
4
t
.
string
"phone"
,
limit:
255
t
.
string
"address"
,
limit:
255
t
.
string
"email"
,
limit:
255
t
.
datetime
"created_at"
,
null:
false
...
...
@@ -59,11 +59,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do
end
create_table
"products"
,
force: :cascade
do
|
t
|
t
.
string
"name"
,
limit:
255
t
.
string
"name"
,
limit:
1000
t
.
string
"image"
,
limit:
255
t
.
integer
"price"
,
limit:
4
t
.
integer
"price"
,
limit:
8
t
.
string
"description"
,
limit:
20000
t
.
integer
"category_id"
,
limit:
4
t
.
integer
"category_id"
,
limit:
8
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
...
...
lib/import_amazon.rb
View file @
f67de3d0
...
...
@@ -3,17 +3,17 @@ class ImportAmazon
def
initialize
@request
=
Vacuum
.
new
(
'US'
)
@request
.
configure
(
aws_access_key_id:
"AKIAJ77C4CTZOP7TUVWQ"
,
aws_secret_access_key:
"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"
,
associate_tag:
"zigexn6400-22"
aws_access_key_id:
'AKIAIAJR65JO6EIPQWTA'
,
aws_secret_access_key:
'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'
,
associate_tag:
'microv'
)
@request
.
associate_tag
=
'tag'
@request
.
associate_tag
=
'tag'
end
def
import_product
def
import_product
(
1
..
100
).
each
do
|
page
|
get_response
(
page
).
each
do
|
item
|
begin
get_response
(
page
).
each
do
|
item
|
begin
category
=
Category
.
find_or_create_by
(
name:
item
[
"ItemAttributes"
][
"ProductGroup"
])
products
=
Product
.
find_or_create_by
(
name:
item
[
"ItemAttributes"
].
to_h
[
"Title"
])
do
|
products
|
products
.
image
=
item
[
"LargeImage"
][
"URL"
]
...
...
@@ -41,7 +41,7 @@ class ImportAmazon
'ItemPage'
=>
page
,
}
)
response
.
to_h
[
"ItemSearchResponse"
].
to_h
[
"Items"
].
to_h
[
"Item"
]
response
.
to_h
[
"ItemSearchResponse"
].
to_h
[
"Items"
].
to_h
[
"Item"
]
end
end
\ No newline at end of file
test/controllers/admins/users_controller_test.rb
View file @
f67de3d0
require
'test_helper'
class
Admins
::
UsersControllerTest
<
ActionController
::
TestCase
test
"should get
sho
w"
do
get
:
sho
w
test
"should get
ne
w"
do
get
:
ne
w
assert_response
:success
end
...
...
test/fixtures/carts.yml
View file @
f67de3d0
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one
:
U
ser_id
:
1
u
ser_id
:
1
total_price
:
1
status
:
MyString
fullname
:
MyString
phone
:
1
phone
:
MyString
address
:
MyString
email
:
MyString
two
:
U
ser_id
:
1
u
ser_id
:
1
total_price
:
1
status
:
MyString
fullname
:
MyString
phone
:
1
phone
:
MyString
address
:
MyString
email
:
MyString
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