From 62ebfcb0901002c1377d5677ed1a0d062ab94ff0 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sun, 16 Apr 2023 13:02:26 -0600 Subject: Añade búsqueda de productos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../searches/scope_filters/product_scope_filter.rb | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 app/queries/searches/scope_filters/product_scope_filter.rb (limited to 'app/queries/searches/scope_filters/product_scope_filter.rb') diff --git a/app/queries/searches/scope_filters/product_scope_filter.rb b/app/queries/searches/scope_filters/product_scope_filter.rb new file mode 100644 index 0000000..d6f254d --- /dev/null +++ b/app/queries/searches/scope_filters/product_scope_filter.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Searches + module ScopeFilters + # ProductScopeFilter + class ProductScopeFilter + def initialize(scope, params) + @scope = scope + @params = params + end + + def query_result + @scope + end + + def order_by_price + return self unless invalid_order_by_param(:price) + + @scope = @scope.order(unitary_price: @params[:price].to_sym) + self + end + + def order_by_bulk_price + return self unless invalid_order_by_param(:bulk_price) + + @scope = @scope.order(bulk_price: @params[:bulk_price].to_sym) + self + end + + def by_category + return self unless @params[:category].present? + + @scope = @scope.where("'categories' LIKE ?", "%#{@params[:category]}%") + self + end + + def order_by_available_quantity + return self unless invalid_order_by_param(:quantity) + + @scope = @scope.order(available_quantity: @params[:quantity].to_sym) + self + end + + def by_company + return self unless @params[:company].present? + + @scope = @scope.where('companies.short_name = ?', "#{@params[:company]}") + self + end + + def by_name + return self unless @params[:name].present? + + @scope = @scope.where('LOWER(products.name) LIKE ?', "%#{@params[:name]}%") + self + end + + private + + def invalid_order_by_param(param_symbol) + @params[param_symbol].present? || %w[asc desc].include?(@params[param_symbol]) + end + end + end +end -- cgit v1.2.3