Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
dhp-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
Hoang Phuc Do
dhp-venshop
Commits
a01f17cd
Commit
a01f17cd
authored
Jun 14, 2017
by
Hoang Phuc Do
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix merge request #2
parent
764def09
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
55 additions
and
29 deletions
+55
-29
app/controllers/application_controller.rb
+9
-0
app/controllers/products_controller.rb
+31
-17
app/helpers/products_helper.rb
+3
-2
app/views/products/_product.html.erb
+1
-3
app/views/products/_recommended.html.erb
+2
-6
app/views/products/edit.html.erb
+1
-0
app/views/shared/_flash_messages.html.erb
+6
-0
app/views/static_pages/index.html.erb
+1
-0
db/migrate/20170608073720_add_user_ref_to_products.rb
+1
-1
No files found.
app/controllers/application_controller.rb
View file @
a01f17cd
class
ApplicationController
<
ActionController
::
Base
class
ApplicationController
<
ActionController
::
Base
protect_from_forgery
with: :exception
protect_from_forgery
with: :exception
# rescue_from ActiveRecord::RecordNotFound, :with => :render_404
def
authenticate_active_admin_user!
def
authenticate_active_admin_user!
authenticate_user!
authenticate_user!
...
@@ -8,4 +9,12 @@ class ApplicationController < ActionController::Base
...
@@ -8,4 +9,12 @@ class ApplicationController < ActionController::Base
redirect_to
root_path
redirect_to
root_path
end
end
end
end
def
render_404
respond_to
do
|
format
|
format
.
html
{
render
file:
"
#{
Rails
.
root
}
/public/404"
,
layout:
false
,
status: :not_found
}
format
.
xml
{
head
:not_found
}
format
.
any
{
head
:not_found
}
end
end
end
end
app/controllers/products_controller.rb
View file @
a01f17cd
class
ProductsController
<
ApplicationController
class
ProductsController
<
ApplicationController
before_action
:authenticate_user!
,
only:
[
:new
,
:edit
,
:create
,
:update
,
:destroy
]
before_action
:authenticate_user!
,
only:
[
:new
,
:edit
,
:create
,
:update
,
:destroy
]
before_action
:correct_user
,
only:
[
:edit
,
:destroy
]
before_action
:set_product
,
only:
[
:show
]
before_action
:user_can_edit_product
,
only:
[
:edit
,
:update
,
:destroy
]
# GET /products/new
def
new
def
new
@product
=
current_user
.
products
.
build
@product
=
Product
.
new
end
end
# POST /products
def
create
def
create
@product
=
current_user
.
products
.
build
(
product_params
)
@product
=
Product
.
new
(
product_params
.
merge
(
user_id:
current_user
.
id
)
)
if
@product
.
save
if
@product
.
save
redirect_to
root_url
redirect_to
root_url
,
flash:
{
success:
"Product
#{
@product
.
title
}
is sucessfully created"
}
else
else
render
'new'
render
'new'
end
end
end
end
def
edit
# PATCH/PUT /products/1
@product
=
Product
.
find
(
params
[
:id
])
end
def
show
@product
=
Product
.
find
(
params
[
:id
])
end
def
update
def
update
@product
=
Product
.
find
(
params
[
:id
])
if
@product
.
update
(
product_params
)
if
@product
.
update
(
product_params
)
redirect_to
root_url
redirect_to
root_url
,
flash:
{
success:
"Product
#{
@product
.
title
}
is sucessfully updated"
}
else
else
render
'edit'
render
'edit'
end
end
end
end
# DELETE /products/1
def
destroy
if
@product
.
destroy
flash
[
:success
]
=
"Product
#{
@product
.
title
}
deleted"
else
flash
[
:danger
]
=
"Product
#{
@product
.
title
}
can't be deleted"
end
redirect_to
root_url
end
private
private
# Use callbacks to share common setup or constraints between actions.
def
set_product
@product
=
Product
.
find
(
params
[
:id
])
end
# Never trust parameters from the scary internet, only allow the white list through.
def
product_params
def
product_params
params
.
require
(
:product
).
permit
(
:title
,
:sku
,
:price
,
:description
,
params
.
require
(
:product
).
permit
(
:title
,
:sku
,
:price
,
:description
,
:category_id
,
:image_url
)
:category_id
,
:image_url
)
end
end
def
correct_user
# Is current user own current editing product?
product
=
current_user
.
products
.
find_by
(
id:
params
[
:id
])
def
user_can_edit_product
redirect_to
root_url
if
product
.
nil?
@product
=
current_user
.
products
.
find_by
(
id:
params
[
:id
])
redirect_to
root_url
,
flash:
{
danger:
'You do not have permission to edit this product'
}
if
@product
.
blank?
end
end
end
end
\ No newline at end of file
app/helpers/products_helper.rb
View file @
a01f17cd
module
ProductsHelper
module
ProductsHelper
def
get_product_thumbnail
(
product
,
thumbnail_width
,
thumbnail_height
)
def
get_product_thumbnail
(
product
,
thumbnail_width
,
thumbnail_height
)
# product.image_url always returns PictureUploader object
default_img_path
=
"product/placeholder_
#{
thumbnail_width
}
x
#{
thumbnail_height
}
"
default_img_path
=
"product/placeholder_
#{
thumbnail_width
}
x
#{
thumbnail_height
}
"
product
.
image_url
.
present?
?
product
.
image_url
:
default_img_path
# product.image_url always returns PictureUploader object
product
.
image_url?
?
product
.
image_url
:
default_img_path
end
end
end
end
\ No newline at end of file
app/views/products/_product.html.erb
View file @
a01f17cd
<li
class=
"product-
<%=
product
.
id
%>
"
>
<li
class=
"product-
<%=
product
.
id
%>
"
>
<div
class=
"product product-list"
>
<div
class=
"product product-list"
>
<figure
class=
"product-image-area"
>
<figure
class=
"product-image-area"
>
<a
href=
"
<%=
product_url
(
product
)
%>
"
>
<%=
link_to
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
)),
product_path
(
product
)
%>
<%=
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
))
%>
</a>
</figure>
</figure>
<div
class=
"product-details-area"
>
<div
class=
"product-details-area"
>
<h2
class=
"product-name"
>
<h2
class=
"product-name"
>
...
...
app/views/products/_recommended.html.erb
View file @
a01f17cd
<div
class=
"products-grid columns3"
>
<%
@recommended_products
.
each
do
|
product
|
%>
<%
@recommended_products
.
each
do
|
product
|
%>
<li
class=
"product-
<%=
product
.
id
%>
"
>
<li
class=
"product-
<%=
product
.
id
%>
"
>
<div
class=
"product"
>
<div
class=
"product"
>
<figure
class=
"product-image-area"
>
<figure
class=
"product-image-area"
>
<a
href=
"
<%=
product_url
(
product
)
%>
"
>
<%=
link_to
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
)),
product_path
(
product
)
%>
<%=
image_tag
(
get_product_thumbnail
(
product
,
170
,
204
))
%>
</a>
</figure>
</figure>
<div
class=
"product-details-area"
>
<div
class=
"product-details-area"
>
<h2
class=
"product-name"
>
<h2
class=
"product-name"
>
...
@@ -18,4 +15,3 @@
...
@@ -18,4 +15,3 @@
</div>
</div>
</li>
</li>
<%
end
%>
<%
end
%>
\ No newline at end of file
</div>
\ No newline at end of file
app/views/products/edit.html.erb
View file @
a01f17cd
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
<div
class=
"col-md-9 col-md-push-3 create-product form-section"
>
<div
class=
"col-md-9 col-md-push-3 create-product form-section"
>
<h1
class=
"h2 heading-primary font-weight-normal"
>
<h1
class=
"h2 heading-primary font-weight-normal"
>
Edit Product #
<%=
@product
.
id
%>
Edit Product #
<%=
@product
.
id
%>
(
<%=
link_to
'Delete'
,
@product
,
method: :delete
,
data:
{
confirm:
'Are your sure?'
}
%>
)
</h1>
</h1>
<div
class=
"featured-box featured-box-primary featured-box-flat featured-box-text-left mt-md"
>
<div
class=
"featured-box featured-box-primary featured-box-flat featured-box-text-left mt-md"
>
...
...
app/views/shared/_flash_messages.html.erb
0 → 100644
View file @
a01f17cd
<%
flash
.
each
do
|
message_type
,
message
|
%>
<div
class=
"alert alert-
<%=
message_type
%>
"
>
<span>
<%=
message
%>
</span>
</div>
<%
end
%>
\ No newline at end of file
app/views/static_pages/index.html.erb
View file @
a01f17cd
<div
class=
"container"
>
<div
class=
"container"
>
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-md-9 col-md-push-3"
>
<div
class=
"col-md-9 col-md-push-3"
>
<%=
render
'shared/flash_messages'
%>
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<span>
Recommended Items
</span>
<span>
Recommended Items
</span>
</h2>
</h2>
...
...
db/migrate/20170608073720_add_user_ref_to_products.rb
View file @
a01f17cd
class
AddUserRefToProducts
<
ActiveRecord
::
Migration
[
5.1
]
class
AddUserRefToProducts
<
ActiveRecord
::
Migration
[
5.1
]
def
change
def
change
add_reference
:products
,
:user
,
foreign_key
:
true
add_reference
:products
,
:user
,
index
:
true
end
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