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