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.

Add serializers relationships. Update examples.

Sebastian Palmac11d0b4acab2
diff --git a/api/app/models/spree/webhooks/subscriber.rb b/api/app/models/spree/webhooks/subscriber.rb
index ced0423150..31c1073125 100644
--- a/api/app/models/spree/webhooks/subscriber.rb
+++ b/api/app/models/spree/webhooks/subscriber.rb
@@ -21,10 +21,10 @@ module Spree
def check_uri_path
uri = begin
- URI.parse(url)
- rescue URI::InvalidURIError
- return false
- end
+ URI.parse(url)
+ rescue URI::InvalidURIError
+ return false
+ end
errors.add(:url, 'the URL must have a path') if uri.blank? || uri.path.blank?
end
diff --git a/api/app/serializers/spree/api/v2/platform/asset_serializer.rb b/api/app/serializers/spree/api/v2/platform/asset_serializer.rb
index f6e7bc05d8..e1f9b931ee 100644
--- a/api/app/serializers/spree/api/v2/platform/asset_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/asset_serializer.rb
@@ -4,6 +4,8 @@ module Spree
module Platform
class AssetSerializer < BaseSerializer
include ResourceSerializerConcern
+
+ belongs_to :viewable, polymorphic: true
end
end
end
diff --git a/api/app/serializers/spree/api/v2/platform/prototype_serializer.rb b/api/app/serializers/spree/api/v2/platform/prototype_serializer.rb
index abd05c34f1..3fef1158d0 100644
--- a/api/app/serializers/spree/api/v2/platform/prototype_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/prototype_serializer.rb
@@ -4,6 +4,10 @@ module Spree
module Platform
class PrototypeSerializer < BaseSerializer
include ResourceSerializerConcern
+
+ has_many :properties
+ has_many :option_types
+ has_many :taxons
end
end
end
diff --git a/api/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb b/api/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb
index 0524ba7acb..9c2235edfb 100644
--- a/api/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb
@@ -4,6 +4,10 @@ module Spree
module Platform
class StockTransferSerializer < BaseSerializer
include ResourceSerializerConcern
+
+ belongs_to :destination_location, serializer: :stock_location, record_type: :destination_location
+ belongs_to :source_location, serializer: :stock_location, record_type: :source_location
+ has_many :stock_movements
end
end
end
diff --git a/api/spec/serializers/spree/api/v2/platform/asset_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/asset_serializer_spec.rb
index 6e46dbfbe8..174a333ba6 100644
--- a/api/spec/serializers/spree/api/v2/platform/asset_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/asset_serializer_spec.rb
@@ -3,29 +3,38 @@ require 'spec_helper'
describe Spree::Api::V2::Platform::AssetSerializer do
include_context 'API v2 serializers params'
- subject { described_class.new(asset, params: serializer_params).serializable_hash }
+ subject { described_class.new(resource, params: serializer_params).serializable_hash }
+ let(:viewable) { create(:variant) }
let(:type) { :asset }
- let(:asset) { create(type) }
+ let(:resource) { create(type, viewable: viewable) }
it do
expect(subject).to eq(
data: {
- id: asset.id.to_s,
- type: type,
+ id: resource.id.to_s,
attributes: {
- viewable_type: asset.viewable_type,
- attachment_height: asset.attachment_height,
- attachment_file_size: asset.attachment_file_size,
- position: asset.position,
- attachment_content_type: asset.attachment_content_type,
- attachment_file_name: asset.attachment_file_name,
- type: asset.type,
- attachment_updated_at: asset.attachment_updated_at,
- alt: asset.alt,
- created_at: asset.created_at,
- updated_at: asset.updated_at
- }
+ viewable_type: resource.viewable_type,
+ attachment_height: resource.attachment_height,
+ attachment_file_size: resource.attachment_file_size,
+ position: resource.position,
+ attachment_content_type: resource.attachment_content_type,
+ attachment_file_name: resource.attachment_file_name,
+ type: resource.type,
+ attachment_updated_at: resource.attachment_updated_at,
+ alt: resource.alt,
+ created_at: resource.created_at,
+ updated_at: resource.updated_at
+ },
+ relationships: {
+ viewable: {
+ data: {
+ id: viewable.id.to_s,
+ type: :variant
+ }
+ }
+ },
+ type: type
}
)
end
diff --git a/api/spec/serializers/spree/api/v2/platform/prototype_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/prototype_serializer_spec.rb
index b594447a79..1626171849 100644
--- a/api/spec/serializers/spree/api/v2/platform/prototype_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/prototype_serializer_spec.rb
@@ -5,19 +5,42 @@ describe Spree::Api::V2::Platform::PrototypeSerializer do
subject { described_class.new(resource, params: serializer_params).serializable_hash }
+ let(:property) { create(:property) }
+ let(:option_type) { create(:option_type) }
+ let(:resource) { create(type, properties: [property], option_types: [option_type], taxons: [taxon]) }
+ let(:taxon) { create(:taxon) }
let(:type) { :prototype }
- let(:resource) { create(type) }
it do
expect(subject).to eq(
data: {
id: resource.id.to_s,
- type: type,
attributes: {
name: resource.name,
created_at: resource.created_at,
updated_at: resource.updated_at
- }
+ },
+ relationships: {
+ properties: {
+ data: [{
+ id: property.id.to_s,
+ type: :property
+ }]
+ },
+ option_types: {
+ data: [{
+ id: option_type.id.to_s,
+ type: :option_type
+ }]
+ },
+ taxons: {
+ data: [{
+ id: taxon.id.to_s,
+ type: :taxon
+ }]
+ }
+ },
+ type: type
}
)
end
diff --git a/api/spec/serializers/spree/api/v2/platform/stock_transfer_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/stock_transfer_serializer_spec.rb
index 62a404e775..dd406851c7 100644
--- a/api/spec/serializers/spree/api/v2/platform/stock_transfer_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/stock_transfer_serializer_spec.rb
@@ -5,21 +5,47 @@ describe Spree::Api::V2::Platform::StockTransferSerializer do
subject { described_class.new(resource, params: serializer_params).serializable_hash }
+ let(:destination_location) { create(:stock_location) }
+ let(:resource) { create(type, destination_location: destination_location, source_location: source_location) }
+ let(:source_location) { create(:stock_location) }
+ let(:stock_item) { stock_location.stock_items.order(:id).first }
+ let(:stock_location) { create(:stock_location_with_items) }
let(:type) { :stock_transfer }
- let(:resource) { create(type) }
+
+ let!(:stock_movement) { create(:stock_movement, stock_item: stock_item, originator: resource) }
it do
expect(subject).to eq(
data: {
id: resource.id.to_s,
- type: type,
attributes: {
type: resource.type,
reference: resource.reference,
created_at: resource.created_at,
updated_at: resource.updated_at,
number: resource.number
- }
+ },
+ relationships: {
+ stock_movements: {
+ data: [{
+ id: stock_movement.id.to_s,
+ type: :stock_movement
+ }]
+ },
+ source_location: {
+ data: {
+ id: source_location.id.to_s,
+ type: :source_location
+ }
+ },
+ destination_location: {
+ data: {
+ id: destination_location.id.to_s,
+ type: :destination_location
+ }
+ }
+ },
+ type: type,
}
)
end