Commit a284126b by Hoang Phuc Committed by Hoang Phuc Do

Quantity input in product show

parent d84cb7a5
...@@ -52,6 +52,8 @@ gem 'ckeditor', '~> 4.2', '>= 4.2.3' ...@@ -52,6 +52,8 @@ gem 'ckeditor', '~> 4.2', '>= 4.2.3'
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.2' gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.2'
# Use jquery as the JavaScript library # Use jquery as the JavaScript library
gem 'jquery-rails' gem 'jquery-rails'
# Bootstrap TouchSpin is a mobile and touch friendly input spinner component for Bootstrap
gem 'rails-assets-bootstrap-touchspin', source: 'https://rails-assets.org'
# Use Capistrano for deployment # Use Capistrano for deployment
# gem 'capistrano-rails', group: :development # gem 'capistrano-rails', group: :development
......
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
remote: https://rails-assets.org/
specs: specs:
actioncable (5.1.1) actioncable (5.1.1)
actionpack (= 5.1.1) actionpack (= 5.1.1)
...@@ -193,6 +194,12 @@ GEM ...@@ -193,6 +194,12 @@ GEM
bundler (>= 1.3.0, < 2.0) bundler (>= 1.3.0, < 2.0)
railties (= 5.1.1) railties (= 5.1.1)
sprockets-rails (>= 2.0.0) sprockets-rails (>= 2.0.0)
rails-assets-bootstrap (3.3.7)
rails-assets-jquery (>= 1.9.1, < 4)
rails-assets-bootstrap-touchspin (3.1.2)
rails-assets-bootstrap (>= 3.0.0)
rails-assets-jquery (>= 1.9.0)
rails-assets-jquery (3.2.1)
rails-dom-testing (2.0.3) rails-dom-testing (2.0.3)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
nokogiri (>= 1.6) nokogiri (>= 1.6)
...@@ -300,6 +307,7 @@ DEPENDENCIES ...@@ -300,6 +307,7 @@ DEPENDENCIES
mysql2 (>= 0.3.18, < 0.5) mysql2 (>= 0.3.18, < 0.5)
puma (~> 3.7) puma (~> 3.7)
rails (~> 5.1.1) rails (~> 5.1.1)
rails-assets-bootstrap-touchspin!
rubocop (~> 0.49.1) rubocop (~> 0.49.1)
sass-rails (~> 5.0) sass-rails (~> 5.0)
selenium-webdriver selenium-webdriver
...@@ -311,4 +319,4 @@ DEPENDENCIES ...@@ -311,4 +319,4 @@ DEPENDENCIES
web-console (>= 3.3.0) web-console (>= 3.3.0)
BUNDLED WITH BUNDLED WITH
1.15.0 1.15.1
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
// //
//= require jquery //= require jquery
//= require jquery_ujs //= require jquery_ujs
//= require bootstrap-touchspin
//# require rails-ujs //# require rails-ujs
//= require turbolinks //= require turbolinks
//# require_tree . //# require_tree .
(function($) {
// Vertical Spinner - Touchspin - Product Details Quantity input
if ( $.fn.TouchSpin ) {
$('#product_vqty').TouchSpin({
verticalbuttons: true
});
$('#qty_input').TouchSpin();
}
}).apply(this, [jQuery]);
\ No newline at end of file
//= require shop-14
\ No newline at end of file
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
* *
*# require_tree . *# require_tree .
*= require_self *= require_self
*= require bootstrap-touchspin
*= require font-awesome
*= require custom *= require custom
*= require main_style *= require main_style
*= require skin_14 *= require skin_14
......
...@@ -13,14 +13,11 @@ class CartsController < ApplicationController ...@@ -13,14 +13,11 @@ class CartsController < ApplicationController
end end
def delete_line_item def delete_line_item
product_id = params[:id] remove_from_cart(params[:id])
if @cart.has_key? product_id product_title = Product.select(:title).find(params[:id]).title
@cart.delete(product_id) respond_to do |format|
session[:cart] = @cart format.html { redirect_to cart_index_url, notice: "#{product_title} was sucessfully removed from your cart" }
product_title = Product.select(:title).find(product_id).title format.js
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product_title} was sucessfully removed from your cart" }
end
end end
end end
......
...@@ -2,11 +2,10 @@ class LineItemsController < ApplicationController ...@@ -2,11 +2,10 @@ class LineItemsController < ApplicationController
include CartsHelper include CartsHelper
before_action :set_cart, only: [:create] before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def create def create
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
@line_items = add_to_cart(product, 1) add_to_cart(product, params[:product_vqty].to_i)
respond_to do |format| respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" } format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" }
...@@ -14,22 +13,4 @@ class LineItemsController < ApplicationController ...@@ -14,22 +13,4 @@ class LineItemsController < ApplicationController
format.json { render 'show' } format.json { render 'show' }
end end
end end
def destroy
@line_item.destroy
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully removed from your cart" }
format.json { head :no_content }
end
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id)
end
end end
\ No newline at end of file
...@@ -6,14 +6,23 @@ module CartsHelper ...@@ -6,14 +6,23 @@ module CartsHelper
end end
def add_to_cart(product, quantity) def add_to_cart(product, quantity)
cart_key = product.id cart_key = product.id.to_s
if @cart.has_key? cart_key.to_s return remove_from_cart(cart_key) if quantity.zero?
@cart[cart_key.to_s]['quantity'] += quantity if @cart.key?(cart_key)
@cart[cart_key][:quantity.to_s] += quantity
else else
@cart[cart_key] = { quantity: quantity } @cart[cart_key] = { quantity: quantity }
end end
end end
def update_cart_item(product_id, quantity)
end
def remove_from_cart(product_id)
@cart = @cart.delete(product_id) if @cart.key?(product_id)
end
def destroy_cart_session def destroy_cart_session
session[:cart] = nil session[:cart] = nil
end end
...@@ -28,10 +37,10 @@ module CartsHelper ...@@ -28,10 +37,10 @@ module CartsHelper
end end
def cart_total_items def cart_total_items
if @cart.nil? if session[:cart].nil?
total = 0 total = 0
else else
total = session[:cart].sum { |product_id, product_attrs| product_attrs['quantity'] || product_attrs[:quantity] } total = session[:cart].sum { |product_id, product_attrs| product_attrs[:quantity.to_s] || product_attrs[:quantity] }
end end
total total
end end
......
...@@ -33,7 +33,14 @@ ...@@ -33,7 +33,14 @@
</h2> </h2>
</td> </td>
<td><%= number_to_currency(line_item[:product].price) %></td> <td><%= number_to_currency(line_item[:product].price) %></td>
<td><%= line_item[:quantity] %></td> <td>
<%= form_tag do %>
<div class="qty-holder">
<%= number_field_tag :qty_input, line_item[:quantity], class: 'qty-input' %>
<%= button_tag nil, class: 'edit-qty' do fa_icon('pencil') end %>
</div>
<% end %>
</td>
<td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td> <td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td>
</tr> </tr>
<% end %> <% end %>
......
...@@ -14,5 +14,6 @@ ...@@ -14,5 +14,6 @@
<%= yield %> <%= yield %>
</div> </div>
<%= render 'layouts/footer' %> <%= render 'layouts/footer' %>
<%= javascript_include_tag 'theme-main' %>
</body> </body>
</html> </html>
<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">
...@@ -17,5 +16,4 @@ ...@@ -17,5 +16,4 @@
</div> </div>
</div> </div>
</li> </li>
<% end %> <% end %>
</div> \ No newline at end of file
\ No newline at end of file
...@@ -22,9 +22,12 @@ ...@@ -22,9 +22,12 @@
</div> </div>
</div> </div>
<div class="product-actions"> <div class="product-actions">
<div class="product-detail-qty"> <%= form_tag line_items_path(product_id: @product), remote: true do %>
</div> <div class="product-detail-qty">
<%= button_to 'Add to cart', line_items_path(product_id: @product), remote: true, class: 'addtocart' %> <%= number_field_tag :product_vqty, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<% end %>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -12,3 +12,4 @@ Rails.application.config.assets.paths << Rails.root.join('node_modules') ...@@ -12,3 +12,4 @@ Rails.application.config.assets.paths << Rails.root.join('node_modules')
# application.js, application.css, and all non-JS/CSS in the app/assets # application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added. # folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css ) # Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.precompile += %w( theme-main.js )
...@@ -8,6 +8,7 @@ Rails.application.routes.draw do ...@@ -8,6 +8,7 @@ Rails.application.routes.draw do
resources :cart, only: :index, controller: 'carts' resources :cart, only: :index, controller: 'carts'
delete '/cart/remove/line_item/:id', to: 'carts#delete_line_item', as: 'remove_line_item_cart' delete '/cart/remove/line_item/:id', to: 'carts#delete_line_item', as: 'remove_line_item_cart'
delete '/cart/remove', to: 'carts#destroy', as: 'remove_cart' delete '/cart/remove', to: 'carts#destroy', as: 'remove_cart'
post 'cart/update', to: 'carts#update', as: 'update_cart'
devise_for :users devise_for :users
ActiveAdmin.routes(self) ActiveAdmin.routes(self)
mount Ckeditor::Engine => '/ckeditor' mount Ckeditor::Engine => '/ckeditor'
......
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