blob: 941fdfde5e1bcef3d539a13ce5fb3d15118041f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# 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
@scope = @scope.order(unitary_price: @params[:price].to_sym) if valid_order_by_param(:price)
self
end
def order_by_bulk_price
@scope = @scope.order(bulk_price: @params[:bulk_price].to_sym) if valid_order_by_param(:bulk_price)
self
end
def by_category
@scope = @scope.where("'categories' LIKE ?", "%#{@params[:category]}%") if @params[:category].present?
self
end
def order_by_available_quantity
@scope = @scope.order(available_quantity: @params[:quantity].to_sym) if valid_order_by_param(:quantity)
self
end
def by_company
@scope = @scope.where('companies.short_name = ?', @params[:company]) if @params[:company].present?
self
end
def by_name
@scope = @scope.where('LOWER(products.name) LIKE ?', "%#{@params[:name]}%") if @params[:name].present?
self
end
private
def valid_order_by_param(param_symbol)
@params[param_symbol].present? && %w[asc desc].include?(@params[param_symbol])
end
end
end
end
|