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
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
huanvl
venshop
Commits
cad828e0
Commit
cad828e0
authored
Oct 31, 2013
by
vulehuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rspec: product - add product to card
parent
d17f3bf9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
51 deletions
+87
-51
venshop_app/app/controllers/cards_controller.rb
+54
-51
venshop_app/spec/requests/product_pages_spec.rb
+33
-0
No files found.
venshop_app/app/controllers/cards_controller.rb
View file @
cad828e0
...
...
@@ -7,78 +7,81 @@ class CardsController < ApplicationController
@card_infos
=
session
[
:SHOPPING_CARD_SESSION_NAME
]
end
if
!
params
[
:product_id
].
nil?
# if remove a product from card
if
!
params
[
:card_action
].
nil?
&&
params
[
:card_action
]
==
'remove'
# Nothing to delete
if
@card_infos
.
empty?
redirect_to
cards_path
else
card_items
=
@card_infos
[
:card_items
]
card_items
.
each
do
|
card_item
|
# if a product exist in card
if
card_item
[
:product_id
]
==
params
[
:product_id
]
card_items
.
delete
(
card_item
)
@card_infos
=
{
card_items:
card_items
,
customer_info:
@card_infos
[
:customer_info
]
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
redirect_to
cards_path
end
end
end
elsif
!
params
[
:card_action
].
nil?
&&
params
[
:card_action
]
==
'update'
# Nothing to update
if
@card_infos
.
empty?
redirect_to
cards_path
else
quantity
=
params
[
:quantity
]
# Invalid request
if
quantity
.
nil?
||
quantity
.
to_i
<=
0
if
Product
.
exists?
(
params
[
:product_id
])
# if remove a product from card
if
!
params
[
:card_action
].
nil?
&&
params
[
:card_action
]
==
'remove'
# Nothing to delete
if
@card_infos
.
empty?
redirect_to
cards_path
else
card_items
=
@card_infos
[
:card_items
]
card_items
.
each
_with_index
do
|
card_item
,
key
|
card_items
.
each
do
|
card_item
|
# if a product exist in card
if
card_item
[
:product_id
]
==
params
[
:product_id
]
card_item
[
:quantity
]
=
quantity
.
to_i
card_items
[
key
]
=
card_item
card_items
.
delete
(
card_item
)
@card_infos
=
{
card_items:
card_items
,
customer_info:
@card_infos
[
:customer_info
]
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
redirect_to
cards_path
end
end
end
elsif
!
params
[
:card_action
].
nil?
&&
params
[
:card_action
]
==
'update'
# Nothing to update
if
@card_infos
.
empty?
redirect_to
cards_path
else
quantity
=
params
[
:quantity
]
# Invalid request
if
quantity
.
nil?
||
quantity
.
to_i
<=
0
redirect_to
cards_path
else
card_items
=
@card_infos
[
:card_items
]
card_items
.
each_with_index
do
|
card_item
,
key
|
# if a product exist in card
if
card_item
[
:product_id
]
==
params
[
:product_id
]
card_item
[
:quantity
]
=
quantity
.
to_i
card_items
[
key
]
=
card_item
@card_infos
=
{
card_items:
card_items
,
customer_info:
@card_infos
[
:customer_info
]
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
end
end
redirect_to
cards_path
end
end
end
else
# if add a product to card
# first time add to card
if
@card_infos
.
empty?
if
Product
.
exists?
(
params
[
:product_id
])
else
# if add a product to card
# first time add to card
if
@card_infos
.
empty?
product
=
Product
.
find
(
params
[
:product_id
])
card_items
=
Array
.
new
card_items
.
push
({
product_id:
params
[
:product_id
],
quantity:
1
})
customer_info
=
Hash
.
new
@card_infos
=
{
card_items:
card_items
,
customer_info:
customer_info
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
end
redirect_to
cards_path
else
card_items
=
@card_infos
[
:card_items
]
found
=
false
card_items
.
each
do
|
card_item
|
# if a product exist in card
if
card_item
[
:product_id
]
==
params
[
:product_id
]
found
=
true
break
redirect_to
cards_path
else
card_items
=
@card_infos
[
:card_items
]
found
=
false
card_items
.
each
do
|
card_item
|
# if a product exist in card
if
card_item
[
:product_id
]
==
params
[
:product_id
]
found
=
true
break
end
end
end
# if a product not exist in card
# if a product not exist in card
if
!
found
card_items
.
push
({
product_id:
params
[
:product_id
],
quantity:
1
})
customer_info
=
@card_infos
[
:customer_info
]
@card_infos
=
{
card_items:
card_items
,
customer_info:
customer_info
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
card_items
.
push
({
product_id:
params
[
:product_id
],
quantity:
1
})
customer_info
=
@card_infos
[
:customer_info
]
@card_infos
=
{
card_items:
card_items
,
customer_info:
customer_info
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
end
redirect_to
cards_path
end
redirect_to
cards_path
end
else
flash
[
:error
]
=
"Invalid product"
redirect_to
products_path
end
end
end
...
...
venshop_app/spec/requests/product_pages_spec.rb
View file @
cad828e0
...
...
@@ -101,4 +101,37 @@ describe "Products Page" do
end
end
end
describe
"when add product to card"
do
describe
"when product is invalid"
do
describe
"when product id is not a number"
do
before
{
visit
cards_path
(
product_id:
"abc"
)
}
it
{
should
have_selector
(
'.alert.alert-error'
,
text:
'Invalid'
)
}
end
describe
"when product id is not exist"
do
before
{
visit
cards_path
(
product_id:
0
)
}
it
{
should
have_selector
(
'.alert.alert-error'
,
text:
'Invalid'
)
}
end
end
describe
"when product is valid"
do
before
do
@product_category
=
FactoryGirl
.
create
(
:product_category
)
@user
=
FactoryGirl
.
create
(
:user
)
@product
=
FactoryGirl
.
create
(
:product
,
product_category_id:
@product_category
.
id
,
user_id:
@user
.
id
)
visit
product_path
(
@product
)
page
.
find
(
'.product-detail'
).
click_link
(
'Order'
)
end
it
{
should
have_selector
(
'table a'
,
text:
@product
.
name
)
}
it
{
should
have_selector
(
'.btn-update-card-quantity'
)
}
it
{
should
have_selector
(
'a.btn'
,
text:
'Remove'
)
}
end
end
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