Class: Spree::Products::Find

Inherits:
Object
  • Object
show all
Defined in:
app/finders/spree/products/find.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope:, params:) ⇒ Find

Returns a new instance of Find.



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
# File 'app/finders/spree/products/find.rb', line 4

def initialize(scope:, params:)
  @scope            = scope
  @query            = params[:q].presence&.strip
  @ids              = String(params.dig(:filter, :ids)).split(',')
  @skus             = String(params.dig(:filter, :skus)).split(',')
  @store            = params[:store] || Spree::Store.default
  @price            = map_prices(String(params.dig(:filter, :price)).split(','))
  @currency         = params.dig(:filter, :currency) || params[:currency] || Spree::Store.default.default_currency
  @taxons           = taxon_ids(params.dig(:filter, :taxons))
  @concat_taxons    = taxon_ids(params.dig(:filter, :concat_taxons))
  @taxonomies       = params.dig(:filter, :taxonomy_ids).to_h
  @name             = params.dig(:filter, :name)
  @slug             = params.dig(:filter, :slug)
  @options          = params.dig(:filter, :options).try(:to_unsafe_hash)
  @option_value_ids = params.dig(:filter, :option_value_ids)
  @sort_by          = params.dig(:sort_by)
  @deleted          = params.dig(:filter, :show_deleted)
  @discontinued     = params.dig(:filter, :show_discontinued)
  @properties       = params.dig(:filter, :properties)
  @in_stock         = params.dig(:filter, :in_stock)
  @backorderable    = params.dig(:filter, :backorderable)
  @purchasable      = params.dig(:filter, :purchasable)
  @out_of_stock     = params.dig(:filter, :out_of_stock).to_b
  @tags             = params.dig(:filter, :tags).to_s.split(',').compact_blank
  @vendor_ids       = params.dig(:filter, :vendor_ids)&.split(',')&.compact_blank || []

  if @purchasable.present? && @out_of_stock.present?
    @purchasable = false
    @out_of_stock = false
  end
end

Instance Method Details

#executeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/finders/spree/products/find.rb', line 36

def execute
  products = by_ids(scope)
  products = by_skus(products)
  products = by_query(products)
  products = include_discontinued(products)
  products = by_price(products)
  products = by_currency(products)
  products = by_taxons(products)
  products = by_concat_taxons(products)
  products = by_name(products)
  products = by_slug(products)
  products = by_options(products)
  products = by_option_value_ids(products)
  products = by_properties(products)
  products = by_tags(products)
  products = include_deleted(products)
  products = show_only_stock(products)
  products = show_only_backorderable(products)
  products = show_only_purchasable(products)
  products = show_only_out_of_stock(products)
  products = by_taxonomies(products)
  products = ordered(products)
  products = by_vendor_ids(products)

  products.distinct
end