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.

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.rb
index 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 Spree
write_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)
}
end
private
+ # 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 Spree
create_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] }
)
end
end
diff --git a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb
index 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 do
end
end
+ # 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' do
it 'narrows through its scope when one is registered' do
described_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.rb
index 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.result
records = 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.rb
index 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 Spree
value unless value == title
end
+ # 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_s
end
diff --git a/spree/core/lib/spree/agent_tools/resource_map.rb b/spree/core/lib/spree/agent_tools/resource_map.rb
index 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 Spree
Entry = 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] = entry
end
diff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb
index 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 do
end
end
+ # 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' do
it 'drops an invitation acceptance link' do
cleaned = described_class.sanitize(