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
1
Merge Requests
1
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
Truong Ba Dieu
VenShop
Commits
ce5b42b0
Commit
ce5b42b0
authored
Jul 17, 2015
by
Truong Ba Dieu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix pull request
parent
f2b33c51
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
35 additions
and
22 deletions
+35
-22
app/controllers/home_controller.rb
+4
-3
app/controllers/orders_controller.rb
+1
-4
app/models/line_item.rb
+3
-0
app/models/order.rb
+5
-5
app/models/product.rb
+1
-2
app/services/cart_service.rb
+1
-2
app/views/home/cart.html.slim
+0
-0
app/views/layouts/_navigation.html.slim
+1
-1
config/routes.rb
+2
-1
db/migrate/20150716093626_change_type_float_to_decimal.rb
+13
-0
db/schema.rb
+4
-4
No files found.
app/controllers/home_controller.rb
View file @
ce5b42b0
...
...
@@ -6,17 +6,18 @@ class HomeController < ApplicationController
before_action
:get_recommend
def
index
params
[
:per_page
]
||=
5
params
[
:page
]
||=
1
@products
=
Product
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
])
@products
=
Product
.
order
(
release_date: :desc
).
page
(
params
[
:page
])
end
def
search
add_breadcrumb
"Search"
add_breadcrumb
"Search"
,
:search_path
@products
=
Product
.
search
(
params
)
end
def
cart
add_breadcrumb
"Cart"
render
layout:
"cart"
end
...
...
app/controllers/orders_controller.rb
View file @
ce5b42b0
...
...
@@ -2,12 +2,9 @@ class OrdersController < ApplicationController
before_action
:authenticate_user!
layout
"cart"
def
edit
end
def
update
current_order
.
update_attributes
(
update_order_params
)
redirect_to
edit_order_path
(
current_order
),
notice:
"Update cart successfully"
redirect_to
cart_path
(
),
notice:
"Update cart successfully"
end
def
add_to_cart
...
...
app/models/line_item.rb
View file @
ce5b42b0
...
...
@@ -3,9 +3,12 @@ class LineItem < ActiveRecord::Base
belongs_to
:product
validate
:check_product_stock
after_save
:trigger_recalculate
before_destroy
:trigger_calculate_order
private
def
trigger_recalculate
order
.
recalculate
change_square
=
quantity
-
quantity_was
...
...
app/models/order.rb
View file @
ce5b42b0
...
...
@@ -7,12 +7,12 @@ class Order < ActiveRecord::Base
accepts_nested_attributes_for
:line_items
def
recalculate
puts
"===================================
#{
line_items
.
inject
(
0
)
{
|
sum
,
item
|
sum
+
item
.
quantity
*
item
.
price
}
}"
count
=
line_items
.
sum
(
:quantity
)
total_price
=
0
line_items
.
each
do
|
item
|
total_price
+=
item
.
quantity
*
item
.
price
end
self
.
update_columns
(
item_count:
count
,
item_total:
total_price
)
self
.
update_columns
(
item_count:
count
,
item_total:
line_items
.
inject
(
0
)
{
|
sum
,
item
|
sum
+
item
.
quantity
*
item
.
price
}
)
end
def
checkout
...
...
app/models/product.rb
View file @
ce5b42b0
...
...
@@ -14,10 +14,9 @@ class Product < ActiveRecord::Base
scope
:recommend
,
->
{
where
(
recommend:
true
)
}
def
self
.
search
(
params
)
Product
.
order
(
"release_date DESC"
).
page
(
params
[
:page
])
Product
.
order
(
release_date: :desc
).
page
(
params
[
:page
])
end
def
can_buy?
(
quantity
)
stock
>=
quantity
.
to_i
end
...
...
app/services/cart_service.rb
View file @
ce5b42b0
...
...
@@ -4,9 +4,8 @@ class CartService
params
[
:quantity
]
||=
1
product
=
Product
.
find
(
params
[
:product_id
])
if
product
.
can_buy?
(
params
[
:quantity
])
line_item
=
current_order
.
line_items
.
where
(
product_id:
product
.
id
).
first_or_create
line_item
=
current_order
.
line_items
.
where
(
product_id:
product
.
id
,
price:
product
.
price
).
first_or_create
line_item
.
quantity
+=
params
[
:quantity
].
to_i
line_item
.
price
=
product
.
price
if
line_item
.
save
return
true
,
"Add to cart successfully"
else
...
...
app/views/
orders/edi
t.html.slim
→
app/views/
home/car
t.html.slim
View file @
ce5b42b0
File moved
app/views/layouts/_navigation.html.slim
View file @
ce5b42b0
...
...
@@ -18,7 +18,7 @@ nav.navbar.navbar-inverse
ul
.nav.navbar-nav.navbar-right
-
if
current_user
.
present?
li
=
link_to
"Cart (
#{
current_order
.
item_count
}
)"
,
edit_order_path
(
current_order
)
=
link_to
"Cart (
#{
current_order
.
item_count
}
)"
,
cart_path
li
=
current_user
.
decorate
.
edit_profile_link
li
...
...
config/routes.rb
View file @
ce5b42b0
...
...
@@ -6,10 +6,11 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root"
root
'home#index'
get
'/search'
=>
"home#search"
,
as: :search
get
'/cart'
=>
"home#cart"
,
as: :cart
resources
:categories
,
:only
=>
[
:show
]
resources
:products
,
:only
=>
[
:new
,
:create
,
:show
]
resources
:orders
,
:only
=>
[
:
edit
,
:
update
,
:show
]
do
resources
:orders
,
:only
=>
[
:update
,
:show
]
do
collection
do
get
"checkout"
=>
"orders#checkout"
,
as: :checkout
post
"add_to_cart"
=>
"orders#add_to_cart"
,
as: :add_to_cart
...
...
db/migrate/20150716093626_change_type_float_to_decimal.rb
0 → 100644
View file @
ce5b42b0
class
ChangeTypeFloatToDecimal
<
ActiveRecord
::
Migration
def
up
change_column
:orders
,
:item_total
,
:decimal
,
:precision
=>
12
,
:scale
=>
2
change_column
:line_items
,
:price
,
:decimal
,
:precision
=>
12
,
:scale
=>
2
change_column
:products
,
:price
,
:decimal
,
:precision
=>
12
,
:scale
=>
2
end
def
down
change_column
:orders
,
:item_total
,
:float
change_column
:line_items
,
:item_total
,
:float
change_column
:products
,
:item_total
,
:float
end
end
db/schema.rb
View file @
ce5b42b0
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2015071
5093529
)
do
ActiveRecord
::
Schema
.
define
(
version:
2015071
6093626
)
do
create_table
"categories"
,
force: :cascade
do
|
t
|
t
.
string
"name"
,
limit:
255
...
...
@@ -21,13 +21,13 @@ ActiveRecord::Schema.define(version: 20150715093529) do
t
.
integer
"product_id"
,
limit:
4
t
.
integer
"order_id"
,
limit:
4
t
.
integer
"quantity"
,
limit:
4
,
default:
0
t
.
float
"price"
,
limit:
24
t
.
decimal
"price"
,
precision:
12
,
scale:
2
end
create_table
"orders"
,
force: :cascade
do
|
t
|
t
.
string
"pid"
,
limit:
255
t
.
integer
"item_count"
,
limit:
4
,
default:
0
t
.
float
"item_total"
,
limit:
24
,
default:
0.0
t
.
decimal
"item_total"
,
precision:
12
,
scale:
2
,
default:
0.0
t
.
integer
"state"
,
limit:
4
,
default:
1
t
.
datetime
"completed_at"
t
.
integer
"user_id"
,
limit:
4
...
...
@@ -41,7 +41,7 @@ ActiveRecord::Schema.define(version: 20150715093529) do
t
.
string
"author"
,
limit:
255
t
.
string
"publisher"
,
limit:
255
t
.
string
"studio"
,
limit:
255
t
.
float
"price"
,
limit:
24
t
.
decimal
"price"
,
precision:
12
,
scale:
2
t
.
string
"currency"
,
limit:
255
t
.
integer
"category_id"
,
limit:
4
t
.
integer
"user_id"
,
limit:
4
...
...
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