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.

Use Property#filter_param rather than IDs for filtering

Damian Legawiecdfe15f067207
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index e02be7f86f..9969b7eb64 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -1101,6 +1101,12 @@ paths:
type: string
example: Red
description: 'Find Prodcuts with Variants that have the specified option (eg. color, size) and value (eg. red, XS)'
+ - in: query
+ name: 'filter[properties][brand-name]'
+ schema:
+ type: string
+ example: alpha
+ description: 'Find Prodcuts with Property Brand Name with value Alpha'
- in: query
name: 'filter[show_deleted]'
schema:
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index a55e5c6be1..51e663e239 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -100,15 +100,27 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
context 'with specified properties' do
- before { get "/api/v2/storefront/products?filter[properties][#{property.id}]=#{product_property.filter_param}&include=product_properties" }
+ context 'using proper filter params' do
+ before { get "/api/v2/storefront/products?filter[properties][#{property.filter_param}]=#{product_property.filter_param}&include=product_properties" }
- it_behaves_like 'returns 200 HTTP status'
+ it_behaves_like 'returns 200 HTTP status'
- it 'returns products with specified options' do
- expect(json_response['data'].first).to have_id(product_with_property.id.to_s)
- expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:name).with_value(property.name)))
- expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:value).with_value(product_property.value)))
- expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:filter_param).with_value(product_property.filter_param)))
+ it 'returns products with specified options' do
+ expect(json_response['data'].first).to have_id(product_with_property.id.to_s)
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:name).with_value(property.name)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:value).with_value(product_property.value)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:filter_param).with_value(product_property.filter_param)))
+ end
+ end
+
+ context 'using property names and values' do
+ before { get "/api/v2/storefront/products?filter[properties][#{property.name}]=#{product_property.value}&include=product_properties" }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products with specified options' do
+ expect(json_response['data'].first).to have_id(product_with_property.id.to_s)
+ end
end
end
diff --git a/backend/app/views/spree/admin/properties/index.html.erb b/backend/app/views/spree/admin/properties/index.html.erb
index 2752cd0419..b0b628dec2 100644
--- a/backend/app/views/spree/admin/properties/index.html.erb
+++ b/backend/app/views/spree/admin/properties/index.html.erb
@@ -44,6 +44,7 @@
<th><%= Spree.t(:name) %></th>
<th><%= Spree.t(:presentation) %></th>
<th><%= Spree.t(:filterable) %></th>
+ <th><%= Spree.t(:filter_param) %></th>
<th class="actions"></th>
</tr>
</thead>
@@ -53,6 +54,7 @@
<td><%= property.name %></td>
<td><%= property.presentation %></td>
<td><%= active_badge(property.filterable) %></td>
+ <td><%= property.filter_param %></td>
<td class="actions">
<span class="d-flex justify-content-end">
<%= link_to_edit(property, no_text: true) if can?(:edit, property) %>
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index b2d1332315..8e3ea8d041 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -172,14 +172,15 @@ module Spree
def by_properties(products)
return products unless properties? && properties.values.reject(&:empty?).present?
- product_ids = properties.to_unsafe_hash.map do |property_id, product_properties_values|
- next if product_properties_values.empty?
+ product_ids = properties.to_unsafe_hash.map do |property_filter_param, product_properties_values|
+ next if property_filter_param.blank? || product_properties_values.empty?
- values = product_properties_values.split(',').map(&:strip)
+ values = product_properties_values.split(',').map(&:parameterize)
products.
- joins(:product_properties).
- where(spree_product_properties: { property_id: property_id, filter_param: values }).ids
+ joins(product_properties: :property).
+ where(spree_properties: { filter_param: property_filter_param.parameterize }).
+ where(spree_product_properties: { filter_param: values }).ids
end.flatten.compact.uniq
products.where(id: product_ids)
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 8bc10b9cf3..57c9ada207 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -1,6 +1,8 @@
module Spree
class Property < Spree::Base
- auto_strip_attributes :name
+ include Spree::FilterParam
+
+ auto_strip_attributes :name, :presentation
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype'
has_many :prototypes, through: :property_prototypes, class_name: 'Spree::Prototype'
diff --git a/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb b/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb
new file mode 100644
index 0000000000..6b727b8495
--- /dev/null
+++ b/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb
@@ -0,0 +1,11 @@
+class AddFilterParamToSpreeProperties < ActiveRecord::Migration[6.0]
+ def change
+ unless column_exists?(:spree_properties, :filter_param)
+ add_column :spree_properties, :filter_param, :string
+ add_index :spree_properties, :filter_param
+
+ Spree::Property.reset_column_information
+ Spree::Property.all.each(&:save)
+ end
+ end
+end
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
new file mode 100644
index 0000000000..fc05db038b
--- /dev/null
+++ b/core/spec/models/spree/property_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe Spree::Property, type: :model do
+ context 'setting filter param' do
+ subject { build(:property, name: 'Brand Name') }
+
+ it { expect { subject.save! }.to change(subject, :filter_param).from(nil).to('brand-name') }
+ end
+end
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index 41cebd9dd1..a60f612c09 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -34,7 +34,7 @@
<% if property.uniq_values.present? %>
<div class="w-100 card plp-filters-card">
<% property_name = property.name %>
- <% collapsed = params.dig(:properties, property.id.to_s).blank? %>
+ <% collapsed = params.dig(:properties, property.filter_param).blank? %>
<button class="card-header <%= 'collapsed' if collapsed %> px-1 plp-filters-card-header" id="filters<%= property_name %>" data-toggle="collapse" data-target="#collapseFilter<%= property_name %>" aria-expanded="<%= !collapsed %>" aria-controls="collapseFilter<%= property_name %>" role="heading" aria-level="2">
<%= property.presentation.titleize %>
<%= icon(name: 'plus',
diff --git a/frontend/app/views/spree/products/_filters_mobile.html.erb b/frontend/app/views/spree/products/_filters_mobile.html.erb
index 23ac3c74d7..f71b5a4e69 100644
--- a/frontend/app/views/spree/products/_filters_mobile.html.erb
+++ b/frontend/app/views/spree/products/_filters_mobile.html.erb
@@ -33,7 +33,7 @@
<% available_properties.each do |property| %>
<% if property.uniq_values.present? %>
<% property_name = property.name %>
- <% collapsed = params.dig(:properties, property.id.to_s).blank? %>
+ <% collapsed = params.dig(:properties, property.filter_param).blank? %>
<div class="card plp-overlay-card">
<div class="card-header text-uppercase <%= 'collapsed' if collapsed %> plp-overlay-card-header" data-toggle="collapse" id="filtersMobile<%= property_name %>" data-target="#collapseFilterMobile<%= property_name %>" aria-expanded="true" aria-controls="collapseFilterMobile<%= property_name %>" role="heading" aria-level="3">
<%= property_name %>
diff --git a/frontend/app/views/spree/shared/_properties.html.erb b/frontend/app/views/spree/shared/_properties.html.erb
index 6f0c5b7069..309809edb8 100644
--- a/frontend/app/views/spree/shared/_properties.html.erb
+++ b/frontend/app/views/spree/shared/_properties.html.erb
@@ -1,8 +1,8 @@
-<% selected_properties = params.dig(:properties, "#{property.id}")&.split(',') || [] %>
-<% param_key = "properties[#{property.id}]" %>
+<% selected_properties = params.dig(:properties, "#{property.filter_param}")&.split(',') || [] %>
+<% param_key = "properties[#{property.filter_param}]" %>
<% property.uniq_values.each do |value| %>
<% 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 %>
+ <%= link_to permitted_params.merge(properties: { property.filter_param => 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>