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.

Fixed model generator with polymorphic belongs_to association

Damian Legawiec21117a86ad5a
diff --git a/spree/core/lib/generators/spree/api_resource/templates/factory.rb.tt b/spree/core/lib/generators/spree/api_resource/templates/factory.rb.tt
index b1066f5efd..705ba358fb 100644
--- a/spree/core/lib/generators/spree/api_resource/templates/factory.rb.tt
+++ b/spree/core/lib/generators/spree/api_resource/templates/factory.rb.tt
@@ -15,7 +15,12 @@ FactoryBot.define do
<% end -%>
<% end -%>
<% attributes.select(&:reference?).each do |attr| -%>
+<% if attr.polymorphic? -%>
+ # <%= attr.name %> is polymorphic — point it at a concrete factory before using:
+ # association :<%= attr.name %>, factory: :product
+<% else -%>
association :<%= attr.name %>
+<% end -%>
<% end -%>
end
end
diff --git a/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb b/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb
index d753bc9fb8..3a8c72edf8 100644
--- a/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb
+++ b/spree/core/spec/lib/generators/spree/api_resource/api_resource_generator_spec.rb
@@ -123,6 +123,13 @@ RSpec.describe Spree::ApiResourceGenerator, type: :generator do
expect(Dir[File.join(destination, 'lib/spree/testing_support/factories/*_factory.rb')]).to be_empty
end
+ it 'comments out polymorphic associations in the factory (no concrete factory to target)' do
+ result = run_generator(['Tag', 'name:string', 'taggable:references{polymorphic}'])
+
+ expect(result[:factory]).to include('# association :taggable, factory: :product')
+ expect(result[:factory]).not_to match(/^\s*association :taggable$/)
+ end
+
it 'generates Store and Admin serializers (Admin inherits Store)' do
result = run_generator(['Brand', 'name:string'])