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
382224cd
Commit
382224cd
authored
Nov 11, 2013
by
vulehuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move card update function to new action
parent
76c37c83
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
54 deletions
+61
-54
venshop_app/app/controllers/cards_controller.rb
+59
-53
venshop_app/app/views/cards/_item_list.html.erb
+1
-1
venshop_app/config/routes.rb
+1
-0
No files found.
venshop_app/app/controllers/cards_controller.rb
View file @
382224cd
...
...
@@ -8,64 +8,34 @@ class CardsController < ApplicationController
end
unless
params
[
:product_id
].
blank?
if
Product
.
where
(
id:
params
[
:product_id
]).
exists?
if
!
params
[
:card_action
].
blank?
&&
params
[
:card_action
]
==
'remove'
elsif
!
params
[
:card_action
].
blank?
&&
params
[
:card_action
]
==
'update'
# Nothing to update
if
@card_infos
.
empty?
flash
[
:error
]
=
"Invalid request"
redirect_to
cards_path
and
return
else
quantity
=
params
[
:quantity
]
# Invalid request
if
quantity
.
blank?
||
quantity
.
to_i
<=
0
flash
[
:error
]
=
"Invalid quantity"
redirect_to
cards_path
and
return
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
and
return
# 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
=
{}
@card_infos
=
{
card_items:
card_items
,
customer_info:
customer_info
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
redirect_to
cards_path
and
return
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
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
# if a product not exist in card
unless
found
card_items
.
push
({
product_id:
params
[
:product_id
],
quantity:
1
})
customer_info
=
{}
customer_info
=
@card_infos
[
:customer_info
]
@card_infos
=
{
card_items:
card_items
,
customer_info:
customer_info
}
session
[
:SHOPPING_CARD_SESSION_NAME
]
=
@card_infos
redirect_to
cards_path
and
return
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
# if a product not exist in card
unless
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
end
redirect_to
cards_path
and
return
end
redirect_to
cards_path
and
return
end
else
flash
[
:error
]
=
"Invalid product"
...
...
@@ -74,7 +44,7 @@ class CardsController < ApplicationController
end
end
#
if
remove a product from card
# remove a product from card
def
remove
@card_infos
=
{}
unless
session
[
:SHOPPING_CARD_SESSION_NAME
].
blank?
...
...
@@ -102,6 +72,42 @@ class CardsController < ApplicationController
end
redirect_to
cards_path
and
return
end
# update quantity
def
update
@card_infos
=
{}
unless
session
[
:SHOPPING_CARD_SESSION_NAME
].
blank?
@card_infos
=
session
[
:SHOPPING_CARD_SESSION_NAME
]
end
unless
params
[
:product_id
].
blank?
# Nothing to update
if
@card_infos
.
empty?
flash
[
:error
]
=
"Invalid request"
redirect_to
cards_path
and
return
else
quantity
=
params
[
:quantity
]
# Invalid request
if
quantity
.
blank?
||
quantity
.
to_i
<=
0
flash
[
:error
]
=
"Invalid quantity"
redirect_to
cards_path
and
return
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
and
return
end
end
end
redirect_to
cards_path
and
return
end
def
checkout
add_breadcrumb
"Checkout"
,
url_for
(
action:
'checkout'
)
@card_infos
=
{}
...
...
venshop_app/app/views/cards/_item_list.html.erb
View file @
382224cd
...
...
@@ -38,7 +38,7 @@
<%
if
can_edit
%>
<td>
<div
class=
"text-center"
>
<%=
link_to
(
cards_path
(
product_id:
card_item
[
:product_id
],
card_action:
'update'
),
class:
'btn btn-info btn-update-card-quantity'
)
do
%>
<%=
link_to
(
url_for
(
controller:
'cards'
,
action:
'update'
,
product_id:
card_item
[
:product_id
]
),
class:
'btn btn-info btn-update-card-quantity'
)
do
%>
<span
class=
"glyphicon glyphicon-plus"
></span>
Update quantity
<%
end
%>
<%=
link_to
(
url_for
(
controller:
'cards'
,
action:
'remove'
,
product_id:
card_item
[
:product_id
]),
class:
'btn btn-danger'
,
dada:
'{:confirm=>"Do you want to remove this product?"}'
)
do
%>
...
...
venshop_app/config/routes.rb
View file @
382224cd
...
...
@@ -11,6 +11,7 @@ VenshopApp::Application.routes.draw do
post
"cards/confirm_checkout"
get
"cards/thankyou"
get
"cards/remove"
get
"cards/update"
resources
:users
resources
:sessions
,
only:
[
:new
,
:create
,
:destroy
]
resources
:products
...
...
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