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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added ProductProperty#param to fix filtering by Property values not IDs
Damian Legawiec3d24429fbe7f
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex 39f58b458d..d879fa7462 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -172,12 +172,14 @@ module Spreedef by_properties(products)return products unless properties? && properties.values.reject(&:empty?).present?- product_ids = properties.map do |property_id, product_properties_ids|- next if product_properties_ids.empty?+ product_ids = properties.map do |property_id, product_properties_values|+ next if product_properties_values.empty?++ values = product_properties_values.split(',').map(&:strip)products.joins(:product_properties).- where(spree_product_properties: { property_id: property_id, id: product_properties_ids.split(',') }).ids+ where(spree_product_properties: { property_id: property_id, param: values }).idsend.flatten.compact.uniqproducts.where(id: product_ids)diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rbindex 681bdb4ce2..410caf54bb 100644--- a/core/app/models/spree/product_property.rb+++ b/core/app/models/spree/product_property.rb@@ -11,13 +11,13 @@ module Spreedefault_scope { order(:position) }- self.whitelisted_ransackable_attributes = ['value']+ self.whitelisted_ransackable_attributes = ['value', 'param']self.whitelisted_ransackable_associations = ['property']# virtual attributes for use with AJAX completion stuffdelegate :name, :presentation, to: :property, prefix: true, allow_nil: true- before_save :trim_value+ before_save :trim_value, :set_paramdef property_name=(name)if name.present?@@ -27,9 +27,15 @@ module Spreeenddef trim_value- return if value.nil?+ return if value.blank?value.strip!end++ def set_param+ return if value.blank?++ self.param = value.parameterize+ endendenddiff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rbindex 55103126f8..879ea24ed5 100644--- a/core/app/models/spree/property.rb+++ b/core/app/models/spree/property.rb@@ -16,11 +16,7 @@ module Spreeself.whitelisted_ransackable_attributes = ['presentation']def uniq_values- product_properties.pluck(:value).compact.uniq- end-- def first_with_value(value)- product_properties.where(value: value).first+ product_properties.pluck(:param).compact.uniqendprivatediff --git a/core/db/migrate/20210504163720_add_param_to_spree_product_properties.rb b/core/db/migrate/20210504163720_add_param_to_spree_product_properties.rbnew file mode 100644index 0000000000..194644e19c--- /dev/null+++ b/core/db/migrate/20210504163720_add_param_to_spree_product_properties.rb@@ -0,0 +1,11 @@+class AddParamToSpreeProductProperties < ActiveRecord::Migration[6.0]+ def change+ unless column_exists?(:spree_product_properties, :param)+ add_column :spree_product_properties, :param, :string+ add_index :spree_product_properties, :param++ Spree::ProductProperty.reset_column_information+ Spree::ProductProperty.all.each(&:save)+ end+ end+enddiff --git a/core/spec/models/spree/product_property_spec.rb b/core/spec/models/spree/product_property_spec.rbindex 02c8b93a0b..e3a650ceb5 100644--- a/core/spec/models/spree/product_property_spec.rb+++ b/core/spec/models/spree/product_property_spec.rb@@ -21,6 +21,18 @@ describe Spree::ProductProperty, type: :model doendcontext 'ransackable_associations' do- it { expect(Spree::ProductProperty.whitelisted_ransackable_associations).to include('property') }+ it { expect(described_class.whitelisted_ransackable_associations).to include('property') }+ end++ context 'setting param' do+ subject { build(:product_property, value: '90% Cotton 10% Elastan') }++ it { expect { subject.save! }.to change(subject, :param).from(nil).to('90-cotton-10-elastan') }+ end++ context 'setting value' do+ subject { build(:product_property, value: ' 90% Cotton 10% Elastan ') }++ it { expect { subject.save! }.to change(subject, :value).to('90% Cotton 10% Elastan') }endenddiff --git a/frontend/app/views/spree/shared/_properties.html.erb b/frontend/app/views/spree/shared/_properties.html.erbindex 1bf188591f..6f0c5b7069 100644--- a/frontend/app/views/spree/shared/_properties.html.erb+++ b/frontend/app/views/spree/shared/_properties.html.erb@@ -1,10 +1,9 @@-<% selected_properties = params.dig(:properties, "#{property.id}")&.split(',')&.map(&:to_i) || [] %>+<% selected_properties = params.dig(:properties, "#{property.id}")&.split(',') || [] %><% param_key = "properties[#{property.id}]" %><% property.uniq_values.each do |value| %>- <% value_id = property.first_with_value(value).id %>- <% property_values = (selected_properties.include?(value_id) ? selected_properties - [value_id] : selected_properties + [value_id]) %>- <%= link_to permitted_params.merge(properties: { property.id => property_values }, menu_open: 1), data: { params: permitted_params.merge(param_key => property_values, menu_open: 1), id: value_id, filter_name: param_key, multiselect: true } do %>- <div class="d-inline-block text-uppercase py-1 px-2 m-1 plp-overlay-card-item <%= 'plp-overlay-card-item--selected' if selected_properties.include?(value_id) %>">+ <% property_values = (selected_properties.include?(value) ? selected_properties - [value] : selected_properties + [value]) %>+ <%= link_to permitted_params.merge(properties: { property.id => property_values }, menu_open: 1), data: { params: permitted_params.merge(param_key => property_values, menu_open: 1), id: value, filter_name: param_key, multiselect: true } do %>+ <div class="d-inline-block text-uppercase py-1 px-2 m-1 plp-overlay-card-item <%= 'plp-overlay-card-item--selected' if selected_properties.include?(value) %>"><%= value %></div><% end %>diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rbindex d32999d6eb..dfbcd3dc2a 100644--- a/frontend/spec/features/products_filtering_spec.rb+++ b/frontend/spec/features/products_filtering_spec.rb@@ -110,7 +110,7 @@ describe 'Products filtering', :js doit 'shows products that match property filter' dovisit spree.products_path- click_on_filter 'Brand', value: 'Alpha'+ click_on_filter 'Brand', value: 'alpha'expect(page).not_to have_content('First shirt')expect(page).to have_content('Second shirt')