Vietth feature product management
{{ resolvedDiscussionCount }}/{{ discussionCount }} {{ resolvedCountText }} resolved
-
app/controllers/categories_controller.rb 0 → 100644
1 class CategoriesController < ApplicationController 2 before_action :set_category, only: [:show] 3 4 def show 5 @products = @category.products.page(params[:page]) 6 end 7 8 private 9 def set_category 10 @category = Category.find_by(id: params[:id]) -
1 1 module ApplicationHelper 2 def session_cart 3 (session[:cart_info] ||= {items: [], total: 0}).with_indifferent_access -
app/controllers/products_controller.rb 0 → 100644
13 14 def create 15 @product = Product.new(product_params) 16 if @product.save 17 redirect_to root_path 18 else 19 render :new 20 end 21 end 22 23 def show 24 add_breadcrumb(@product.category.decorate.title, category_path(@product.category)) 25 add_breadcrumb(@product.title) 26 end 27 28 def add_cart -
app/controllers/carts_controller.rb 0 → 100644
1 class CartsController < ApplicationController 2 include CartsManagement 3 4 before_action :authenticate_user! 5 6 def index 7 @cart = Cart.new 8 end 9 10 def create 11 cart = current_user.carts.build(cart_params) 12 13 product_ids = [] 14 quantites = {} 15 cart_params['cart_items_attributes'].each do |item| -
app/controllers/carts_controller.rb 0 → 100644
7 @cart = Cart.new 8 end 9 10 def create 11 cart = current_user.carts.build(cart_params) 12 13 product_ids = [] 14 quantites = {} 15 cart_params['cart_items_attributes'].each do |item| 16 id = item.last['product_id'] 17 product_ids << id 18 quantites[id] = item.last['quantity'] 19 end 20 21 products = Product.where(id: [product_ids]).select(:id, :price) 22 total = products.inject(0) do |sum, product| -
app/controllers/carts_controller.rb 0 → 100644
21 products = Product.where(id: [product_ids]).select(:id, :price) 22 total = products.inject(0) do |sum, product| 23 sum + (product.price.to_i * quantites[product.id.to_s].to_i) 24 end 25 cart.total = total 26 27 if cart.save 28 clear_cart 29 self.send_checkout_email(cart) 30 redirect_to root_path 31 else 32 render :index 33 end 34 end 35 36 def send_checkout_email(cart) -
app/controllers/products_controller.rb 0 → 100644
20 end 21 end 22 23 def show 24 add_breadcrumb(@product.category.decorate.title, category_path(@product.category)) 25 add_breadcrumb(@product.title) 26 end 27 28 def add_cart 29 add_to_cart @product 30 redirect_to @product 31 end 32 33 private 34 def set_product 35 @product = Product.find_by(id: params[:id]) -
app/controllers/categories_controller.rb 0 → 100644
1 class CategoriesController < ApplicationController 2 before_action :set_category, only: [:show] 3 4 def show 5 @products = @category.products.page(params[:page]) 6 end 7 8 private 9 def set_category 10 @category = Category.find_by(id: params[:id]) -
Master
Fixed
-
-
1 1 module ApplicationHelper 2 def session_cart 3 (session[:cart_info] ||= {items: [], total: 0}).with_indifferent_access -
Master
All info cart is stored in session.
-
-
app/controllers/products_controller.rb 0 → 100644
13 14 def create 15 @product = Product.new(product_params) 16 if @product.save 17 redirect_to root_path 18 else 19 render :new 20 end 21 end 22 23 def show 24 add_breadcrumb(@product.category.decorate.title, category_path(@product.category)) 25 add_breadcrumb(@product.title) 26 end 27 28 def add_cart -
Master
It's create product, not cart_item. Cart_item use nested_attributes to create
-
-
app/controllers/carts_controller.rb 0 → 100644
1 class CartsController < ApplicationController 2 include CartsManagement 3 4 before_action :authenticate_user! 5 6 def index 7 @cart = Cart.new 8 end 9 10 def create 11 cart = current_user.carts.build(cart_params) 12 13 product_ids = [] 14 quantites = {} 15 cart_params['cart_items_attributes'].each do |item| -
Master
Logic is removed to service.
-
-
app/controllers/carts_controller.rb 0 → 100644
7 @cart = Cart.new 8 end 9 10 def create 11 cart = current_user.carts.build(cart_params) 12 13 product_ids = [] 14 quantites = {} 15 cart_params['cart_items_attributes'].each do |item| 16 id = item.last['product_id'] 17 product_ids << id 18 quantites[id] = item.last['quantity'] 19 end 20 21 products = Product.where(id: [product_ids]).select(:id, :price) 22 total = products.inject(0) do |sum, product| -
Master
Logic is removed to service.
-
-
app/controllers/carts_controller.rb 0 → 100644
21 products = Product.where(id: [product_ids]).select(:id, :price) 22 total = products.inject(0) do |sum, product| 23 sum + (product.price.to_i * quantites[product.id.to_s].to_i) 24 end 25 cart.total = total 26 27 if cart.save 28 clear_cart 29 self.send_checkout_email(cart) 30 redirect_to root_path 31 else 32 render :index 33 end 34 end 35 36 def send_checkout_email(cart) -
Master
Fixed
-
-
app/controllers/products_controller.rb 0 → 100644
20 end 21 end 22 23 def show 24 add_breadcrumb(@product.category.decorate.title, category_path(@product.category)) 25 add_breadcrumb(@product.title) 26 end 27 28 def add_cart 29 add_to_cart @product 30 redirect_to @product 31 end 32 33 private 34 def set_product 35 @product = Product.find_by(id: params[:id]) -
Master
Fixed
-
-
Status changed to merged
Toggle commit list