Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

refactor presenting Price Filters

Łukasz Adamczykda01d50e6f0f
diff --git a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6 b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6
index d7fc3f86c2..77bb64e375 100644
--- a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6
+++ b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6
@@ -14,7 +14,7 @@ Spree.ready(function() {
priceInput.addEventListener('change', () => {
this.updatePricesForFiltering(
parseInt(this.minPriceInput.value) || 0,
- parseInt(this.maxPriceInput.value) || 0
+ parseInt(this.maxPriceInput.value) || 'Infinity'
)
})
})
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index 72cdaf47f5..292d10a014 100644
--- a/frontend/app/helpers/spree/frontend_helper.rb
+++ b/frontend/app/helpers/spree/frontend_helper.rb
@@ -227,17 +227,6 @@ module Spree
inline_svg_tag "#{name}.svg", class: "spree-icon #{classes}", size: "#{width}px*#{height}px"
end
- def price_filters
- @price_filters ||= [
- less_than_price_range(50),
- price_range(50, 100),
- price_range(101, 150),
- price_range(151, 200),
- price_range(201, 300),
- more_than_price_range(300)
- ]
- end
-
def price_filter_values
@price_filter_values ||= [
"#{I18n.t('activerecord.attributes.spree/product.less_than')} #{formatted_price(50)}",
diff --git a/frontend/app/helpers/spree/products_filters_helper.rb b/frontend/app/helpers/spree/products_filters_helper.rb
index 05ec2f4d43..32321ff79e 100644
--- a/frontend/app/helpers/spree/products_filters_helper.rb
+++ b/frontend/app/helpers/spree/products_filters_helper.rb
@@ -1,9 +1,20 @@
module Spree
module ProductsFiltersHelper
+ def price_filters
+ @price_filters ||= [
+ filters_less_than_price_range(50),
+ filters_price_range(50, 100),
+ filters_price_range(101, 150),
+ filters_price_range(151, 200),
+ filters_price_range(201, 300),
+ filters_more_than_price_range(300)
+ ]
+ end
+
def min_price_filter_input(**html_options)
price_filter_input(
name: :min_price,
- value: prices.first,
+ value: filters_price_range_from_param.min_price.to_i,
placeholder: "#{currency_symbol(current_currency)} #{Spree.t(:min)}",
**html_options
)
@@ -12,7 +23,7 @@ module Spree
def max_price_filter_input(**html_options)
price_filter_input(
name: :max_price,
- value: prices.last,
+ value: filters_price_range_from_param.max_price.to_i,
placeholder: "#{currency_symbol(current_currency)} #{Spree.t(:max)}",
**html_options
)
@@ -33,10 +44,27 @@ module Spree
private
- def prices
- price_param = params[:price].to_s
- split_prices = price_param.split('-')
- split_prices.map { |price| price.to_money.to_i }
+ def filters_price_range_from_param
+ Filters::PriceRange.from_param(params[:price].to_s, currency: current_currency)
+ end
+
+ def filters_price_range(min_amount, max_amount)
+ Filters::PriceRange.new(
+ min_price: filters_price(min_amount),
+ max_price: filters_price(max_amount)
+ )
+ end
+
+ def filters_less_than_price_range(amount)
+ Filters::QuantifiedPriceRange.new(price: filters_price(amount), quantifier: :less_than)
+ end
+
+ def filters_more_than_price_range(amount)
+ Filters::QuantifiedPriceRange.new(price: filters_price(amount), quantifier: :more_than)
+ end
+
+ def filters_price(amount)
+ Filters::Price.new(amount: amount, currency: current_currency)
end
end
end
diff --git a/frontend/app/models/spree/filters/less_than_price_range.rb b/frontend/app/models/spree/filters/less_than_price_range.rb
deleted file mode 100644
index 1b838c5ac0..0000000000
--- a/frontend/app/models/spree/filters/less_than_price_range.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Spree
- module Filters
- class LessThanPriceRange
- def initialize(price:)
- @price = price
- end
-
- def to_param
- "0-#{price.to_i}"
- end
-
- def to_s
- "#{I18n.t('activerecord.attributes.spree/product.less_than')} #{price}"
- end
-
- private
-
- attr_reader :price
- end
- end
-end
diff --git a/frontend/app/models/spree/filters/more_than_price_range.rb b/frontend/app/models/spree/filters/more_than_price_range.rb
deleted file mode 100644
index 9d5ad2bb11..0000000000
--- a/frontend/app/models/spree/filters/more_than_price_range.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Spree
- module Filters
- class MoreThanPriceRange
- def initialize(price:)
- @price = price
- end
-
- def to_param
- "#{price.to_i}-0"
- end
-
- def to_s
- "#{I18n.t('activerecord.attributes.spree/product.more_than')} #{price}"
- end
-
- private
-
- attr_reader :price
- end
- end
-end
diff --git a/frontend/app/models/spree/filters/price.rb b/frontend/app/presenters/spree/filters/price.rb
similarity index 100%
rename from frontend/app/models/spree/filters/price.rb
rename to frontend/app/presenters/spree/filters/price.rb
diff --git a/frontend/app/models/spree/filters/price_range.rb b/frontend/app/presenters/spree/filters/price_range.rb
similarity index 58%
rename from frontend/app/models/spree/filters/price_range.rb
rename to frontend/app/presenters/spree/filters/price_range.rb
index ca743b3267..64a2f2270d 100644
--- a/frontend/app/models/spree/filters/price_range.rb
+++ b/frontend/app/presenters/spree/filters/price_range.rb
@@ -1,11 +1,22 @@
module Spree
module Filters
class PriceRange
+ def self.from_param(param, currency:)
+ prices = param.split('-')
+
+ new(
+ min_price: Price.new(amount: prices.first, currency: currency),
+ max_price: Price.new(amount: prices.last, currency: currency)
+ )
+ end
+
def initialize(min_price:, max_price:)
@min_price = min_price
@max_price = max_price
end
+ attr_reader :min_price, :max_price
+
def to_param
"#{min_price.to_i}-#{max_price.to_i}"
end
@@ -13,10 +24,6 @@ module Spree
def to_s
"#{min_price} - #{max_price}"
end
-
- private
-
- attr_reader :min_price, :max_price
end
end
end
diff --git a/frontend/app/presenters/spree/filters/quantified_price_range.rb b/frontend/app/presenters/spree/filters/quantified_price_range.rb
new file mode 100644
index 0000000000..ac425a9be1
--- /dev/null
+++ b/frontend/app/presenters/spree/filters/quantified_price_range.rb
@@ -0,0 +1,44 @@
+module Spree
+ module Filters
+ class QuantifiedPriceRange
+ ALLOWED_QUANTIFIERS = [
+ :less_than,
+ :more_than
+ ].freeze
+
+ def initialize(price:, quantifier:)
+ if ALLOWED_QUANTIFIERS.exclude?(quantifier.to_sym)
+ raise ArgumentError, "quantifier must be one of: #{ALLOWED_QUANTIFIERS.join(', ')}"
+ end
+
+ @price = price
+ @quantifier = quantifier.to_sym
+ end
+
+ def to_param
+ case quantifier
+ when :less_than
+ less_than_param
+ when :more_than
+ more_than_param
+ end
+ end
+
+ def to_s
+ "#{I18n.t("activerecord.attributes.spree/product.#{quantifier}")} #{price}"
+ end
+
+ private
+
+ attr_reader :price, :quantifier
+
+ def less_than_param
+ "0-#{price.to_i}"
+ end
+
+ def more_than_param
+ "#{price.to_i}-Infinity"
+ end
+ end
+ end
+end
diff --git a/frontend/spec/models/spree/filters/less_than_price_range_spec.rb b/frontend/spec/models/spree/filters/less_than_price_range_spec.rb
deleted file mode 100644
index 44269833d9..0000000000
--- a/frontend/spec/models/spree/filters/less_than_price_range_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-require 'spec_helper'
-
-module Spree
- module Filters
- RSpec.describe LessThanPriceRange do
- let(:price_range) { described_class.new(price: price) }
- let(:price) { Price.new(amount: 50, currency: 'USD') }
-
- describe '#to_param' do
- it 'returns price range as param' do
- expect(price_range.to_param).to eq('0-50')
- end
- end
-
- describe '#to_s' do
- it 'returns a formatted price range' do
- expect(price_range.to_s).to eq('Less than $50')
- end
- end
- end
- end
-end
diff --git a/frontend/spec/models/spree/filters/more_than_price_range_spec.rb b/frontend/spec/models/spree/filters/more_than_price_range_spec.rb
deleted file mode 100644
index 7cfd6a721c..0000000000
--- a/frontend/spec/models/spree/filters/more_than_price_range_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-require 'spec_helper'
-
-module Spree
- module Filters
- RSpec.describe MoreThanPriceRange do
- let(:price_range) { described_class.new(price: price) }
- let(:price) { Price.new(amount: 50, currency: 'USD') }
-
- describe '#to_param' do
- it 'returns price range as param' do
- expect(price_range.to_param).to eq('50-0')
- end
- end
-
- describe '#to_s' do
- it 'returns a formatted price range' do
- expect(price_range.to_s).to eq('More than $50')
- end
- end
- end
- end
-end
diff --git a/frontend/spec/models/spree/filters/price_range_spec.rb b/frontend/spec/presenters/spree/filters/price_range_spec.rb
similarity index 71%
rename from frontend/spec/models/spree/filters/price_range_spec.rb
rename to frontend/spec/presenters/spree/filters/price_range_spec.rb
index 28720fe3af..506ca28a21 100644
--- a/frontend/spec/models/spree/filters/price_range_spec.rb
+++ b/frontend/spec/presenters/spree/filters/price_range_spec.rb
@@ -8,6 +8,14 @@ module Spree
let(:min_price) { Price.new(amount: 50, currency: 'USD') }
let(:max_price) { Price.new(amount: 100, currency: 'USD') }
+ describe '.from_param' do
+ subject(:price_range) { described_class.from_param('50-100', currency: 'USD') }
+
+ it 'builds a price range based on a param' do
+ expect(price_range.to_s).to eq('$50 - $100')
+ end
+ end
+
describe '#to_param' do
it 'returns price range as param' do
expect(price_range.to_param).to eq('50-100')
diff --git a/frontend/spec/models/spree/filters/price_spec.rb b/frontend/spec/presenters/spree/filters/price_spec.rb
similarity index 100%
rename from frontend/spec/models/spree/filters/price_spec.rb
rename to frontend/spec/presenters/spree/filters/price_spec.rb
diff --git a/frontend/spec/presenters/spree/filters/quantified_price_range_spec.rb b/frontend/spec/presenters/spree/filters/quantified_price_range_spec.rb
new file mode 100644
index 0000000000..ade24cb23e
--- /dev/null
+++ b/frontend/spec/presenters/spree/filters/quantified_price_range_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+module Spree
+ module Filters
+ RSpec.describe QuantifiedPriceRange do
+ let(:price_range) { described_class.new(price: price, quantifier: quantifier) }
+ let(:price) { Price.new(amount: 50, currency: 'USD') }
+
+ context 'when the quantifier is less_than' do
+ let(:quantifier) { :less_than }
+
+ describe '#to_param' do
+ it 'returns price range as param' do
+ expect(price_range.to_param).to eq('0-50')
+ end
+ end
+
+ describe '#to_s' do
+ it 'returns a formatted price range' do
+ expect(price_range.to_s).to eq('Less than $50')
+ end
+ end
+ end
+
+ context 'when the quantifier is more_than' do
+ let(:quantifier) { :more_than }
+
+ describe '#to_param' do
+ it 'returns price range as param' do
+ expect(price_range.to_param).to eq('50-Infinity')
+ end
+ end
+
+ describe '#to_s' do
+ it 'returns a formatted price range' do
+ expect(price_range.to_s).to eq('More than $50')
+ end
+ end
+ end
+ end
+ end
+end