Commit b669e37f by vulehuan

fix code review post on gitlab (if ! ~ unless, .nil? --> .blank?, Hash.new ->…

fix code review post on gitlab (if ! ~ unless, .nil? --> .blank?, Hash.new -> {}, has_many + plural)
parent dd62a366
...@@ -2,14 +2,14 @@ class CardsController < ApplicationController ...@@ -2,14 +2,14 @@ class CardsController < ApplicationController
add_breadcrumb "Cards", :cards_path add_breadcrumb "Cards", :cards_path
def index def index
@card_infos = Hash.new @card_infos = {}
if !session[:SHOPPING_CARD_SESSION_NAME].nil? unless session[:SHOPPING_CARD_SESSION_NAME].blank?
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if !params[:product_id].nil? unless params[:product_id].blank?
if Product.exists?(params[:product_id]) if Product.exists?(params[:product_id])
# if remove a product from card # if remove a product from card
if !params[:card_action].nil? && params[:card_action] == 'remove' if !params[:card_action].blank? && params[:card_action] == 'remove'
# Nothing to delete # Nothing to delete
if @card_infos.empty? if @card_infos.empty?
redirect_to cards_path and return redirect_to cards_path and return
...@@ -24,11 +24,11 @@ class CardsController < ApplicationController ...@@ -24,11 +24,11 @@ class CardsController < ApplicationController
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
found = true found = true
end end
flash[:error] = "Invalid request (product not exist in card)" if !found flash[:error] = "Invalid request (product not exist in card)" unless found
redirect_to cards_path and return redirect_to cards_path and return
end end
end end
elsif !params[:card_action].nil? && params[:card_action] == 'update' elsif !params[:card_action].blank? && params[:card_action] == 'update'
# Nothing to update # Nothing to update
if @card_infos.empty? if @card_infos.empty?
flash[:error] = "Invalid request" flash[:error] = "Invalid request"
...@@ -36,7 +36,7 @@ class CardsController < ApplicationController ...@@ -36,7 +36,7 @@ class CardsController < ApplicationController
else else
quantity = params[:quantity] quantity = params[:quantity]
# Invalid request # Invalid request
if quantity.nil? || quantity.to_i <= 0 if quantity.blank? || quantity.to_i <= 0
flash[:error] = "Invalid quantity" flash[:error] = "Invalid quantity"
redirect_to cards_path and return redirect_to cards_path and return
else else
...@@ -60,7 +60,7 @@ class CardsController < ApplicationController ...@@ -60,7 +60,7 @@ class CardsController < ApplicationController
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
card_items = Array.new card_items = Array.new
card_items.push({ product_id: params[:product_id], quantity: 1 }) card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = Hash.new customer_info = {}
@card_infos = { card_items: card_items, customer_info: customer_info } @card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path and return redirect_to cards_path and return
...@@ -75,7 +75,7 @@ class CardsController < ApplicationController ...@@ -75,7 +75,7 @@ class CardsController < ApplicationController
end end
end end
# if a product not exist in card # if a product not exist in card
if !found unless found
card_items.push({ product_id: params[:product_id], quantity: 1 }) card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = @card_infos[:customer_info] customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info } @card_infos = { card_items: card_items, customer_info: customer_info }
...@@ -93,8 +93,8 @@ class CardsController < ApplicationController ...@@ -93,8 +93,8 @@ class CardsController < ApplicationController
def checkout def checkout
add_breadcrumb "Checkout", url_for(action: 'checkout') add_breadcrumb "Checkout", url_for(action: 'checkout')
@card_infos = Hash.new @card_infos = {}
if !session[:SHOPPING_CARD_SESSION_NAME].nil? unless session[:SHOPPING_CARD_SESSION_NAME].blank?
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if @card_infos.empty? || @card_infos[:card_items].empty? if @card_infos.empty? || @card_infos[:card_items].empty?
...@@ -103,7 +103,7 @@ class CardsController < ApplicationController ...@@ -103,7 +103,7 @@ class CardsController < ApplicationController
end end
if request.post? if request.post?
@errors = Array.new @errors = Array.new
customer_info = Hash.new customer_info = {}
if params[:full_name].empty? if params[:full_name].empty?
@errors.push("Full name is required") @errors.push("Full name is required")
end end
...@@ -143,8 +143,8 @@ class CardsController < ApplicationController ...@@ -143,8 +143,8 @@ class CardsController < ApplicationController
def confirm_checkout def confirm_checkout
add_breadcrumb "Check out", url_for(action: 'checkout') add_breadcrumb "Check out", url_for(action: 'checkout')
add_breadcrumb "Confirm", url_for(action: 'confirm_checkout') add_breadcrumb "Confirm", url_for(action: 'confirm_checkout')
@card_infos = Hash.new @card_infos = {}
if !session[:SHOPPING_CARD_SESSION_NAME].nil? unless session[:SHOPPING_CARD_SESSION_NAME].blank?
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if @card_infos.empty? || @card_infos[:card_items].empty? || @card_infos[:customer_info].empty? if @card_infos.empty? || @card_infos[:card_items].empty? || @card_infos[:customer_info].empty?
...@@ -183,7 +183,7 @@ class CardsController < ApplicationController ...@@ -183,7 +183,7 @@ class CardsController < ApplicationController
end end
def thankyou def thankyou
if params[:card_id].nil? || !MyCard.exists?(params[:card_id]) if params[:card_id].blank? || !MyCard.exists?(params[:card_id])
flash[:error] = "Invalid card request" flash[:error] = "Invalid card request"
redirect_to root_path and return redirect_to root_path and return
end end
......
...@@ -9,14 +9,14 @@ class ProductsController < ApplicationController ...@@ -9,14 +9,14 @@ class ProductsController < ApplicationController
def show def show
begin begin
@product = Product.find(params[:id]) @product = Product.find(params[:id])
if !@product.status unless @product.status
user = current_user user = current_user
if user.nil? || @product.user_id != user.id if user.blank? || @product.user_id != user.id
redirect_to products_path redirect_to products_path
end end
end end
product_category = ProductCategory.find(@product.product_category_id) product_category = ProductCategory.find(@product.product_category_id)
if !product_category.nil? unless product_category.blank?
add_breadcrumb product_category.name, product_category_path(product_category) add_breadcrumb product_category.name, product_category_path(product_category)
add_breadcrumb @product.name, product_path(@product) add_breadcrumb @product.name, product_path(@product)
end end
...@@ -49,13 +49,13 @@ class ProductsController < ApplicationController ...@@ -49,13 +49,13 @@ class ProductsController < ApplicationController
def user_items def user_items
add_breadcrumb "My items", url_for(action: 'user_items') add_breadcrumb "My items", url_for(action: 'user_items')
user = current_user user = current_user
all_status = !user.nil? && user.id.to_s == params[:user_id].to_s all_status = !user.blank? && user.id.to_s == params[:user_id].to_s
@products = Product.get_user_items(limit: 16, page: params[:page], user_id: params[:user_id], all_status: all_status) @products = Product.get_user_items(limit: 16, page: params[:page], user_id: params[:user_id], all_status: all_status)
end end
def search def search
add_breadcrumb "Search results", url_for(action: 'search') add_breadcrumb "Search results", url_for(action: 'search')
if params[:keyword].nil? if params[:keyword].blank?
redirect_to products_path redirect_to products_path
end end
search = Product.search do search = Product.search do
......
...@@ -77,7 +77,7 @@ class Product < ActiveRecord::Base ...@@ -77,7 +77,7 @@ class Product < ActiveRecord::Base
query = Product.select('id, name, image_medium, price, price_currency') query = Product.select('id, name, image_medium, price, price_currency')
.where(availability: 'instock') .where(availability: 'instock')
.where(user_id: user_id) .where(user_id: user_id)
if !options[:all_status] unless options[:all_status]
query = query.where(status: true) query = query.where(status: true)
end end
query = query.paginate(:page => page, :per_page => limit).order('created_at DESC').order('updated_at DESC').order('name') query = query.paginate(:page => page, :per_page => limit).order('created_at DESC').order('updated_at DESC').order('name')
......
class ProductCategory < ActiveRecord::Base class ProductCategory < ActiveRecord::Base
has_many :product has_many :products
end end
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_many :product has_many :products
before_save { self.email = email.downcase } before_save { self.email = email.downcase }
before_create :create_remember_token before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 } validates :name, presence: true, length: { maximum: 50 }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<h2 class="sprite-2"><%= @product.name %><span class="sprite-2"></span></h2> <h2 class="sprite-2"><%= @product.name %><span class="sprite-2"></span></h2>
<div class="text-justify product-detail"> <div class="text-justify product-detail">
<div id="block-message-visible" class="hidden"></div> <div id="block-message-visible" class="hidden"></div>
<% if !@product.status %> <% unless @product.status %>
<div class="alert alert-danger"> <div class="alert alert-danger">
<p class="text-center">This product is only visible to you.</p> <p class="text-center">This product is only visible to you.</p>
</div> </div>
......
<% <%
if items.nil? || items.empty? if items.blank?
if params[:action] != 'search' if params[:action] != 'search'
%> %>
<p>No products available.</p> <p>No products available.</p>
......
require 'sunspot' require 'sunspot'
require 'sunspot_matchers' require 'sunspot_matchers'
require 'rspec'
require 'spec_helper' require 'spec_helper'
describe "Product Search" do describe "Product Search" do
......
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