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
Fix: every price search failed, and a variant's subtitle printed a Ruby hash
Damian Legawiecdbe16d780197
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rbindex 201de7f09b..7590c12f78 100644--- a/spree/api/lib/spree/api/agent_resource_map.rb+++ b/spree/api/lib/spree/api/agent_resource_map.rb@@ -175,12 +175,25 @@ module Spreewrite_permission: write_permission_for(model, scope, create_workflow, update_workflow, writable, controller),writable_attributes: writable,create_workflow_key: create_workflow,- update_workflow_key: update_workflow+ update_workflow_key: update_workflow,+ distinct: collection_distinct?(instance)}endprivate+ # What the controller does with its own collection. A controller that+ # disables DISTINCT has a reason — Postgres refuses it beside an+ # ORDER BY the select list does not carry — and a search tool adding+ # it anyway answers with a 500.+ def collection_distinct?(instance)+ return true unless instance.respond_to?(:collection_distinct?, true)++ instance.send(:collection_distinct?)+ rescue StandardError+ true+ end+# A model can be served by more than one controller under different# permission scopes — a tax identifier through customers and through# orders, media through products and through the media library. One@@ -208,7 +221,11 @@ module Spreecreate_workflow_key: candidates.filter_map { |entry| entry[:create_workflow_key] }.first,update_workflow_key: candidates.filter_map { |entry| entry[:update_workflow_key] }.first,write_permission: contested ? nil : narrowest[:write_permission],- writable_attributes: contested ? [] : narrowest[:writable_attributes]+ writable_attributes: contested ? [] : narrowest[:writable_attributes],+ # One controller refusing DISTINCT is enough: its sort would+ # make Postgres reject the query, and whichever controller+ # happened to sort first is not an answer.+ distinct: candidates.all? { |entry| entry[:distinct] })endenddiff --git a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rbindex 696d738b27..143ee1b11b 100644--- a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb+++ b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb@@ -62,6 +62,25 @@ RSpec.describe Spree::AgentTools::ResourceMap doendend+ # Postgres refuses SELECT DISTINCT beside an ORDER BY the select list does+ # not carry, which is why some controllers turn it off. A search adding it+ # anyway answered every price query with a 500.+ describe 'a resource whose controller disables DISTINCT' do+ it 'carries that through to the map' do+ expect(described_class.find('prices').distinct?).to be(false)+ end++ # One controller refusing is enough — whichever sorted first is not an+ # answer, and products is served by five.+ it 'refuses when any colliding controller refuses' do+ expect(described_class.find('products').distinct?).to be(false)+ end++ it 'leaves it on where no controller objects' do+ expect(described_class.find('orders').distinct?).to be(true)+ end+ end+describe 'a resource narrowed by a scope' doit 'narrows through its scope when one is registered' dodescribed_class.register(key: 'draft_orders', model_name: 'Spree::Order', scope_name: :drafts,diff --git a/spree/core/app/tools/spree/agent_tools/search_resources.rb b/spree/core/app/tools/spree/agent_tools/search_resources.rbindex 025a885b9d..0168f9866b 100644--- a/spree/core/app/tools/spree/agent_tools/search_resources.rb+++ b/spree/core/app/tools/spree/agent_tools/search_resources.rb@@ -42,7 +42,12 @@ module Spree# (`can :read, Product, categories: {...}`), so without `distinct` a# record repeats once per matching row and the count is inflated —# and the assistant states that count as fact.- result = query.result.distinct+ #+ # Except where the resource's own controller has turned it off:+ # Postgres rejects SELECT DISTINCT beside an ORDER BY the select list+ # does not carry, which is why `Admin::PricesController` disables it.+ # Applying it here anyway made every price search answer with a 500.+ result = entry.distinct? ? query.result.distinct : query.resultrecords = result.limit(capped_limit(limit)).to_a{diff --git a/spree/core/lib/spree/agent_tools/record_summary.rb b/spree/core/lib/spree/agent_tools/record_summary.rbindex 00ecf59448..6be73929b1 100644--- a/spree/core/lib/spree/agent_tools/record_summary.rb+++ b/spree/core/lib/spree/agent_tools/record_summary.rb@@ -161,8 +161,13 @@ module Spreevalue unless value == titleend+ # A subtitle is one short line a model reads aloud, so only a scalar+ # will do. A variant's `price` serializes as an object, and `to_s` on+ # it printed the whole Ruby hash — amount, currency, cents and all —+ # where "£89.99" belonged.def formatted(value)return if value.blank?+ return unless value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(Symbol)value.to_senddiff --git a/spree/core/lib/spree/agent_tools/resource_map.rb b/spree/core/lib/spree/agent_tools/resource_map.rbindex 1a45cbb02f..2210f1fbb5 100644--- a/spree/core/lib/spree/agent_tools/resource_map.rb+++ b/spree/core/lib/spree/agent_tools/resource_map.rb@@ -19,7 +19,19 @@ module SpreeEntry = Struct.new(:key, :model_name, :permission, :serializer_name,:dashboard_path, :scope_name, :write_permission,:writable_attributes, :create_workflow_key, :update_workflow_key,- keyword_init: true) do+ :distinct, keyword_init: true) do+ # Whether a search may add DISTINCT.+ #+ # Postgres rejects SELECT DISTINCT beside an ORDER BY the select list+ # does not carry, which is why some controllers turn it off. Carried+ # from the controller rather than decided here, so a resource that+ # changes its mind is followed without a second list to maintain.+ #+ # @return [Boolean]+ def distinct?+ distinct.nil? ? true : distinct+ end+# Whether the generic write tools may touch this resource at all.# A resource written through a workflow must be written through its# workflow tool, so there is exactly one way to write each thing.@@ -156,16 +168,20 @@ module Spree# @param create_workflow_key [String, nil] set when the controller# declares a create workflow, which the generic write defers to# @param update_workflow_key [String, nil] likewise for updates+ # @param distinct [Boolean, nil] false where the controller disables+ # DISTINCT on its own collection, which Postgres requires when the+ # sort column is not in the select list# @return [Entry]def register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil,write_permission: nil, writable_attributes: [], create_workflow_key: nil,- update_workflow_key: nil)+ update_workflow_key: nil, distinct: nil)entry = Entry.new(key: key.to_s, model_name: model_name, permission: permission,serializer_name: serializer_name, dashboard_path: dashboard_path,scope_name: scope_name, write_permission: write_permission,writable_attributes: writable_attributes,create_workflow_key: create_workflow_key,- update_workflow_key: update_workflow_key)+ update_workflow_key: update_workflow_key,+ distinct: distinct)entries[entry.key] = entryenddiff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rbindex 261d8e71ea..e47b326913 100644--- a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb+++ b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb@@ -81,6 +81,27 @@ RSpec.describe Spree::AgentTools::RecordSummary doendend+ # A subtitle is one short line a model reads aloud. A variant's `price`+ # serializes as an object, and calling `to_s` on it printed the whole Ruby+ # hash — amount, currency, cents and all — where a price belonged.+ describe 'a subtitle field that is not a scalar' do+ it 'is left out rather than dumped' do+ subtitle = described_class.send(+ :subtitle_for,+ { 'price' => { 'amount' => nil, 'currency' => nil, 'amount_in_cents' => 0 } },+ 'Espresso Machine'+ )++ expect(subtitle).to be_nil+ end++ it 'still uses a scalar one' do+ subtitle = described_class.send(:subtitle_for, { 'sku' => 'ESPRESSO-BLACK' }, 'Espresso')++ expect(subtitle).to eq('ESPRESSO-BLACK')+ end+ end+describe 'a credential carried in a value' doit 'drops an invitation acceptance link' docleaned = described_class.sanitize(