Commit 4f37530b by Hoang Phuc Do

Allow user input product quantity

parent f0d57a97
......@@ -947,6 +947,60 @@ html .featured-box-primary .box-content {
/*
* Product details
*/
@media (max-width: 767px) {
.product-img-box {
margin-bottom: 25px;
}
}
.product-img-box img {
display: block;
width: 100%;
height: auto;
}
.product-img-box .product-img-wrapper {
padding: 3px;
border: 1px solid #ddd;
border-radius: 0;
}
.product-img-box-wrapper {
position: relative;
margin-bottom: 10px;
}
.product-details-box .product-name {
margin: 15px 0;
font-size: 28px;
font-weight: 600;
line-height: 1;
color: #555;
}
.product-details-box .product-short-desc {
padding: 0 0 10px;
border-bottom: 1px solid #ebebeb;
}
.product-details-box .product-short-desc p {
font-size: 14px;
line-height: 1.65;
margin: 0 0 20px;
}
.product-details-box .product-detail-info {
padding-bottom: 20px;
margin-top: 20px;
border-bottom: 1px solid #ebebeb;
}
.product-details-box .product-detail-info .product-price-box {
margin: 0 0 20px;
}
.product-details-box .product-detail-info .product-price-box .product-price {
font-size: 33px;
line-height: 1;
color: #000;
}
.product-details-box .product-detail-info .availability {
margin: 0 0 10px;
font-size: 14px;
color: #777;
font-weight: 400;
}
.product-details-box .product-detail-qty {
display: inline-block;
vertical-align: middle;
......
......@@ -69,61 +69,15 @@ html .btn-primary:active:focus {
.btn {
border-radius: 0;
}
.font-weight-semibold {
font-weight: 600 !important;
}
.featured-box .box-content {
border-radius: 0;
}
.product-essential {
margin-bottom: 50px;
}
@media (max-width: 767px) {
.product-img-box {
margin-bottom: 25px;
}
}
.product-img-box img {
display: block;
width: 100%;
height: auto;
}
.product-img-box .product-img-wrapper {
padding: 3px;
border: 1px solid #ddd;
border-radius: 0;
}
.product-img-box-wrapper {
position: relative;
margin-bottom: 10px;
}
.product-details-box .product-name {
margin: 15px 0;
font-size: 28px;
font-weight: 600;
line-height: 1;
color: #555;
}
.product-details-box .product-short-desc {
padding: 0 0 10px;
border-bottom: 1px solid #ebebeb;
}
.product-details-box .product-short-desc p {
font-size: 14px;
line-height: 1.65;
margin: 0 0 20px;
}
.product-details-box .product-detail-info {
padding-bottom: 20px;
margin-top: 20px;
border-bottom: 1px solid #ebebeb;
}
.product-details-box .product-detail-info .product-price-box {
margin: 0 0 20px;
}
.product-details-box .product-detail-info .product-price-box .product-price {
font-size: 33px;
line-height: 1;
color: #000;
}
#header .header-body {
border-top: none;
background-color: transparent;
......
class LineItemsController < ApplicationController
include CartsHelper
include LineItemsHelper
before_action :set_cart, only: [:create, :update, :destroy]
# We don not render line item details page
# We do not render line item details page
def show
redirect_to product_url(params[:id])
end
......@@ -11,20 +12,32 @@ class LineItemsController < ApplicationController
# POST /line_items
def create
product = Product.find(params[:product_id])
add_to_cart(product.id.to_s, params[:product_quantity].to_i)
product_is_in_stock = product_is_in_stock?(product, get_line_item_quantity(product.id, params[:product_quantity]))
add_to_cart(product.id, params[:product_quantity]) if product_is_in_stock
respond_to do |format|
if product_is_in_stock
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" }
format.js
else
format.html { redirect_to cart_index_url, notice: "#{product.title} is out of stock" }
end
end
end
# PATCH/PUT /line_items/1
def update
update_cart_item(*update_params.to_h.values)
update_arg = update_params.to_h.values
product = Product.find(update_arg.first)
unless product_is_in_stock?(product, update_arg.last)
return respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} is out of stock" }
end
end
update_cart_item(*update_arg)
respond_to do |format|
format.html { redirect_to cart_index_url }
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully updated" }
end
end
......@@ -44,4 +57,8 @@ class LineItemsController < ApplicationController
def update_params
params.permit(:id, :qty_input)
end
def product_is_in_stock?(product, quantity)
product.quantity >= quantity.to_i
end
end
\ No newline at end of file
......@@ -14,7 +14,7 @@ class OrdersController < ApplicationController
# POST /orders
def create
@order = current_user.orders.create
@order = current_user.orders.new
create_line_items_for_order(@order)
respond_to do |format|
......
......@@ -47,8 +47,8 @@ class ProductsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :sku, :price, :description,
:category_id, :image_url)
params.require(:product).permit(:title, :sku, :price, :quantity,
:description, :category_id, :image_url)
end
# Is current user own current editing product?
......
......@@ -5,11 +5,11 @@ module CartsHelper
end
def add_to_cart(product_id, quantity)
return if quantity.zero?
if @cart.key?(product_id)
@cart[product_id][:quantity.to_s] += quantity
return if quantity.to_i.zero?
if @cart.key?(product_id.to_s)
@cart[product_id.to_s][:quantity.to_s] += quantity.to_i
else
@cart[product_id] = { quantity: quantity }
@cart[product_id.to_s] = { quantity: quantity.to_i }
end
end
......
......@@ -6,4 +6,10 @@ module LineItemsHelper
quantity: product_attrs['quantity'].to_i }
end
end
def get_line_item_quantity(product_id, new_quantity = 0)
return new_quantity.to_i if @cart.empty? || !@cart.key?(product_id.to_s)
old_quantity = @cart[product_id.to_s][:quantity] || @cart[product_id.to_s][:quantity.to_s]
new_quantity.to_i + old_quantity
end
end
\ No newline at end of file
......@@ -12,14 +12,21 @@
<div class="col-md-6">
<div class="form-group">
<%= f.label :sku %>
<%= f.text_field :sku, class: "form-control", step: :any %>
<%= f.text_field :sku, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.label :price %>
<%= f.number_field :price, class: "form-control" %>
<%= f.number_field :price, step: :any, class: "form-control" %>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<%= f.label :quantity %>
<%= f.number_field :quantity, class: "form-control" %>
</div>
</div>
......
<li class="product-<%= product.id %>">
<div class="product product-list">
<figure class="product-image-area">
<%= link_to image_tag(get_product_thumbnail(product, 170, 204)), product_path(product) %>
</figure>
<div class="product-details-area">
<h2 class="product-name">
<%= link_to product.title, product_url(product) %>
</h2>
<div class="product-view">
<div class="product-essential">
<div class="row">
<div class="product-img-box col-sm-5">
<div class="product-img-box-wrapper">
<div class="product-img-wrapper">
<%= image_tag(get_product_thumbnail(product, 715, 952)) %>
</div>
</div>
</div>
<div class="product-details-box col-sm-7">
<h1 class="product-name">
<%= product.title %>
</h1>
<div class="product-short-desc">
<%= product.description %>
</div>
<div class="product-detail-info">
<div class="product-price-box">
<span class="product-price"><%= number_to_currency(product.price) %></span>
</div>
<p class="availability">
<span class="font-weight-semibold">Availability:</span>
<%= product.quantity %>
</p>
</div>
<div class="product-actions">
<%= form_tag line_items_path(product_id: product), remote: true do %>
<div class="product-detail-qty">
<%= number_field_tag :product_quantity, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<% end %>
</div>
</div>
</div>
</div>
</li>
</div>
\ No newline at end of file
<% @recommended_products.each do |product| %>
<div class="products-grid columns<%= columns %>">
<% products.each do |product| %>
<li class="product-<%= product.id %>">
<div class="product">
<figure class="product-image-area">
......@@ -14,4 +15,5 @@
</div>
</div>
</li>
<% end %>
\ No newline at end of file
<% end %>
</div>
\ No newline at end of file
<ul class="products-list">
<% products.each do |product| %>
<li class="product-<%= product.id %>">
<div class="product product-list">
<figure class="product-image-area">
<%= link_to image_tag(get_product_thumbnail(product, 170, 204)), product_path(product) %>
</figure>
<div class="product-details-area">
<h2 class="product-name">
<%= link_to product.title, product_url(product) %>
</h2>
<div class="product-short-desc">
<%= product.description %>
</div>
<div class="product-price-box">
<span class="product-price"><%= number_to_currency(product.price) %></span>
</div>
</div>
</div>
</li>
<% end %>
</ul>
\ No newline at end of file
<div class="container">
<div class="product-view">
<div class="product-essential">
<div class="row">
<div class="product-img-box col-sm-5">
<div class="product-img-box-wrapper">
<div class="product-img-wrapper">
<%= image_tag(get_product_thumbnail(@product, 715, 952)) %>
</div>
</div>
</div>
<div class="product-details-box col-sm-7">
<h1 class="product-name">
<%= @product.title %>
</h1>
<div class="product-short-desc">
<%= @product.description %>
</div>
<div class="product-detail-info">
<div class="product-price-box">
<span class="product-price"><%= number_to_currency(@product.price) %></span>
</div>
</div>
<div class="product-actions">
<%= form_tag line_items_path(product_id: @product), remote: true do %>
<div class="product-detail-qty">
<%= number_field_tag :product_quantity, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= render @product %>
</div>
\ No newline at end of file
......@@ -5,16 +5,12 @@
<h2 class="h2 heading-primary mt-lg clearfix">
<span>Recommended Items</span>
</h2>
<div class="products-grid columns3">
<%= render 'products/recommended' %>
</div>
<%= render partial: 'products/product_grid', locals: { products: @recommended_products, columns: 3 } %>
<h2 class="h2 heading-primary mt-lg clearfix">
<span>Newest Items</span>
</h2>
<ul class="products-list">
<%= render @latest_products %>
</ul>
<%= render partial: 'products/product_list', locals: { products: @latest_products } %>
<div class="toolbar-bottom">
<div class="toolbar">
......
class AddQuantityToProducts < ActiveRecord::Migration[5.1]
def change
add_column :products, :quantity, :integer, default: 1
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170613012113) do
ActiveRecord::Schema.define(version: 20170615035827) do
create_table "categories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title"
......@@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 20170613012113) do
t.datetime "updated_at", null: false
t.string "image_url"
t.bigint "user_id"
t.integer "quantity", default: 1
t.index ["category_id"], name: "index_products_on_category_id"
t.index ["user_id"], name: "index_products_on_user_id"
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