Commit 274b6899 by Hoang Phuc Do

Merge remote-tracking branch 'origin/development' into dhp_order

# Conflicts:
#	app/controllers/products_controller.rb
#	app/services/solr.rb
parents d1deeb42 9c71bd06
...@@ -8,14 +8,14 @@ class AdminController < ApplicationController ...@@ -8,14 +8,14 @@ class AdminController < ApplicationController
# GET /admin/orders # GET /admin/orders
def orders def orders
@orders = Order.all.page(params[:page]).per(5) @orders = Order.page(params[:page]).per(5)
end end
private private
def authenticate_admin_user! def authenticate_admin_user!
authenticate_user! authenticate_user!
unless current_user.admin? unless current_user.is_admin?
redirect_to root_path, notice: 'Unauthorized access!' redirect_to root_path, notice: 'Unauthorized access!'
end end
end end
......
...@@ -18,7 +18,7 @@ class ApplicationController < ActionController::Base ...@@ -18,7 +18,7 @@ class ApplicationController < ActionController::Base
def user_is_admin? def user_is_admin?
return false unless user_signed_in? return false unless user_signed_in?
unless current_user.admin? unless current_user.is_admin?
redirect_to root_url, notice: "You don't have access to this page" redirect_to root_url, notice: "You don't have access to this page"
end end
end end
......
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
def show def show
@category = Category.find(params[:id]) @category = Category.find(params[:id])
@products = @products = @category.products.page(params[:page]).per(5) @products = @category.products.page(params[:page]).per(5)
end end
end end
\ No newline at end of file
...@@ -5,7 +5,6 @@ class OrdersController < ApplicationController ...@@ -5,7 +5,6 @@ class OrdersController < ApplicationController
before_action :user_is_admin?, only: [:edit, :update] before_action :user_is_admin?, only: [:edit, :update]
before_action :set_order, only: [:show, :edit, :update] before_action :set_order, only: [:show, :edit, :update]
before_action :order_owner?, only: :show before_action :order_owner?, only: :show
before_action :set_order_items, only: :new
# GET /orders/1 # GET /orders/1
def show def show
...@@ -16,6 +15,7 @@ class OrdersController < ApplicationController ...@@ -16,6 +15,7 @@ class OrdersController < ApplicationController
def new def new
redirect_to root_url if @cart.blank? redirect_to root_url if @cart.blank?
@order = Order.new @order = Order.new
@order_items = @cart.product_items
end end
# POST /orders # POST /orders
...@@ -52,21 +52,16 @@ class OrdersController < ApplicationController ...@@ -52,21 +52,16 @@ class OrdersController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def order_params def order_params
default_params = { order_status: Order.default_order_status } params.require(:order).permit(:status)
params.require(:order).permit(:order_status).reverse_merge(default_params)
end end
def set_order def set_order
@order = Order.find(params[:id]) @order = Order.find(params[:id])
end end
def set_order_items
@order_items = @cart.product_items
end
def order_owner? def order_owner?
unless @order.user == current_user unless @order.user == current_user
redirect_to root_url, notice: "You don't have access to this order" return redirect_to root_url, notice: "You don't have access to this order"
end end
end end
end end
\ No newline at end of file
...@@ -2,6 +2,7 @@ class ProductsController < ApplicationController ...@@ -2,6 +2,7 @@ class ProductsController < ApplicationController
before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy] before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy]
before_action :set_product, only: :show before_action :set_product, only: :show
before_action :user_can_edit_product, only: [:edit, :update, :destroy] before_action :user_can_edit_product, only: [:edit, :update, :destroy]
before_action :set_solr, only: [:create, :update, :destroy ]
# GET /products/new # GET /products/new
def new def new
...@@ -12,6 +13,7 @@ class ProductsController < ApplicationController ...@@ -12,6 +13,7 @@ class ProductsController < ApplicationController
def create def create
@product = Product.new(product_params.merge(user_id: current_user.id)) @product = Product.new(product_params.merge(user_id: current_user.id))
if @product.save if @product.save
@solr.add_product(@product)
redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully created" } redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully created" }
else else
render 'new' render 'new'
...@@ -21,6 +23,7 @@ class ProductsController < ApplicationController ...@@ -21,6 +23,7 @@ class ProductsController < ApplicationController
# PATCH/PUT /products/1 # PATCH/PUT /products/1
def update def update
if @product.update(product_params) if @product.update(product_params)
@solr.update_product(@product)
redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully updated" } redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully updated" }
else else
render 'edit' render 'edit'
...@@ -30,6 +33,7 @@ class ProductsController < ApplicationController ...@@ -30,6 +33,7 @@ class ProductsController < ApplicationController
# DELETE /products/1 # DELETE /products/1
def destroy def destroy
if @product.destroy if @product.destroy
@solr.delete_product(@product)
flash[:success] = "Product #{@product.title} deleted" flash[:success] = "Product #{@product.title} deleted"
else else
flash[:alert] = "Product #{@product.title} can't be deleted" flash[:alert] = "Product #{@product.title} can't be deleted"
...@@ -56,4 +60,8 @@ class ProductsController < ApplicationController ...@@ -56,4 +60,8 @@ class ProductsController < ApplicationController
@product = current_user.products.find_by(id: params[:id]) @product = current_user.products.find_by(id: params[:id])
redirect_to root_url, flash: { alert: 'You do not have permission to edit this product' } if @product.blank? redirect_to root_url, flash: { alert: 'You do not have permission to edit this product' } if @product.blank?
end end
def set_solr
@solr = Solr.new
end
end end
\ No newline at end of file
module ApplicationHelper
def product_categories
Category.all
end
end
require 'active_support/concern'
module SolrProduct
extend ActiveSupport::Concern
included do
attr_accessor :solr
before_commit { self.solr = Solr.new }
after_create_commit :add_product_to_solr
after_update_commit :update_product_in_solr
after_destroy_commit :delete_product_in_solr
end
def add_product_to_solr
solr.add_product(self)
end
def update_product_in_solr
solr.update_product(self)
end
def delete_product_in_solr
solr.delete_product(self)
end
end
\ No newline at end of file
class Order < ApplicationRecord class Order < ApplicationRecord
has_many :product_items, dependent: :destroy has_many :product_items, dependent: :destroy
belongs_to :user belongs_to :user
enum order_status: { 'Pending' => 0, 'Done' => 1 } enum status: { 'Pending' => 0, 'Done' => 1 }
validates :order_status, presence: true validates :status, presence: true
validate :validate_pending_order, on: :create validate :validate_status, on: :create
def self.default_order_status def self.default_status
'Pending' 'Pending'
end end
def validate_pending_order def validate_status
errors.add(:base, 'Error') if order_status != 'Pending' errors.add(:order_status, 'Error') if status != self.status['Pending']
end end
def total_price def total_price
......
class Product < ApplicationRecord class Product < ApplicationRecord
include SolrProduct
belongs_to :category belongs_to :category
belongs_to :user belongs_to :user
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<% @orders.each do |order| %> <% @orders.each do |order| %>
<tr> <tr>
<td><%= link_to "##{order.id}", order_url(order) %></td> <td><%= link_to "##{order.id}", order_url(order) %></td>
<td><%= order.order_status %></td> <td><%= order.status %></td>
<td><%= order.created_at.strftime('%d-%M-%Y %H:%m') %></td> <td><%= order.created_at.strftime('%d-%M-%Y %H:%m') %></td>
<td><%= number_to_currency(order.total_price) %></td> <td><%= number_to_currency(order.total_price) %></td>
<td><%= link_to fa_icon('pencil'), edit_order_path(order) %></td> <td><%= link_to fa_icon('pencil'), edit_order_path(order) %></td>
......
<div class="custom-block"> <div class="custom-block">
<% if user_signed_in? %> <% if user_signed_in? %>
<% if current_user.admin? %> <% if current_user.is_admin? %>
<%= link_to 'SITE ADMIN', admin_index_path %> <%= link_to 'SITE ADMIN', admin_index_path %>
<span class="split"></span> <span class="split"></span>
<% end %> <% end %>
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<div class="form-group"> <div class="form-group">
<%= f.label :order_status %> <%= f.label :status %>
<%= f.select :order_status, Order.order_statuses.keys, {}, class: 'form-control' %> <%= f.select :status, Order.statuses.keys, {}, class: 'form-control' %>
</div> </div>
</div> </div>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<thead> <thead>
<tr> <tr>
<td colspan="2" class="text-left"> <td colspan="2" class="text-left">
Order Detail - <span><%= order.order_status %></span> Order Detail - <span><%= order.status %></span>
</td> </td>
</tr> </tr>
<tbody> <tbody>
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
<div class="checkout-review-action"> <div class="checkout-review-action">
<h5>Grand Total <span><%= number_to_currency(cart_total_price) %></span></h5> <h5>Grand Total <span><%= number_to_currency(cart_total_price) %></span></h5>
<%= form_for @order do |f| %> <%= form_for @order do |f| %>
<%= hidden_field_tag 'order[create_order]' %> <%= hidden_field_tag 'order[create_order]' %>
<%= f.submit 'Place Order now', class: 'btn btn-primary' %> <%= f.submit 'Place Order now', class: 'btn btn-primary' %>
<% end %> <% end %>
</div> </div>
......
<div class="panel-group"> <div class="panel-group">
<%= render 'shared/widgets/product_categories' %> <%= render 'shared/widgets/categories' %>
</div> </div>
\ No newline at end of file
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<div id="panel-filter-category"> <div id="panel-filter-category">
<div class="panel-body"> <div class="panel-body">
<ul> <ul>
<% product_categories.each do |category| %> <% Category.all.each do |category| %>
<%= active_link_to category.title, category_url(category), wrap_tag: :li %> <%= active_link_to category.title, category_url(category), wrap_tag: :li %>
<% end %> <% end %>
</ul> </ul>
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<% orders.each do |order| %> <% orders.each do |order| %>
<tr> <tr>
<td><%= link_to "##{order.id}", order_url(order) %></td> <td><%= link_to "##{order.id}", order_url(order) %></td>
<td><%= order.order_status %></td> <td><%= order.status %></td>
<td><%= order.created_at.strftime('%d-%M-%Y %H:%m') %></td> <td><%= order.created_at.strftime('%d-%M-%Y %H:%m') %></td>
<td><%= number_to_currency(order.total_price) %></td> <td><%= number_to_currency(order.total_price) %></td>
</tr> </tr>
......
# RSolr
rsolr:
address: http://localhost:8983/solr/dhp_venshop
\ No newline at end of file
class AddOrderStatusToOrders < ActiveRecord::Migration[5.1] class AddOrderStatusToOrders < ActiveRecord::Migration[5.1]
def change def change
add_column :orders, :order_status, :integer, null: false, default: 0 add_column :orders, :status, :integer, null: false, default: 0
end end
end end
class AddAdminToUsers < ActiveRecord::Migration[5.1] class AddAdminToUsers < ActiveRecord::Migration[5.1]
def change def change
add_column :users, :admin, :boolean, null: false, default: false add_column :users, :is_admin, :boolean, null: false, default: false
end end
end end
...@@ -21,7 +21,8 @@ ActiveRecord::Schema.define(version: 20170623064632) do ...@@ -21,7 +21,8 @@ ActiveRecord::Schema.define(version: 20170623064632) do
t.bigint "user_id" t.bigint "user_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "order_status", null: false, default: 0 t.integer "status"
t.integer "status", default: 0, null: false
t.index ["user_id"], name: "index_orders_on_user_id" t.index ["user_id"], name: "index_orders_on_user_id"
end end
...@@ -67,6 +68,7 @@ ActiveRecord::Schema.define(version: 20170623064632) do ...@@ -67,6 +68,7 @@ ActiveRecord::Schema.define(version: 20170623064632) do
t.string "first_name" t.string "first_name"
t.string "last_name" t.string "last_name"
t.boolean "admin", default: false, null: false t.boolean "admin", default: false, null: false
t.boolean "is_admin", default: false, null: false
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
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