Commit ccd4ffc7 by Hoang Phuc Do

Fix merge request #3

parent a1294599
......@@ -46,9 +46,12 @@ gem 'devise', '~> 4.3'
gem 'rubocop', '~> 0.49.1'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# FontAwesome
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.2'
# Bootstrap TouchSpin is a mobile and touch friendly input spinner component for Bootstrap
gem 'rails-assets-bootstrap-touchspin', source: 'https://rails-assets.org'
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.2'
# Simple, efficient background processing for Ruby.
gem 'sidekiq', '~> 5.0', '>= 5.0.2'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
......@@ -59,8 +62,6 @@ group :development, :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1'
end
group :development do
......@@ -70,6 +71,8 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
......
......@@ -73,6 +73,7 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
connection_pool (2.2.1)
devise (4.3.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
......@@ -140,6 +141,8 @@ GEM
public_suffix (2.0.5)
puma (3.9.1)
rack (2.0.3)
rack-protection (2.0.0)
rack
rack-test (0.6.3)
rack (>= 1.0)
rails (5.1.1)
......@@ -177,6 +180,7 @@ GEM
rb-fsevent (0.9.8)
rb-inotify (0.9.8)
ffi (>= 0.5.0)
redis (3.3.3)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
......@@ -201,6 +205,11 @@ GEM
childprocess (~> 0.5)
rubyzip (~> 1.0)
websocket (~> 1.0)
sidekiq (5.0.2)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (~> 3.3, >= 3.3.3)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
......@@ -263,6 +272,7 @@ DEPENDENCIES
rubocop (~> 0.49.1)
sass-rails (~> 5.0)
selenium-webdriver
sidekiq (~> 5.0, >= 5.0.2)
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
......
(function($) {
// Vertical Spinner - Touchspin - Product Details Quantity input
if ( $.fn.TouchSpin ) {
$('#product_vqty').TouchSpin({
$('#product_quantity').TouchSpin({
verticalbuttons: true
});
$('.qty-input').TouchSpin();
......
(function($) {
}).apply(this, [jQuery]);
\ No newline at end of file
//= require shop-14
\ No newline at end of file
//= require cart/cart
\ No newline at end of file
......@@ -954,7 +954,7 @@ html .featured-box-primary .box-content {
width: 60px;
}
.product-details-box .product-detail-qty #product_vqty {
.product-details-box .product-detail-qty #product_quantity {
border-radius: 0;
width: 35px !important;
border-color: #e1e1e1;
......
class ApplicationController < ActionController::Base
include CartsHelper
protect_from_forgery with: :exception
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
include CartsHelper
before_action :set_cart
before_action :configure_permitted_parameters, if: :devise_controller?
......
......@@ -3,6 +3,7 @@ class LineItemsController < ApplicationController
before_action :set_cart, only: [:create, :update, :destroy]
# We don not render line item details page
def show
redirect_to product_url(params[:id])
end
......@@ -10,7 +11,7 @@ class LineItemsController < ApplicationController
# POST /line_items
def create
product = Product.find(params[:product_id])
add_to_cart(product.id.to_s, params[:product_vqty].to_i)
add_to_cart(product.id.to_s, params[:product_quantity].to_i)
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" }
......@@ -29,11 +30,11 @@ class LineItemsController < ApplicationController
# DELETE /line_items/1
def destroy
remove_from_cart(params[:id])
product_title = Product.select(:title).find(params[:id]).title
remove_line_item_from_cart(params[:id])
product = Product.find(params[:id])
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product_title} was sucessfully removed from your cart" }
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully removed from your cart" }
end
end
......
......@@ -5,10 +5,10 @@ class OrdersController < ApplicationController
before_action :authenticate_user!, only: :new
before_action :set_cart, only: [:new, :create]
before_action :get_line_items, only: :new
before_action :ensure_cart_is_not_empty, only: :new
# GET /orders/new
def new
redirect_to root_url if @cart.blank?
@order = Order.new
end
......@@ -20,10 +20,10 @@ class OrdersController < ApplicationController
respond_to do |format|
if @order.save
destroy_cart_session
OrderMailer.order_detail(current_user, @order).deliver_now
format.html { redirect_to root_url }
OrderMailer.send_order_detail_to_user(current_user, @order).deliver_later
format.html { redirect_to root_url, notice: 'Your order is successfully created' }
else
format.html { redirect_to root_url }
format.html { redirect_to root_url, notice: 'Your order can not be created' }
end
end
end
......@@ -33,10 +33,4 @@ class OrdersController < ApplicationController
order.line_items.create(product_id: product_id, quantity: product_attrs['quantity'])
end
end
private
def ensure_cart_is_not_empty
redirect_to root_url if @cart.blank?
end
end
\ No newline at end of file
......@@ -5,7 +5,7 @@ module CartsHelper
end
def add_to_cart(product_id, quantity)
return remove_from_cart(product_id) if quantity.zero?
return if quantity.zero?
if @cart.key?(product_id)
@cart[product_id][:quantity.to_s] += quantity
else
......@@ -16,14 +16,14 @@ module CartsHelper
def update_cart_item(product_id, quantity)
return unless @cart.key?(product_id)
if quantity.to_i.zero?
remove_from_cart(product_id)
remove_line_item_from_cart(product_id)
else
@cart[product_id][:quantity.to_s] = quantity.to_i
end
end
def remove_from_cart(product_id)
@cart = @cart.delete(product_id) if @cart.key?(product_id)
def remove_line_item_from_cart(product_id)
@cart.tap { |cart| cart.delete(product_id) } if @cart.key?(product_id)
end
def destroy_cart_session
......@@ -32,8 +32,8 @@ module CartsHelper
def cart_total_price
@cart.sum do |product_id, product_attrs|
product_price = Product.select(:price).find(product_id).price
product_price * product_attrs['quantity'].to_i
product = Product.find(product_id)
product.price * product_attrs[:quantity.to_s].to_i
end
end
......
class OrderMailer < ApplicationMailer
def order_detail(user, order)
def send_order_detail_to_user(user, order)
@user = user
@order = order
mail to: user.email, subject: "Order detail"
mail to: user.email, subject: "Order detail #{@order.id}"
end
end
......@@ -2,13 +2,6 @@ class LineItem < ApplicationRecord
belongs_to :product, optional: true
belongs_to :order, optional: true
def create_line_items_from_cart(cart)
cart.each do |product_id, product_attrs|
LineItem.create(product_id: product_id,
quantity: product_attrs['quantity'])
end
end
def total_price
product.price * quantity
end
......
......@@ -24,7 +24,7 @@
<div class="product-actions">
<%= form_tag line_items_path(product_id: @product), remote: true do %>
<div class="product-detail-qty">
<%= number_field_tag :product_vqty, 1, min: 0, class: 'vertical-spinner' %>
<%= number_field_tag :product_quantity, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<% 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