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.

Added `rails g spree:model` generator to generate active record model… (#13310)

Damian Legawiec6f679ad2c8b8
diff --git a/core/lib/generators/spree/model/model_generator.rb b/core/lib/generators/spree/model/model_generator.rb
new file mode 100644
index 0000000000..28379167ba
--- /dev/null
+++ b/core/lib/generators/spree/model/model_generator.rb
@@ -0,0 +1,21 @@
+require "rails/generators/active_record/model/model_generator"
+
+module Spree
+ class ModelGenerator < ActiveRecord::Generators::ModelGenerator
+
+ def self.source_paths
+ paths = superclass.source_paths
+ paths << File.expand_path('templates', __dir__)
+ paths.flatten
+ end
+
+ class_option :parent, type: :string, default: "Spree::Base", desc: "The parent class for the generated model"
+
+ desc 'Creates a new Spree model'
+
+ # Override to prevent module file from being created
+ def create_module_file
+ return
+ end
+ end
+end
diff --git a/core/lib/generators/spree/model/templates/model.rb.tt b/core/lib/generators/spree/model/templates/model.rb.tt
new file mode 100644
index 0000000000..215da8cba8
--- /dev/null
+++ b/core/lib/generators/spree/model/templates/model.rb.tt
@@ -0,0 +1,22 @@
+module Spree
+ class <%= class_name.demodulize %> < <%= parent_class_name.classify %>
+<% attributes.select(&:reference?).each do |attribute| -%>
+ belongs_to :<%= attribute.name %><%= ", polymorphic: true" if attribute.polymorphic? %>
+<% end -%>
+<% attributes.select(&:rich_text?).each do |attribute| -%>
+ has_rich_text :<%= attribute.name %>
+<% end -%>
+<% attributes.select(&:attachment?).each do |attribute| -%>
+ has_one_attached :<%= attribute.name %>
+<% end -%>
+<% attributes.select(&:attachments?).each do |attribute| -%>
+ has_many_attached :<%= attribute.name %>
+<% end -%>
+<% attributes.select(&:token?).each do |attribute| -%>
+ has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
+<% end -%>
+<% if attributes.any?(&:password_digest?) -%>
+ has_secure_password
+<% end -%>
+ end
+end