Commit e340ee6a by vulehuan

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

parent 1df0b000
...@@ -8,6 +8,12 @@ class ProductsController < ApplicationController ...@@ -8,6 +8,12 @@ class ProductsController < ApplicationController
def show def show
@product = Product.find(params[:id]) @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) product_category = ProductCategory.find(@product.product_category_id)
if product_category != nil if product_category != nil
Please register or sign in to reply
add_breadcrumb product_category.name, product_category_path(product_category) add_breadcrumb product_category.name, product_category_path(product_category)
...@@ -25,6 +31,8 @@ class ProductsController < ApplicationController ...@@ -25,6 +31,8 @@ class ProductsController < ApplicationController
@product = Product.new(product_params) @product = Product.new(product_params)
Please register or sign in to reply
user = current_user user = current_user
@product.user_id = user.id @product.user_id = user.id
@product.status = false
@product.availability = 'instock'
if @product.save if @product.save
flash[:success] = "Add new product successfully!" flash[:success] = "Add new product successfully!"
redirect_to @product redirect_to @product
...@@ -34,12 +42,16 @@ class ProductsController < ApplicationController ...@@ -34,12 +42,16 @@ class ProductsController < ApplicationController
end end
def user_items 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 end
private private
def product_params 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 end
# Before filters # Before filters
......
class Product < ActiveRecord::Base 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 belongs_to :product_category
# Returns recommended products (order by rate, and available) # Returns recommended products (order by rate, and available)
def self.get_recommended_products(options = { limit: 8 }) def self.get_recommended_products(options = { limit: 8 })
limit = options[:limit] 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 end
# Returns newest products # Returns newest products
def self.get_newest_products(options = { limit: 8 }) def self.get_newest_products(options = { limit: 8 })
limit = options[:limit] 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 end
# Returns products in a category # Returns products in a category
...@@ -21,6 +29,7 @@ class Product < ActiveRecord::Base ...@@ -21,6 +29,7 @@ class Product < ActiveRecord::Base
page = options[:page] page = options[:page]
return Product.select('id, name, image_medium, price, price_currency') return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock', product_category_id: product_category_id) .where(availability: 'instock', product_category_id: product_category_id)
.where(status: true)
.paginate(:page => page, :per_page => limit) .paginate(:page => page, :per_page => limit)
.order('created_at DESC').order('updated_at DESC').order('name') .order('created_at DESC').order('updated_at DESC').order('name')
end end
...@@ -32,6 +41,7 @@ class Product < ActiveRecord::Base ...@@ -32,6 +41,7 @@ class Product < ActiveRecord::Base
return Product.select('id, name, image_medium, price, price_currency') 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(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') .order('created_at DESC').order('updated_at DESC').order('name')
.limit(limit) .limit(limit)
end end
...@@ -42,7 +52,22 @@ class Product < ActiveRecord::Base ...@@ -42,7 +52,22 @@ class Product < ActiveRecord::Base
page = options[:page] page = options[:page]
return Product.select('id, name, image_medium, price, price_currency') return Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock') .where(availability: 'instock')
.where(status: true)
.paginate(:page => page, :per_page => limit) .paginate(:page => page, :per_page => limit)
.order('created_at DESC').order('updated_at DESC').order('name') .order('created_at DESC').order('updated_at DESC').order('name')
end 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 end
\ No newline at end of file
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
</div> </div>
<div class="block-user-action"> <div class="block-user-action">
<div class="btn-group"> <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 href="<%= cards_path %>" class="btn btn-danger"> <span class="glyphicon glyphicon-shopping-cart"></span> Your cart
</a> </a>
<a data-toggle="dropdown" href="#" class="btn btn-default"><span class="glyphicon glyphicon-list"></span> My actions</a> <a data-toggle="dropdown" href="#" class="btn btn-default"><span class="glyphicon glyphicon-list"></span> My actions</a>
...@@ -22,7 +25,7 @@ ...@@ -22,7 +25,7 @@
<% end %> <% end %>
</li> </li>
<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 <span class="glyphicon glyphicon-home"></span> My products
<% end %> <% end %>
</li> </li>
......
...@@ -11,9 +11,6 @@ ...@@ -11,9 +11,6 @@
<%= f.label :headline %> <%= f.label :headline %>
<%= f.text_field :headline, class: "form-control" %> <%= f.text_field :headline, class: "form-control" %>
<%= f.label :availability %>
<%= f.text_field :availability, class: "form-control" %>
<%= f.label :code %> <%= f.label :code %>
<%= f.text_field :code, class: "form-control" %> <%= f.text_field :code, class: "form-control" %>
......
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
<div class="body-box"> <div class="body-box">
<h2 class="sprite-2"><%= @product.name %><span class="sprite-2"></span></h2> <h2 class="sprite-2"><%= @product.name %><span class="sprite-2"></span></h2>
<div class="text-justify product-detail"> <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="row">
<div class="col-md-6"> <div class="col-md-6">
<a data-fancybox-type="image" href="<%= @product.image_medium %>" class="thumbnail fancybox"><img src="<%= @product.image_medium %>" alt="" /></a> <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 if items == nil || items.empty?
Please register or sign in to reply
return ''
end
items.each_slice(4) do |row|
%> %>
<div class="row"> <p>No products available.</p>
<% <%
row.each do |obj| else
url = product_path(obj) items.each_slice(4) do |row|
add_to_card_url = cards_path + "?product_id=" + obj.id.to_s
%> %>
<div class="col-md-3 col-sm-3"> <div class="row">
<div class="item"> <%
<h4> row.each do |obj|
<a href="<%= url %>" title="<%= obj.name %>"><%= obj.name %></a> url = product_path(obj)
</h4> add_to_card_url = cards_path + "?product_id=" + obj.id.to_s
<a href="<%= url %>" class="thumbnail" title="<%= obj.name %>"><img src="<%= obj.image_medium %>" alt="<%= obj.name %>"></a> %>
<div class="price"> <div class="col-md-3 col-sm-3">
Price: <span><%= number_with_delimiter(obj.price) %> <%= obj.price_currency %></span> <div class="item">
</div> <h4>
<div class="action"> <a href="<%= url %>" title="<%= obj.name %>"><%= obj.name %></a>
<a class="view-detail" href="<%= url %>" title="<%= obj.name %>">Detail</a><a class="order" </h4>
href="<%= add_to_card_url %>">Order</a> <a href="<%= url %>" class="thumbnail" title="<%= obj.name %>"><img src="<%= obj.image_medium %>" alt="<%= obj.name %>"></a>
<div class="price">
Price: <span><%= number_with_delimiter(obj.price) %> <%= obj.price_currency %></span>
</div>
<div class="action">
<a class="view-detail" href="<%= url %>" title="<%= obj.name %>">Detail</a><a class="order"
href="<%= add_to_card_url %>">Order</a>
</div>
</div>
</div> </div>
<% end %>
</div> </div>
</div>
<% end %> <% end %>
</div> <% end %>
<% end %>
\ No newline at end of file
...@@ -93,7 +93,7 @@ namespace :db do ...@@ -93,7 +93,7 @@ namespace :db do
price_currency: price_currency, price_currency: price_currency,
price: price, price: price,
product_category_id: category_id, product_category_id: category_id,
status: 1 status: true
) )
end end
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