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
Extend `spree:model` generator with Spree conventions
Damian Legawiec2a1532e03e20
diff --git a/spree/core/lib/generators/spree/model/model_generator.rb b/spree/core/lib/generators/spree/model/model_generator.rbindex 28379167ba..76e35d17ea 100644--- a/spree/core/lib/generators/spree/model/model_generator.rb+++ b/spree/core/lib/generators/spree/model/model_generator.rb@@ -1,21 +1,75 @@require "rails/generators/active_record/model/model_generator"module Spree+ # spree:model — extend Rails' ActiveRecord model generator with Spree+ # conventions: Spree::Base parent, has_prefix_id, no FK constraints in+ # migrations, null: false on all columns by default, plus opt-in flags+ # for soft-delete (--paranoid) and custom fields (--metafields).+ #+ # spree:api_resource inherits from this — see api_resource_generator.rb.class ModelGenerator < ActiveRecord::Generators::ModelGenerator-def self.source_paths- paths = superclass.source_paths- paths << File.expand_path('templates', __dir__)- paths.flatten+ # Spree's templates must come FIRST so they override Rails' bundled+ # model.rb.tt and create_table_migration.rb.tt. Thor's template lookup+ # uses first-match across source_paths.+ [File.expand_path('templates', __dir__), *superclass.source_paths]end- class_option :parent, type: :string, default: "Spree::Base", desc: "The parent class for the generated model"+ class_option :parent,+ type: :string,+ default: 'Spree::Base',+ desc: 'The parent class for the generated model'++ class_option :id_prefix,+ type: :string,+ desc: 'Prefix for prefixed IDs (default: snake_cased class name, e.g. brand)'++ class_option :paranoid,+ type: :boolean,+ default: false,+ desc: 'Enable acts_as_paranoid soft-delete (adds deleted_at column + index)'- desc 'Creates a new Spree model'+ class_option :metafields,+ type: :boolean,+ default: false,+ desc: 'Include Spree::Metafields and Spree::Metadata concerns'++ desc 'Creates a new Spree model with prefixed IDs and Spree::Base parent'# Override to prevent module file from being createddef create_module_filereturnend++ # Exposed to templates (model.rb.tt, create_table_migration.rb.tt) via+ # the standard generator-method-as-template-helper mechanism.+ no_tasks do+ def id_prefix+ options[:id_prefix] || file_name+ end++ def paranoid?+ options[:paranoid]+ end++ def metafields?+ options[:metafields]+ end++ # Spree models live under app/models/spree/, regardless of whether+ # the user invokes `spree:model Brand` or `spree:model Spree::Brand`.+ # Rails' generator uses `class_path` (the namespace components) to+ # decide where to put the model file; we force `["spree"]`.+ def class_path+ ['spree']+ end++ # Spree tables are prefixed `spree_` (spree_brands, not brands).+ # Rails' `table_name` derives from the class name without namespace+ # prefixing in tables, so we add it explicitly.+ def table_name+ "spree_#{super.delete_prefix('spree_')}"+ end+ endendenddiff --git a/spree/core/lib/generators/spree/model/templates/create_table_migration.rb.tt b/spree/core/lib/generators/spree/model/templates/create_table_migration.rb.ttnew file mode 100644index 0000000000..4647d085d5--- /dev/null+++ b/spree/core/lib/generators/spree/model/templates/create_table_migration.rb.tt@@ -0,0 +1,39 @@+class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]+ def change+ create_table :<%= table_name %><%= primary_key_type %> do |t|+<% attributes.reject(&:reference?).reject(&:virtual?).each do |attribute| -%>+<% if attribute.password_digest? -%>+ t.string :password_digest, null: false+<% elsif attribute.token? -%>+ t.string :<%= attribute.name %>, null: false+<% else -%>+ t.<%= attribute.type %> :<%= attribute.name %>, null: false+<% end -%>+<% end -%>+<% attributes.select(&:reference?).each do |attribute| -%>+ t.references :<%= attribute.name %>, null: false, index: true<%= ', polymorphic: true' if attribute.polymorphic? %>+<% end -%>+<% if paranoid? -%>+ t.datetime :deleted_at++<% end -%>+<% if options[:timestamps] -%>+ t.timestamps+<% end -%>+ end+<% attributes.select(&:token?).each do |attribute| -%>+ add_index :<%= table_name %>, :<%= attribute.name %>, unique: true,+ name: 'index_<%= table_name %>_on_<%= attribute.name %>'+<% end -%>+<% attributes.select(&:has_uniq_index?).reject(&:token?).each do |attribute| -%>+ add_index :<%= table_name %>, :<%= attribute.name %>, unique: true,+ name: 'index_<%= table_name %>_on_<%= attribute.name %>'+<% end -%>+<% attributes.select { |a| a.has_index? && !a.has_uniq_index? && !a.reference? && !a.token? }.each do |attribute| -%>+ add_index :<%= table_name %>, :<%= attribute.name %>+<% end -%>+<% if paranoid? -%>+ add_index :<%= table_name %>, :deleted_at+<% end -%>+ end+enddiff --git a/spree/core/lib/generators/spree/model/templates/model.rb.tt b/spree/core/lib/generators/spree/model/templates/model.rb.ttindex 215da8cba8..d3465bbcd1 100644--- a/spree/core/lib/generators/spree/model/templates/model.rb.tt+++ b/spree/core/lib/generators/spree/model/templates/model.rb.tt@@ -1,7 +1,18 @@module Spreeclass <%= class_name.demodulize %> < <%= parent_class_name.classify %>+<% if metafields? -%>+ include Spree::Metafields+ include Spree::Metadata++<% end -%>+<% if paranoid? -%>+ acts_as_paranoid++<% end -%>+ has_prefix_id :<%= id_prefix %>+<% attributes.select(&:reference?).each do |attribute| -%>- belongs_to :<%= attribute.name %><%= ", polymorphic: true" if attribute.polymorphic? %>+ belongs_to :<%= attribute.name %><%= ", polymorphic: true" if attribute.polymorphic? %>, class_name: 'Spree::<%= attribute.name.camelize %>'<% end -%><% attributes.select(&:rich_text?).each do |attribute| -%>has_rich_text :<%= attribute.name %>@@ -17,6 +28,17 @@ module Spree<% end -%><% if attributes.any?(&:password_digest?) -%>has_secure_password+<% end -%>+<% required_attrs = attributes.reject { |a| a.reference? || a.type == :boolean || a.token? || a.attachment? || a.attachments? || a.rich_text? || a.password_digest? } -%>+<% if required_attrs.any? %>+<% required_attrs.each do |attr| -%>+ validates :<%= attr.name %>, presence: true<% if attr.has_uniq_index? %>, uniqueness: { scope: spree_base_uniqueness_scope }<% end %>++<% end -%>+<% end -%>+<% ransackable = attributes.reject { |a| a.reference? || a.attachment? || a.attachments? || a.rich_text? || a.token? || a.password_digest? }.map(&:name) -%>+<% if ransackable.any? -%>+ self.whitelisted_ransackable_attributes = %w[<%= ransackable.join(' ') %>]<% end -%>endend