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
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
43 additions
and
30 deletions
+43
-30
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
+12
-12
No files found.
app/controllers/home_controller.rb
View file @
ce5b42b0
...
@@ -6,17 +6,18 @@ class HomeController < ApplicationController
...
@@ -6,17 +6,18 @@ class HomeController < ApplicationController
before_action
:get_recommend
before_action
:get_recommend
def
index
def
index
params
[
:per_page
]
||=
5
@products
=
Product
.
order
(
release_date: :desc
).
page
(
params
[
:page
])
params
[
:page
]
||=
1
@products
=
Product
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
])
end
end
def
search
def
search
add_breadcrumb
"Search"
add_breadcrumb
"Search"
,
:search_path
add_breadcrumb
"Search"
,
:search_path
@products
=
Product
.
search
(
params
)
@products
=
Product
.
search
(
params
)
end
end
def
cart
def
cart
add_breadcrumb
"Cart"
render
layout:
"cart"
end
end
...
...
app/controllers/orders_controller.rb
View file @
ce5b42b0
...
@@ -2,12 +2,9 @@ class OrdersController < ApplicationController
...
@@ -2,12 +2,9 @@ class OrdersController < ApplicationController
before_action
:authenticate_user!
before_action
:authenticate_user!
layout
"cart"
layout
"cart"
def
edit
end
def
update
def
update
current_order
.
update_attributes
(
update_order_params
)
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
end
def
add_to_cart
def
add_to_cart
...
...
app/models/line_item.rb
View file @
ce5b42b0
...
@@ -3,9 +3,12 @@ class LineItem < ActiveRecord::Base
...
@@ -3,9 +3,12 @@ class LineItem < ActiveRecord::Base
belongs_to
:product
belongs_to
:product
validate
:check_product_stock
validate
:check_product_stock
after_save
:trigger_recalculate
after_save
:trigger_recalculate
before_destroy
:trigger_calculate_order
before_destroy
:trigger_calculate_order
private
def
trigger_recalculate
def
trigger_recalculate
order
.
recalculate
order
.
recalculate
change_square
=
quantity
-
quantity_was
change_square
=
quantity
-
quantity_was
...
...
app/models/order.rb
View file @
ce5b42b0
...
@@ -7,12 +7,12 @@ class Order < ActiveRecord::Base
...
@@ -7,12 +7,12 @@ class Order < ActiveRecord::Base
accepts_nested_attributes_for
:line_items
accepts_nested_attributes_for
:line_items
def
recalculate
def
recalculate
puts
"===================================
#{
line_items
.
inject
(
0
)
{
|
sum
,
item
|
sum
+
item
.
quantity
*
item
.
price
}
}"
count
=
line_items
.
sum
(
:quantity
)
count
=
line_items
.
sum
(
:quantity
)
total_price
=
0
self
.
update_columns
(
line_items
.
each
do
|
item
|
item_count:
count
,
total_price
+=
item
.
quantity
*
item
.
price
item_total:
line_items
.
inject
(
0
)
{
|
sum
,
item
|
sum
+
item
.
quantity
*
item
.
price
}
end
)
self
.
update_columns
(
item_count:
count
,
item_total:
total_price
)
end
end
def
checkout
def
checkout
...
...
app/models/product.rb
View file @
ce5b42b0
...
@@ -14,10 +14,9 @@ class Product < ActiveRecord::Base
...
@@ -14,10 +14,9 @@ class Product < ActiveRecord::Base
scope
:recommend
,
->
{
where
(
recommend:
true
)
}
scope
:recommend
,
->
{
where
(
recommend:
true
)
}
def
self
.
search
(
params
)
def
self
.
search
(
params
)
Product
.
order
(
"release_date DESC"
).
page
(
params
[
:page
])
Product
.
order
(
release_date: :desc
).
page
(
params
[
:page
])
end
end
def
can_buy?
(
quantity
)
def
can_buy?
(
quantity
)
stock
>=
quantity
.
to_i
stock
>=
quantity
.
to_i
end
end
...
...
app/services/cart_service.rb
View file @
ce5b42b0
...
@@ -4,9 +4,8 @@ class CartService
...
@@ -4,9 +4,8 @@ class CartService
params
[
:quantity
]
||=
1
params
[
:quantity
]
||=
1
product
=
Product
.
find
(
params
[
:product_id
])
product
=
Product
.
find
(
params
[
:product_id
])
if
product
.
can_buy?
(
params
[
:quantity
])
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
.
quantity
+=
params
[
:quantity
].
to_i
line_item
.
price
=
product
.
price
if
line_item
.
save
if
line_item
.
save
return
true
,
"Add to cart successfully"
return
true
,
"Add to cart successfully"
else
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
...
@@ -18,7 +18,7 @@ nav.navbar.navbar-inverse
ul
.nav.navbar-nav.navbar-right
ul
.nav.navbar-nav.navbar-right
-
if
current_user
.
present?
-
if
current_user
.
present?
li
li
=
link_to
"Cart (
#{
current_order
.
item_count
}
)"
,
edit_order_path
(
current_order
)
=
link_to
"Cart (
#{
current_order
.
item_count
}
)"
,
cart_path
li
li
=
current_user
.
decorate
.
edit_profile_link
=
current_user
.
decorate
.
edit_profile_link
li
li
...
...
config/routes.rb
View file @
ce5b42b0
...
@@ -6,10 +6,11 @@ Rails.application.routes.draw do
...
@@ -6,10 +6,11 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root"
# You can have the root of your site routed with "root"
root
'home#index'
root
'home#index'
get
'/search'
=>
"home#search"
,
as: :search
get
'/search'
=>
"home#search"
,
as: :search
get
'/cart'
=>
"home#cart"
,
as: :cart
resources
:categories
,
:only
=>
[
:show
]
resources
:categories
,
:only
=>
[
:show
]
resources
:products
,
:only
=>
[
:new
,
:create
,
:show
]
resources
:products
,
:only
=>
[
:new
,
:create
,
:show
]
resources
:orders
,
:only
=>
[
:
edit
,
:
update
,
:show
]
do
resources
:orders
,
:only
=>
[
:update
,
:show
]
do
collection
do
collection
do
get
"checkout"
=>
"orders#checkout"
,
as: :checkout
get
"checkout"
=>
"orders#checkout"
,
as: :checkout
post
"add_to_cart"
=>
"orders#add_to_cart"
,
as: :add_to_cart
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 @@
...
@@ -11,7 +11,7 @@
#
#
# It's strongly recommended that you check this file into your version control system.
# 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
|
create_table
"categories"
,
force: :cascade
do
|
t
|
t
.
string
"name"
,
limit:
255
t
.
string
"name"
,
limit:
255
...
@@ -20,36 +20,36 @@ ActiveRecord::Schema.define(version: 20150715093529) do
...
@@ -20,36 +20,36 @@ ActiveRecord::Schema.define(version: 20150715093529) do
create_table
"line_items"
,
force: :cascade
do
|
t
|
create_table
"line_items"
,
force: :cascade
do
|
t
|
t
.
integer
"product_id"
,
limit:
4
t
.
integer
"product_id"
,
limit:
4
t
.
integer
"order_id"
,
limit:
4
t
.
integer
"order_id"
,
limit:
4
t
.
integer
"quantity"
,
limit:
4
,
default:
0
t
.
integer
"quantity"
,
limit:
4
,
default:
0
t
.
float
"price"
,
limit:
24
t
.
decimal
"price"
,
precision:
12
,
scale:
2
end
end
create_table
"orders"
,
force: :cascade
do
|
t
|
create_table
"orders"
,
force: :cascade
do
|
t
|
t
.
string
"pid"
,
limit:
255
t
.
string
"pid"
,
limit:
255
t
.
integer
"item_count"
,
limit:
4
,
default:
0
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
.
integer
"state"
,
limit:
4
,
default:
1
t
.
datetime
"completed_at"
t
.
datetime
"completed_at"
t
.
integer
"user_id"
,
limit:
4
t
.
integer
"user_id"
,
limit:
4
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
"products"
,
force: :cascade
do
|
t
|
create_table
"products"
,
force: :cascade
do
|
t
|
t
.
string
"pid"
,
limit:
255
t
.
string
"pid"
,
limit:
255
t
.
string
"title"
,
limit:
1024
,
default:
""
,
null:
false
t
.
string
"title"
,
limit:
1024
,
default:
""
,
null:
false
t
.
string
"author"
,
limit:
255
t
.
string
"author"
,
limit:
255
t
.
string
"publisher"
,
limit:
255
t
.
string
"publisher"
,
limit:
255
t
.
string
"studio"
,
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
.
string
"currency"
,
limit:
255
t
.
integer
"category_id"
,
limit:
4
t
.
integer
"category_id"
,
limit:
4
t
.
integer
"user_id"
,
limit:
4
t
.
integer
"user_id"
,
limit:
4
t
.
string
"image_uid"
,
limit:
255
t
.
string
"image_uid"
,
limit:
255
t
.
datetime
"release_date"
t
.
datetime
"release_date"
t
.
datetime
"public_date"
t
.
datetime
"public_date"
t
.
boolean
"recommend"
,
limit:
1
,
default:
false
t
.
boolean
"recommend"
,
limit:
1
,
default:
false
t
.
integer
"stock"
,
limit:
4
,
default:
10
t
.
integer
"stock"
,
limit:
4
,
default:
10
end
end
create_table
"users"
,
force: :cascade
do
|
t
|
create_table
"users"
,
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