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.

Make a digital link's store public so the keyless download works

Damian Legawiec65480a5edbb9
diff --git a/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
index 5b7b55f357..123cb761e4 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
@@ -191,5 +191,20 @@ RSpec.describe Spree::Api::V3::Store::DigitalLinksController, type: :controller
expect(response).to have_http_status(:found)
end
end
+
+ # The shared 'API v3 Store' context stubs current_store on the base
+ # controllers, which hides this controller's own current_store override. A
+ # real emailed link carries no publishable key, so the override runs and
+ # derives the store from the link itself. Exercise the real override
+ # directly — reading the store off the link — so a regression (e.g. the
+ # store method going private) is caught here rather than only in production.
+ describe '#current_store (real override, unstubbed)' do
+ it 'derives the store from the link when no key resolves one' do
+ real_controller = described_class.new
+ real_controller.instance_variable_set(:@resource, digital_link)
+
+ expect(real_controller.send(:current_store)).to eq(order.store)
+ end
+ end
end
end
diff --git a/spree/core/app/models/spree/digital_link.rb b/spree/core/app/models/spree/digital_link.rb
index 3530775b80..adbc4b5c42 100644
--- a/spree/core/app/models/spree/digital_link.rb
+++ b/spree/core/app/models/spree/digital_link.rb
@@ -21,6 +21,11 @@ module Spree
validates :access_counter, numericality: { greater_than_or_equal_to: 0 }
delegate :filename, :content_type, to: :digital_asset
+ # The order's store is authoritative for both the download-limit flags and,
+ # passed into the asset, the limit values — so one store answers the whole
+ # check. Public so the keyless download endpoint can derive store context
+ # from the link (emailed links carry no publishable key).
+ delegate :store, to: :line_item
delegate :order, to: :line_item
# @deprecated Use {#digital_asset}; removed in 6.1.
@@ -81,12 +86,6 @@ module Spree
private
- # The order's store is authoritative for both the limit flags and, passed
- # into the asset, the limit values — so one store answers the whole check.
- def store
- @store ||= line_item.order.store
- end
-
def set_defaults
self.access_counter ||= 0
end
diff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rb
index 4a890f0297..5c8e539700 100644
--- a/spree/core/app/models/spree/line_item.rb
+++ b/spree/core/app/models/spree/line_item.rb
@@ -87,6 +87,9 @@ module Spree
variant.primary_media || product.primary_media
end
delegate :digital?, :can_supply?, to: :variant
+ # A line item's store is its owner's — an order's or a cart's, whichever it
+ # belongs to.
+ delegate :store, to: :owner
scope :with_digital_assets, -> { joins(:variant).merge(Spree::Variant.with_digital_assets) }
diff --git a/spree/core/spec/models/spree/digital_link_spec.rb b/spree/core/spec/models/spree/digital_link_spec.rb
index bf61840d8e..7027568359 100644
--- a/spree/core/spec/models/spree/digital_link_spec.rb
+++ b/spree/core/spec/models/spree/digital_link_spec.rb
@@ -34,6 +34,23 @@ describe Spree::DigitalLink, type: :model do
end
end
+ describe '#store' do
+ let(:order) { create(:order, store: store) }
+ let(:line_item) { create(:line_item, order: order, variant: variant) }
+ let(:digital_link) { create(:digital_link, digital_asset: digital_asset, line_item: line_item) }
+
+ # The keyless download endpoint derives store context from the link, so
+ # this must be callable from outside the model — a private method would
+ # raise NoMethodError there.
+ it 'is public' do
+ expect(digital_link.public_methods).to include(:store)
+ end
+
+ it 'returns the owning order store' do
+ expect(digital_link.store).to eq(order.store)
+ end
+ end
+
describe '#reset!' do
let!(:digital_link) { create(:digital_link, access_counter: 5) }