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 controller decorator generator

Damian Legawiec105ab2f37bfb
diff --git a/docs/developer/customization/decorators.mdx b/docs/developer/customization/decorators.mdx
index a35b46863e..e33104278a 100644
--- a/docs/developer/customization/decorators.mdx
+++ b/docs/developer/customization/decorators.mdx
@@ -117,19 +117,21 @@ bin/rails g spree:controller_decorator Spree::Admin::ProductsController
This creates `app/controllers/spree/admin/products_controller_decorator.rb`:
```ruby
-module Spree
- module Admin
- module ProductsControllerDecorator
- def self.prepended(base)
- # Class-level configurations go here
- end
+module Spree::Admin
+ module ProductsControllerDecorator
+ def self.prepended(base)
+ # base.before_action :my_filter
end
- ProductsController.prepend(ProductsControllerDecorator)
+ # add custom methods here
end
end
+
+Spree::Admin::ProductsController.prepend Spree::Admin::ProductsControllerDecorator
```
+The generator accepts any namespace depth. `Spree::ProductsController` produces a top-level `module Spree` wrapper; `Spree::Api::V3::Store::ProductsController` produces a `module Spree::Api::V3::Store` wrapper. The final `.prepend` line is always fully qualified.
+
## Decorating Models
### Changing Behavior of Existing Methods
diff --git a/spree/core/lib/generators/spree/controller_decorator/controller_decorator_generator.rb b/spree/core/lib/generators/spree/controller_decorator/controller_decorator_generator.rb
new file mode 100644
index 0000000000..07f91bdb5c
--- /dev/null
+++ b/spree/core/lib/generators/spree/controller_decorator/controller_decorator_generator.rb
@@ -0,0 +1,66 @@
+module Spree
+ # spree:controller_decorator — generate a decorator file for an existing
+ # Spree controller. Mirrors the model_decorator generator but handles
+ # arbitrary namespace depth (Spree::ProductsController,
+ # Spree::Admin::ProductsController, Spree::Api::V3::Store::ProductsController).
+ #
+ # @example
+ # bin/rails g spree:controller_decorator Spree::ProductsController
+ # => app/controllers/spree/products_controller_decorator.rb
+ #
+ # bin/rails g spree:controller_decorator Spree::Admin::ProductsController
+ # => app/controllers/spree/admin/products_controller_decorator.rb
+ class ControllerDecoratorGenerator < Rails::Generators::NamedBase
+ desc 'Creates a controller decorator for a Spree controller'
+
+ argument :name, type: :string, required: true,
+ banner: 'Spree::ControllerName | Spree::Namespace::ControllerName'
+
+ def self.source_paths
+ paths = superclass.source_paths
+ paths << File.expand_path('templates', __dir__)
+ paths.flatten
+ end
+
+ def create_controller_decorator_file
+ template 'controller_decorator.rb.tt',
+ "app/controllers/#{file_path}_decorator.rb"
+ end
+
+ private
+
+ # Strip a leading `Spree::` and split the remainder on `::`.
+ # `"Spree::Admin::ProductsController"` => `["Admin", "ProductsController"]`
+ def name_parts
+ @name_parts ||= name.sub(/\ASpree::/, '').split('::').reject(&:empty?)
+ end
+
+ # The unqualified controller name — the last segment.
+ def controller_name
+ name_parts.last
+ end
+
+ # The namespace segments above the controller, joined as a constant
+ # chain. Empty string when the controller sits directly under Spree.
+ def namespace_chain
+ name_parts[0..-2].join('::')
+ end
+
+ # The decorator module name.
+ def decorator_name
+ "#{controller_name}Decorator"
+ end
+
+ # Path under app/controllers/, including the `spree/` root.
+ def file_path
+ ['spree', *name_parts.map(&:underscore)].join('/')
+ end
+
+ # Fully-qualified `Spree::Foo::Bar.prepend Spree::Foo::BarDecorator`.
+ def prepend_invocation
+ target = "Spree::#{name_parts.join('::')}"
+ module_chain = ['Spree', *name_parts[0..-2], decorator_name].join('::')
+ "#{target}.prepend #{module_chain}"
+ end
+ end
+end
diff --git a/spree/core/lib/generators/spree/controller_decorator/templates/controller_decorator.rb.tt b/spree/core/lib/generators/spree/controller_decorator/templates/controller_decorator.rb.tt
new file mode 100644
index 0000000000..b6dfd83cd4
--- /dev/null
+++ b/spree/core/lib/generators/spree/controller_decorator/templates/controller_decorator.rb.tt
@@ -0,0 +1,25 @@
+<% if namespace_chain.empty? -%>
+module Spree
+ module <%= decorator_name %>
+ def self.prepended(base)
+ # base.before_action :my_filter
+ end
+
+ # add custom methods here
+ end
+end
+
+Spree::<%= controller_name %>.prepend Spree::<%= decorator_name %>
+<% else -%>
+module Spree::<%= namespace_chain %>
+ module <%= decorator_name %>
+ def self.prepended(base)
+ # base.before_action :my_filter
+ end
+
+ # add custom methods here
+ end
+end
+
+<%= prepend_invocation %>
+<% end -%>