Commit e340ee6a by vulehuan

posting feature: list user items and warning in product detail page

parent 1df0b000
......@@ -8,6 +8,12 @@ class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
Please register or sign in to reply
if !@product.status
user = current_user
if user == nil || @product.user_id != user.id
Please register or sign in to reply
redirect_to products_path
end
end
product_category = ProductCategory.find(@product.product_category_id)
if product_category != nil
Please register or sign in to reply
add_breadcrumb product_category.name, product_category_path(product_category)
......@@ -25,6 +31,8 @@ class ProductsController < ApplicationController
@product = Product.new(product_params)
Please register or sign in to reply
user = current_user
@product.user_id = user.id
@product.status = false
@product.availability = 'instock'
if @product.save
flash[:success] = "Add new product successfully!"
redirect_to @product
......@@ -34,12 +42,16 @@ class ProductsController < ApplicationController
end
def user_items
add_breadcrumb "My items", url_for(action: 'user_items')
user = current_user
all_status = user != nil && user.id.to_s == params[:user_id].to_s
@products = Product.get_user_items(limit: 16, page: params[:page], user_id: params[:user_id], all_status: all_status)
end
private
def product_params
params.require(:product).permit(:name, :description, :headline, :availability, :code, :condition, :image_small, :image_medium, :price_currency, :price, :product_category_id)
params.require(:product).permit(:name, :description, :headline, :code, :condition, :image_small, :image_medium, :price_currency, :price, :product_category_id)
end
# Before filters
......
class Product < ActiveRecord::Base
validates :name, :availability, :code, :condition, :image_medium, :price, :price_currency, presence: true
validates :name, :code, :condition, :image_medium, :price, :price_currency, presence: true
belongs_to :product_category
# Returns recommended products (order by rate, and available)
def self.get_recommended_products(options = { limit: 8 })
limit = options[:limit]
return Product.select('id, name, image_medium, price, price_currency').where(availability: 'instock').order('review_rate DESC').order('review_count DESC').limit(limit)
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(status: true)
.order('review_rate DESC').order('review_count DESC')
.limit(limit)
end
# Returns newest products
def self.get_newest_products(options = { limit: 8 })
limit = options[:limit]
return Product.select('id, name, image_medium, price, price_currency').where(availability: 'instock').order('created_at DESC').order('updated_at DESC').order('name').limit(limit)
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(status: true)
.order('created_at DESC').order('updated_at DESC').order('name')
.limit(limit)
end
# Returns products in a category
......@@ -21,6 +29,7 @@ class Product < ActiveRecord::Base
page = options[:page]
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock', product_category_id: product_category_id)
.where(status: true)
.paginate(:page => page, :per_page => limit)
.order('created_at DESC').order('updated_at DESC').order('name')
end
......@@ -32,6 +41,7 @@ class Product < ActiveRecord::Base
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock').where(product_category_id: product_category_id).where('id != ?', product_id)
.where(status: true)
.order('created_at DESC').order('updated_at DESC').order('name')
.limit(limit)
end
......@@ -42,7 +52,22 @@ class Product < ActiveRecord::Base
page = options[:page]
return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(status: true)
.paginate(:page => page, :per_page => limit)
.order('created_at DESC').order('updated_at DESC').order('name')
end
def self.get_user_items(options = { limit: 16, user_id: 0, all_status: false })
limit = options[:limit]
page = options[:page]
user_id = options[:user_id]
query = Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock')
.where(user_id: user_id)
if !options[:all_status]
query = query.where(status: true)
end
query = query.paginate(:page => page, :per_page => limit).order('created_at DESC').order('updated_at DESC').order('name')
return query
end
end
\ No newline at end of file
......@@ -11,7 +11,10 @@
</div>
<div class="block-user-action">
<div class="btn-group">
<% if signed_in? %>
<%
Please register or sign in to reply
if signed_in?
user = current_user
%>
<a href="<%= cards_path %>" class="btn btn-danger"> <span class="glyphicon glyphicon-shopping-cart"></span> Your cart
</a>
<a data-toggle="dropdown" href="#" class="btn btn-default"><span class="glyphicon glyphicon-list"></span> My actions</a>
......@@ -22,7 +25,7 @@
<% end %>
</li>
<li>
<%= link_to url_for(controller: 'products', action:'user_items') do %>
<%= link_to url_for(controller: 'products', action:'user_items', user_id: user.id) do %>
<span class="glyphicon glyphicon-home"></span> My products
<% end %>
</li>
......
......@@ -11,9 +11,6 @@
<%= f.label :headline %>
<%= f.text_field :headline, class: "form-control" %>
<%= f.label :availability %>
<%= f.text_field :availability, class: "form-control" %>
<%= f.label :code %>
<%= f.text_field :code, class: "form-control" %>
......
......@@ -2,6 +2,11 @@
<div class="body-box">
<h2 class="sprite-2"><%= @product.name %><span class="sprite-2"></span></h2>
<div class="text-justify product-detail">
<% if !@product.status %>
<div class="alert alert-danger">
<p class="text-center">This product is only visible to you.</p>
</div>
<% end %>
<div class="row">
<div class="col-md-6">
<a data-fancybox-type="image" href="<%= @product.image_medium %>" class="thumbnail fancybox"><img src="<%= @product.image_medium %>" alt="" /></a>
......
<% provide(:title, 'My items') %>
<div class="body-box grid-view">
<h2 class="sprite-2">My items<span class="sprite-2"></span></h2>
<div class="text-justify">
<%= render 'shared/grid', items:@products %>
<%= will_paginate @products, renderer: BootstrapPagination::Rails %>
</div>
</div>
\ No newline at end of file
<%
if items == nil
return ''
end
if items == nil || items.empty?
%>
<p>No products available.</p>
<%
else
items.each_slice(4) do |row|
%>
<div class="row">
......@@ -28,3 +30,4 @@
<% end %>
</div>
<% end %>
<% end %>
......@@ -93,7 +93,7 @@ namespace :db do
price_currency: price_currency,
price: price,
product_category_id: category_id,
status: 1
status: true
)
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment