From 9f23908f7f5ba7985cd9604e150eda19295127cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A9?= Date: Wed, 29 Sep 2021 16:56:13 +0200 Subject: [PATCH] p_product search + filter ok --- .../admin/p_products_controller.rb | 19 ++++++++--- app/models/p_product.rb | 10 ++++++ app/views/admin/p_products/index.html.haml | 34 ++++++++++++++----- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/app/controllers/admin/p_products_controller.rb b/app/controllers/admin/p_products_controller.rb index 9cb4915..ff04834 100644 --- a/app/controllers/admin/p_products_controller.rb +++ b/app/controllers/admin/p_products_controller.rb @@ -18,9 +18,13 @@ class Admin::PProductsController < ApplicationController params[:search] = params[:search] || {} + global_search_ids = p_product_cat_id_ids = s_brand_id_ids = @p_products.ids + + if params[:search][:global].to_s != "" + global_search_ids = PProduct.global_search(params[:search][:global]).ids + end params[:search][:enabled] = "Oui" if !params[:search][:enabled] - if params[:search][:enabled].to_s == "Oui" @p_products = @p_products.where(:enabled => true) elsif params[:search][:enabled].to_s == "Non" @@ -37,17 +41,23 @@ class Admin::PProductsController < ApplicationController if params[:search][:p_product_cat_id].to_s != "" if params[:search][:p_product_cat_id].to_s == "null" - @p_products = @p_products.where(:p_product_cat_id => nil) + # @p_products = @p_products.where(:p_product_cat_id => nil) + p_product_cat_id_ids = @p_product_refs.where(:cc_p_product_cat_id => nil).ids else - @p_products = @p_products.where(:p_product_cat_id => params[:search][:p_product_cat_id]) + # @p_products = @p_products.where(:p_product_cat_id => params[:search][:p_product_cat_id]) + p_product_cat_id_ids = @p_products.where(:p_product_cat_id => params[:search][:p_product_cat_id]).ids end end if params[:search][:s_brand_id].to_s != "" @p_products = @p_products.where(:s_brand_id => params[:search][:s_brand_id]) + s_brand_id_ids = @p_products.where(:s_brand_id => params[:search][:s_brand_id]).ids end - + + final_ids = global_search_ids & p_product_cat_id_ids & s_brand_id_ids + @p_products = PProduct.where(id: final_ids) + @p_products = sort_by_sorting(@p_products, "id DESC") respond_to do |format| format.html{ @@ -60,7 +70,6 @@ class Admin::PProductsController < ApplicationController } end - end def show diff --git a/app/models/p_product.rb b/app/models/p_product.rb index 893a83c..78ee662 100644 --- a/app/models/p_product.rb +++ b/app/models/p_product.rb @@ -70,6 +70,16 @@ class PProduct < ApplicationRecord } + include PgSearch::Model + pg_search_scope :global_search, + against: [:name, :code], + associated_against: { + p_product_refs: [:cc_name, :ref], + s_brand: [ :code, :name ], + }, + using: { + tsearch: { prefix: true } + } diff --git a/app/views/admin/p_products/index.html.haml b/app/views/admin/p_products/index.html.haml index ec874e5..2c89729 100644 --- a/app/views/admin/p_products/index.html.haml +++ b/app/views/admin/p_products/index.html.haml @@ -14,25 +14,41 @@ %table %tr - %td - Actif : - =select_tag "search[enabled]", options_for_select([[""], "Oui", "Non"], params[:search][:enabled]) + %td{style: "min-width: 500px;"} + .input-group + =text_field_tag "search[global]", params[:search][:global],:class => "form-control", :placeholder => "Marque, modèle, couleur, code..." + .input-group-append{:onclick => "$(this).prev('input').val('');"} + .btn.btn-outline-dark + =ic(:times) + %tr + -if false + %td + Actif : + =select_tag "search[enabled]", options_for_select([[""], "Oui", "Non"], params[:search][:enabled]) - %td=text_field_tag "search[code]", params[:search][:code],:class => "form-control", :placeholder => "Code" + %td=text_field_tag "search[code]", params[:search][:code],:class => "form-control", :placeholder => "Code" - %td=text_field_tag "search[name]", params[:search][:name],:class => "form-control", :placeholder => "Nom" + %td=text_field_tag "search[name]", params[:search][:name],:class => "form-control", :placeholder => "Nom" %table %tr %td Catégorie : - =select_tag "search[p_product_cat_id]", options_for_select([["",""],["Aucune","null"]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_cat_id]) - + %td + .input-group + =select_tag "search[p_product_cat_id]", options_for_select([["",""],["Aucune","null"]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_cat_id]), class: "custom-select" + .input-group-append{:onclick => "$(this).prev('select').val('').prop('selectedIndex',0);"} + .btn.btn-outline-dark + =ic(:times) %td Marque : - =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id]) - + %td + .input-group + =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id]), class: "custom-select" + .input-group-append{:onclick => "$(this).prev('select').val('').prop('selectedIndex',0);"} + .btn.btn-outline-dark + =ic(:times) =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_products}