Skip to content

Commit e971bae

Browse files
forkatajarednormanadammathyssofiabesenski4
committed
Simplify sample product filter implementation
This cleans up some of the sample product filters provided to be a bit more performant and easier to read. Co-authored-by: Jared Norman <jared@super.gd> Co-authored-by: Adam Mueller <adam@super.gd> Co-authored-by: Sofia Besenski <sofia@super.gd>
1 parent c146c56 commit e971bae

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

core/lib/spree/core/product_filters.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ module ProductFilters
5454
# to below scope would be something like ["$10 - $15", "$15 - $18", "$18 -
5555
# $20"].
5656
Spree::Product.add_search_scope :price_range_any do |*opts|
57-
conds = opts.map { |element| Spree::Core::ProductFilters.price_filter[:conds][element] }.reject(&:nil?)
58-
scope = conds.shift
59-
conds.each do |new_scope|
60-
scope = scope.or(new_scope)
61-
end
57+
scope = opts.filter_map { |element|
58+
Spree::Core::ProductFilters.price_filter[:conds][element]
59+
}.inject { |scope1, scope2| scope1.or(scope2) }
6260

6361
Spree::Product.joins(master: :prices).where(scope)
6462
end
@@ -98,11 +96,10 @@ def self.price_filter
9896
# test for brand info being blank. Note that this relies on
9997
# `with_property` doing a left outer join rather than an inner join.
10098
Spree::Product.add_search_scope :brand_any do |*opts|
101-
conds = opts.map { |value| ProductFilters.brand_filter[:conds][value] }.reject(&:nil?)
102-
scope = conds.shift
103-
conds.each do |new_scope|
104-
scope = scope.or(new_scope)
105-
end
99+
scope = opts.filter_map { |value|
100+
ProductFilters.brand_filter[:conds][value]
101+
}.inject { |scope1, scope2| scope1.or(scope2) }
102+
106103
Spree::Product.with_property('brand').where(scope)
107104
end
108105

0 commit comments

Comments
 (0)