Commit ccd4ffc7 by Hoang Phuc Do

Fix merge request #3

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