summaryrefslogtreecommitdiff
path: root/app/queries/searches
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-16 14:49:41 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-16 14:49:41 -0600
commitc0e301c32d59ab10509b8532041cfd7f9fd0aec8 (patch)
tree456ac8a8d33aefe0fb3c84d1764f58e95639eee2 /app/queries/searches
parent62ebfcb0901002c1377d5677ed1a0d062ab94ff0 (diff)
Refactoriza ProductScopeFilter
Diffstat (limited to 'app/queries/searches')
-rw-r--r--app/queries/searches/scope_filters/product_scope_filter.rb22
1 files changed, 8 insertions, 14 deletions
diff --git a/app/queries/searches/scope_filters/product_scope_filter.rb b/app/queries/searches/scope_filters/product_scope_filter.rb
index d6f254d..941fdfd 100644
--- a/app/queries/searches/scope_filters/product_scope_filter.rb
+++ b/app/queries/searches/scope_filters/product_scope_filter.rb
@@ -14,51 +14,45 @@ module Searches
end
def order_by_price
- return self unless invalid_order_by_param(:price)
+ @scope = @scope.order(unitary_price: @params[:price].to_sym) if valid_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) if valid_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]}%") if @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) if valid_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]) if @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]}%") if @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])
+ def valid_order_by_param(param_symbol)
+ @params[param_symbol].present? && %w[asc desc].include?(@params[param_symbol])
end
end
end