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
Include Products in Taxons API only for show action
Damian Legawiec2fbbaa608c65
diff --git a/api/app/controllers/spree/api/v2/platform/taxons_controller.rb b/api/app/controllers/spree/api/v2/platform/taxons_controller.rbindex 3146c271fe..3e0483392b 100644--- a/api/app/controllers/spree/api/v2/platform/taxons_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/taxons_controller.rb@@ -10,16 +10,19 @@ module Spreeenddef scope_includes- node_includes = %i[icon products parent taxonomy]+ node_includes = %i[icon parent taxonomy]{parent: node_includes,children: node_includes,taxonomy: [root: node_includes],- products: [],icon: [attachment_attachment: :blob]}end++ def serializer_params+ super.merge(include_products: action_name == 'show')+ endendendenddiff --git a/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb b/api/app/controllers/spree/api/v2/storefront/taxons_controller.rbindex 82de2a4fbc..0e78a0b9ff 100644--- a/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb+++ b/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb@@ -30,16 +30,19 @@ module Spreeenddef scope_includes- node_includes = %i[icon products parent taxonomy]+ node_includes = %i[icon parent taxonomy]{parent: node_includes,children: node_includes,taxonomy: [root: node_includes],- products: [],icon: [attachment_attachment: :blob]}end++ def serializer_params+ super.merge(include_products: action_name == 'show')+ endendendenddiff --git a/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb b/api/app/serializers/spree/api/v2/platform/taxon_serializer.rbindex e71a3e2649..0c5b9be37a 100644--- a/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb@@ -23,7 +23,8 @@ module Spreebelongs_to :taxonomy, record_type: :taxonomyhas_many :children, record_type: :taxon, serializer: :taxon- has_many :products, record_type: :product+ has_many :products, record_type: :product,+ if: proc { |_taxon, params| params && params[:include_products] == true }has_one :image,object_method_name: :icon,diff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rbindex 97aff9c9f5..b0bbc9b4f2 100644--- a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb@@ -23,7 +23,8 @@ module Spreebelongs_to :taxonomy, record_type: :taxonomyhas_many :children, record_type: :taxon, serializer: :taxon- has_many :products, record_type: :product+ has_many :products, record_type: :product,+ if: proc { |_taxon, params| params && params[:include_products] == true }has_one :image,object_method_name: :icon,diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rbindex f278bee205..43e01edbe4 100644--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb@@ -30,7 +30,8 @@ describe 'Taxons Spec', type: :request doit 'returns all taxons' doexpect(json_response['data'].size).to eq(3)expect(json_response['data'][0]).to have_type('taxon')- expect(json_response['data'][0]).to have_relationships(:parent, :children)+ expect(json_response['data'][0]).to have_relationships(:parent, :taxonomy, :children, :image)+ expect(json_response['data'][0]).not_to have_relationships(:produts)endit 'should return only default store taxons' do@@ -48,7 +49,7 @@ describe 'Taxons Spec', type: :request doexpect(json_response['data'][0]).to have_type('taxon')expect(json_response['data'][0]).to have_id(taxonomy.root.id.to_s)expect(json_response['data'][0]).to have_relationship(:parent).with_data(nil)- expect(json_response['data'][0]).to have_relationships(:parent, :taxonomy, :children, :products, :image)+ expect(json_response['data'][0]).to have_relationships(:parent, :taxonomy, :children, :image)endenddiff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rbindex d97baf6289..b74b00e1c5 100644--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb@@ -1,84 +1,157 @@require 'spec_helper'describe Spree::Api::V2::Platform::TaxonSerializer do- subject { described_class.new(taxon) }+ include_context 'API v2 serializers params'++ subject { described_class.new(taxon, params: serializer_params) }let(:taxon) { create(:taxon, products: create_list(:product, 2)) }let!(:children) { [create(:taxon, parent_id: taxon.id), create(:taxon, parent_id: taxon.id)] }it { expect(subject.serializable_hash).to be_kind_of(Hash) }- it do- expect(subject.serializable_hash).to eq(- {- data: {- id: taxon.id.to_s,- type: :taxon,- attributes: {- position: taxon.position,- name: taxon.name,- permalink: taxon.permalink,- lft: taxon.lft,- rgt: taxon.rgt,- description: taxon.description,- created_at: taxon.created_at,- updated_at: taxon.updated_at,- meta_title: taxon.meta_title,- meta_description: taxon.meta_description,- meta_keywords: taxon.meta_keywords,- depth: taxon.depth,- pretty_name: taxon.pretty_name,- seo_title: taxon.seo_title,- is_root: taxon.root?,- is_child: taxon.child?,- is_leaf: taxon.leaf?- },- relationships: {- parent: {- data: {- id: taxon.parent.id.to_s,- type: :taxon- }- },- taxonomy: {- data: {- id: taxon.taxonomy.id.to_s,- type: :taxonomy- }- },- image: {- data: {- id: taxon.icon.id.to_s,- type: :taxon_image- }+ context 'without products' do+ it do+ expect(subject.serializable_hash).to eq(+ {+ data: {+ id: taxon.id.to_s,+ type: :taxon,+ attributes: {+ position: taxon.position,+ name: taxon.name,+ permalink: taxon.permalink,+ lft: taxon.lft,+ rgt: taxon.rgt,+ description: taxon.description,+ created_at: taxon.created_at,+ updated_at: taxon.updated_at,+ meta_title: taxon.meta_title,+ meta_description: taxon.meta_description,+ meta_keywords: taxon.meta_keywords,+ depth: taxon.depth,+ pretty_name: taxon.pretty_name,+ seo_title: taxon.seo_title,+ is_root: taxon.root?,+ is_child: taxon.child?,+ is_leaf: taxon.leaf?},- products: {- data: [- {- id: taxon.products.first.id.to_s,- type: :product- },- {- id: taxon.products.second.id.to_s,- type: :product+ relationships: {+ parent: {+ data: {+ id: taxon.parent.id.to_s,+ type: :taxon+ }+ },+ taxonomy: {+ data: {+ id: taxon.taxonomy.id.to_s,+ type: :taxonomy+ }+ },+ image: {+ data: {+ id: taxon.icon.id.to_s,+ type: :taxon_image}- ]+ },+ children: {+ data: [+ {+ id: taxon.children.first.id.to_s,+ type: :taxon+ },+ {+ id: taxon.children.second.id.to_s,+ type: :taxon+ }+ ]+ }+ }+ }+ }+ )+ end+ end++ context 'with products' do+ before do+ serializer_params[:include_products] = true+ end++ it do+ expect(subject.serializable_hash).to eq(+ {+ data: {+ id: taxon.id.to_s,+ type: :taxon,+ attributes: {+ position: taxon.position,+ name: taxon.name,+ permalink: taxon.permalink,+ lft: taxon.lft,+ rgt: taxon.rgt,+ description: taxon.description,+ created_at: taxon.created_at,+ updated_at: taxon.updated_at,+ meta_title: taxon.meta_title,+ meta_description: taxon.meta_description,+ meta_keywords: taxon.meta_keywords,+ depth: taxon.depth,+ pretty_name: taxon.pretty_name,+ seo_title: taxon.seo_title,+ is_root: taxon.root?,+ is_child: taxon.child?,+ is_leaf: taxon.leaf?},- children: {- data: [- {- id: taxon.children.first.id.to_s,- type: :taxon- },- {- id: taxon.children.second.id.to_s,+ relationships: {+ parent: {+ data: {+ id: taxon.parent.id.to_s,type: :taxon}- ]+ },+ taxonomy: {+ data: {+ id: taxon.taxonomy.id.to_s,+ type: :taxonomy+ }+ },+ image: {+ data: {+ id: taxon.icon.id.to_s,+ type: :taxon_image+ }+ },+ products: {+ data: [+ {+ id: taxon.products.first.id.to_s,+ type: :product+ },+ {+ id: taxon.products.second.id.to_s,+ type: :product+ }+ ]+ },+ children: {+ data: [+ {+ id: taxon.children.first.id.to_s,+ type: :taxon+ },+ {+ id: taxon.children.second.id.to_s,+ type: :taxon+ }+ ]+ }}}}- }- )+ )+ end+endend