Commit b24e4e5f by Tran Hoang Viet

VietTH: Implement send email after checkout

parent 048a291e
...@@ -42,6 +42,7 @@ group :development, :test do ...@@ -42,6 +42,7 @@ group :development, :test 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 'pry-rails', '~> 0.3.4' gem 'pry-rails', '~> 0.3.4'
gem 'letter_opener', '~> 1.4.1'
end end
# template # template
...@@ -62,4 +63,7 @@ gem 'draper', '~> 2.1.0' ...@@ -62,4 +63,7 @@ gem 'draper', '~> 2.1.0'
gem 'devise', '~> 3.5.1' gem 'devise', '~> 3.5.1'
gem 'carrierwave', '~> 0.10.0' gem 'carrierwave', '~> 0.10.0'
gem 'mini_magick', '~> 3.8.1' gem 'mini_magick', '~> 3.8.1'
\ No newline at end of file
# Background job
gem 'sidekiq', '~> 3.4.1'
\ No newline at end of file
...@@ -36,6 +36,7 @@ GEM ...@@ -36,6 +36,7 @@ GEM
minitest (~> 5.1) minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4) thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.3.8)
arel (6.0.0) arel (6.0.0)
bcrypt (3.1.10) bcrypt (3.1.10)
binding_of_caller (0.7.2) binding_of_caller (0.7.2)
...@@ -48,6 +49,8 @@ GEM ...@@ -48,6 +49,8 @@ GEM
activesupport (>= 3.2.0) activesupport (>= 3.2.0)
json (>= 1.7) json (>= 1.7)
mime-types (>= 1.16) mime-types (>= 1.16)
celluloid (0.16.0)
timers (~> 4.0.0)
coderay (1.1.0) coderay (1.1.0)
coffee-rails (4.1.0) coffee-rails (4.1.0)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
...@@ -57,6 +60,7 @@ GEM ...@@ -57,6 +60,7 @@ GEM
execjs execjs
coffee-script-source (1.9.1.1) coffee-script-source (1.9.1.1)
columnize (0.9.0) columnize (0.9.0)
connection_pool (2.2.0)
debug_inspector (0.0.2) debug_inspector (0.0.2)
devise (3.5.1) devise (3.5.1)
bcrypt (~> 3.0) bcrypt (~> 3.0)
...@@ -87,13 +91,14 @@ GEM ...@@ -87,13 +91,14 @@ GEM
haml (>= 4.0.6, < 5.0) haml (>= 4.0.6, < 5.0)
html2haml (>= 1.0.1) html2haml (>= 1.0.1)
railties (>= 4.0.1) railties (>= 4.0.1)
hitimes (1.2.2)
html2haml (2.0.0) html2haml (2.0.0)
erubis (~> 2.7.0) erubis (~> 2.7.0)
haml (~> 4.0.0) haml (~> 4.0.0)
nokogiri (~> 1.6.0) nokogiri (~> 1.6.0)
ruby_parser (~> 3.5) ruby_parser (~> 3.5)
i18n (0.7.0) i18n (0.7.0)
jbuilder (2.3.0) jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5) activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2) multi_json (~> 1.2)
jeff (1.3.0) jeff (1.3.0)
...@@ -104,6 +109,10 @@ GEM ...@@ -104,6 +109,10 @@ GEM
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.3) json (1.8.3)
kgio (2.9.3) kgio (2.9.3)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.4.1)
launchy (~> 2.2)
loofah (2.0.2) loofah (2.0.2)
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
mail (2.6.3) mail (2.6.3)
...@@ -114,7 +123,7 @@ GEM ...@@ -114,7 +123,7 @@ GEM
subexec (~> 0.2.1) subexec (~> 0.2.1)
mini_portile (0.6.2) mini_portile (0.6.2)
minitest (5.7.0) minitest (5.7.0)
multi_json (1.11.1) multi_json (1.11.2)
multi_xml (0.5.5) multi_xml (0.5.5)
mysql2 (0.3.18) mysql2 (0.3.18)
nokogiri (1.6.6.2) nokogiri (1.6.6.2)
...@@ -159,6 +168,9 @@ GEM ...@@ -159,6 +168,9 @@ GEM
rake (10.4.2) rake (10.4.2)
rdoc (4.2.0) rdoc (4.2.0)
json (~> 1.4) json (~> 1.4)
redis (3.2.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
request_store (1.1.0) request_store (1.1.0)
responders (2.1.0) responders (2.1.0)
railties (>= 4.2.0, < 5) railties (>= 4.2.0, < 5)
...@@ -176,6 +188,12 @@ GEM ...@@ -176,6 +188,12 @@ GEM
rdoc (~> 4.0) rdoc (~> 4.0)
settingslogic (2.0.9) settingslogic (2.0.9)
sexp_processor (4.6.0) sexp_processor (4.6.0)
sidekiq (3.4.1)
celluloid (~> 0.16.0)
connection_pool (>= 2.1.1)
json
redis (>= 3.0.6)
redis-namespace (>= 1.3.1)
slop (3.6.0) slop (3.6.0)
spring (1.3.6) spring (1.3.6)
sprockets (3.2.0) sprockets (3.2.0)
...@@ -188,6 +206,8 @@ GEM ...@@ -188,6 +206,8 @@ GEM
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.5) thread_safe (0.3.5)
tilt (1.4.1) tilt (1.4.1)
timers (4.0.1)
hitimes
turbolinks (2.5.3) turbolinks (2.5.3)
coffee-rails coffee-rails
twitter-bootstrap-rails (3.2.0) twitter-bootstrap-rails (3.2.0)
...@@ -229,6 +249,7 @@ DEPENDENCIES ...@@ -229,6 +249,7 @@ DEPENDENCIES
haml-rails (~> 0.9) haml-rails (~> 0.9)
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
letter_opener (~> 1.4.1)
mini_magick (~> 3.8.1) mini_magick (~> 3.8.1)
mysql2 mysql2
pry-rails (~> 0.3.4) pry-rails (~> 0.3.4)
...@@ -237,6 +258,7 @@ DEPENDENCIES ...@@ -237,6 +258,7 @@ DEPENDENCIES
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
settingslogic (~> 2.0.9) settingslogic (~> 2.0.9)
sidekiq (~> 3.4.1)
spring spring
turbolinks turbolinks
twitter-bootstrap-rails (~> 3.2.0) twitter-bootstrap-rails (~> 3.2.0)
......
...@@ -6,11 +6,6 @@ class ApplicationController < ActionController::Base ...@@ -6,11 +6,6 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception
protected protected
def params_permitted data = nil
data ||= self.class::PERMIT
params.require(data.keys.first).permit(data.values.first)
end
def add_breadcrumb(title, url = nil) def add_breadcrumb(title, url = nil)
@breadcrumbs = [] if @breadcrumbs.blank? @breadcrumbs = [] if @breadcrumbs.blank?
@breadcrumbs.push({title: title, url: url}) @breadcrumbs.push({title: title, url: url})
......
...@@ -24,12 +24,17 @@ class CartsController < ApplicationController ...@@ -24,12 +24,17 @@ class CartsController < ApplicationController
if cart.save if cart.save
clear_cart clear_cart
self.send_checkout_email(cart)
redirect_to root_path redirect_to root_path
else else
render :index render :index
end end
end end
def send_checkout_email(cart)
CartMailer.checkout(current_user.email, cart).deliver_later
end
private private
def cart_params def cart_params
params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity]) params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity])
......
...@@ -5,15 +5,13 @@ class ProductsController < ApplicationController ...@@ -5,15 +5,13 @@ class ProductsController < ApplicationController
before_action :set_categories, only: [:new] before_action :set_categories, only: [:new]
before_action :add_breadcrumb_home before_action :add_breadcrumb_home
PERMIT = { product: %i(title price category_id image) }
def new def new
@product = Product.new @product = Product.new
add_breadcrumb('New product') add_breadcrumb('New product')
end end
def create def create
@product = Product.new(params_permitted) @product = Product.new(product_params)
if @product.save if @product.save
redirect_to root_path redirect_to root_path
else else
...@@ -41,4 +39,8 @@ class ProductsController < ApplicationController ...@@ -41,4 +39,8 @@ class ProductsController < ApplicationController
@categories = Category.all.limit(Settings.limit_category) @categories = Category.all.limit(Settings.limit_category)
end end
def product_params
params.require(:product).permit(:title, :price, :category_id, :image)
end
end end
\ No newline at end of file
class CartMailer < ActionMailer::Base
default from: 'demo@example.com'
def checkout(email, cart)
@cart = cart
@cart_items = @cart.cart_items.includes(:product)
mail(
to: email,
subject: "Order number #{cart.id}"
)
end
end
\ No newline at end of file
%p= "Order ##{@cart.id}"
%p= "Total: #{@cart.total}"
%table
%thead
%tr
%td.text-right #
%td Title
%td Price
%td Quantity
%tbody
- @cart_items.each_with_index do |cart_item, index|
%tr
%td.number.text-right
= index + 1
%td.title
= link_to cart_item.product.title, product_path(cart_item.product)
%td.price
= cart_item.product.price
%td.quantity
= cart_item.product.quantity
\ No newline at end of file
...@@ -22,5 +22,7 @@ module VenShop ...@@ -22,5 +22,7 @@ module VenShop
# Do not swallow errors in after_commit/after_rollback callbacks. # Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
end end
end end
...@@ -40,4 +40,6 @@ Rails.application.configure do ...@@ -40,4 +40,6 @@ Rails.application.configure do
# config.action_view.raise_on_missing_translations = true # config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :letter_opener
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