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.

Simplify setting/accessing injection partials for storefront/admin (#13295)

Damian Legawiec88e9b14d6589
diff --git a/admin/app/helpers/spree/admin/base_helper.rb b/admin/app/helpers/spree/admin/base_helper.rb
index c9a9967c40..f137d3fc98 100644
--- a/admin/app/helpers/spree/admin/base_helper.rb
+++ b/admin/app/helpers/spree/admin/base_helper.rb
@@ -4,7 +4,7 @@ module Spree
include Spree::ImagesHelper
def render_admin_partials(section, options = {})
- Rails.application.config.spree_admin.send(section).map do |partial|
+ Spree::Admin.partials.send(section.to_s.gsub('_partials', '').to_sym).map do |partial|
render partial, options
end.join.html_safe
end
diff --git a/admin/lib/spree/admin.rb b/admin/lib/spree/admin.rb
index 584e03c8e4..678de437e4 100644
--- a/admin/lib/spree/admin.rb
+++ b/admin/lib/spree/admin.rb
@@ -18,6 +18,7 @@ require 'inline_svg'
require 'spree/admin/action_callbacks'
require 'spree/admin/callbacks'
require 'spree/admin/engine'
+require 'spree/core/partials'
require 'tinymce-rails'
@@ -30,5 +31,12 @@ module Spree
def self.navigation=(navigation)
Rails.application.config.spree_admin.navigation = navigation
end
+
+ def self.partials
+ @partials ||= Spree::Core::Partials.new(
+ Rails.application.config.spree_admin,
+ Spree::Admin::Engine::Environment
+ )
+ end
end
end
diff --git a/admin/spec/helpers/spree/admin/base_helper_spec.rb b/admin/spec/helpers/spree/admin/base_helper_spec.rb
index ebb0d00164..fafd8bf213 100644
--- a/admin/spec/helpers/spree/admin/base_helper_spec.rb
+++ b/admin/spec/helpers/spree/admin/base_helper_spec.rb
@@ -33,4 +33,70 @@ describe Spree::Admin::BaseHelper do
expect(helper.spree_time_ago(nil)).to eq('')
end
end
+
+ describe '#render_admin_partials' do
+ before do
+ # Set up test partials
+ allow(Rails.application.config.spree_admin).to receive(:product_form_partials).and_return(['spree/admin/products/test_partial'])
+ end
+
+ context 'with new naming convention (without _partials suffix)' do
+ it 'renders partials for the given section' do
+ allow(helper).to receive(:render).with('spree/admin/products/test_partial', {}).and_return('partial content')
+
+ result = helper.render_admin_partials(:product_form)
+
+ expect(result).to eq('partial content')
+ end
+
+ it 'passes options to the partials' do
+ allow(helper).to receive(:render).with('spree/admin/products/test_partial', { product: 'test' }).and_return('partial with options')
+
+ result = helper.render_admin_partials(:product_form, { product: 'test' })
+
+ expect(result).to eq('partial with options')
+ end
+
+ it 'renders multiple partials' do
+ allow(Rails.application.config.spree_admin).to receive(:product_form_partials).and_return([
+ 'spree/admin/products/partial1',
+ 'spree/admin/products/partial2'
+ ])
+ allow(helper).to receive(:render).with('spree/admin/products/partial1', {}).and_return('content1')
+ allow(helper).to receive(:render).with('spree/admin/products/partial2', {}).and_return('content2')
+
+ result = helper.render_admin_partials(:product_form)
+
+ expect(result).to eq('content1content2')
+ end
+ end
+
+ context 'with old naming convention (with _partials suffix)' do
+ it 'renders partials for the given section (backward compatibility)' do
+ allow(helper).to receive(:render).with('spree/admin/products/test_partial', {}).and_return('partial content')
+
+ result = helper.render_admin_partials(:product_form_partials)
+
+ expect(result).to eq('partial content')
+ end
+
+ it 'passes options to the partials' do
+ allow(helper).to receive(:render).with('spree/admin/products/test_partial', { product: 'test' }).and_return('partial with options')
+
+ result = helper.render_admin_partials(:product_form_partials, { product: 'test' })
+
+ expect(result).to eq('partial with options')
+ end
+ end
+
+ context 'with empty partials array' do
+ it 'returns empty string' do
+ allow(Rails.application.config.spree_admin).to receive(:dashboard_analytics_partials).and_return([])
+
+ result = helper.render_admin_partials(:dashboard_analytics)
+
+ expect(result).to eq('')
+ end
+ end
+ end
end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 0c7d4b5d71..417ecb7568 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -192,6 +192,7 @@ require 'spree/service_module'
require 'spree/database_type_utilities'
require 'spree/analytics'
+require 'spree/core/partials'
require 'spree/core/importer'
require 'spree/core/query_filters'
require 'spree/core/controller_helpers/auth'
diff --git a/core/lib/spree/core/partials.rb b/core/lib/spree/core/partials.rb
new file mode 100644
index 0000000000..c7b1204b65
--- /dev/null
+++ b/core/lib/spree/core/partials.rb
@@ -0,0 +1,42 @@
+# Ability to define partial injection points for Admin and Storefront
+module Spree
+ module Core
+ class Partials
+ def initialize(config, environment)
+ @config = config
+ @environment = environment
+ define_dynamic_methods
+ end
+
+ attr_reader :config, :environment
+
+ def partial_members
+ environment.members.select { |member| member.to_s.end_with?('_partials') }
+ end
+
+ def keys
+ partial_members.map { |member| member.to_s.sub(/_partials$/, '') }
+ end
+
+ private
+
+ def define_dynamic_methods
+ # Get all members that end with _partials from the Environment
+ partial_members.each do |member|
+ # Strip the _partials suffix for the method name
+ method_name = member.to_s.sub(/_partials$/, '')
+
+ # Define getter method on singleton class
+ singleton_class.define_method(method_name) do
+ config.send(member)
+ end
+
+ # Define setter method on singleton class
+ singleton_class.define_method("#{method_name}=") do |value|
+ config.send("#{member}=", value)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/core/spec/lib/spree/core/partials_spec.rb b/core/spec/lib/spree/core/partials_spec.rb
new file mode 100644
index 0000000000..cddf53903c
--- /dev/null
+++ b/core/spec/lib/spree/core/partials_spec.rb
@@ -0,0 +1,103 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Core::Partials do
+ let(:environment) do
+ Struct.new(
+ :product_form_partials,
+ :dashboard_analytics_partials,
+ :order_page_sidebar_partials,
+ :admin_users_actions_partials,
+ :navigation # Not a _partials attribute
+ )
+ end
+ let(:config) { environment.new }
+ let(:partials) { described_class.new(config, environment) }
+
+ describe '#initialize' do
+ it 'accepts config and environment parameters' do
+ expect { described_class.new(config, environment) }.not_to raise_error
+ end
+
+ it 'sets config and environment' do
+ expect(partials.config).to eq(config)
+ expect(partials.environment).to eq(environment)
+ end
+ end
+
+ describe '#partial_members' do
+ it 'returns only members ending with _partials' do
+ members = partials.partial_members
+ expect(members).to include(:product_form_partials)
+ expect(members).to include(:dashboard_analytics_partials)
+ expect(members).to include(:order_page_sidebar_partials)
+ expect(members).to include(:admin_users_actions_partials)
+ expect(members).not_to include(:navigation)
+ end
+ end
+
+ describe '#keys' do
+ it 'returns partial member names without _partials suffix' do
+ keys = partials.keys
+ expect(keys).to include('product_form')
+ expect(keys).to include('dashboard_analytics')
+ expect(keys).to include('order_page_sidebar')
+ expect(keys).to include('admin_users_actions')
+ expect(keys).not_to include('navigation')
+ end
+ end
+
+ describe 'dynamically defined methods' do
+ it 'defines getter methods for all _partials attributes' do
+ expect(partials).to respond_to(:product_form)
+ expect(partials).to respond_to(:order_page_sidebar)
+ expect(partials).to respond_to(:dashboard_analytics)
+ expect(partials).to respond_to(:admin_users_actions)
+ end
+
+ it 'defines setter methods for all _partials attributes' do
+ expect(partials).to respond_to(:product_form=)
+ expect(partials).to respond_to(:order_page_sidebar=)
+ expect(partials).to respond_to(:dashboard_analytics=)
+ expect(partials).to respond_to(:admin_users_actions=)
+ end
+
+ it 'does not define methods for non-partials attributes' do
+ # :navigation is in Environment but doesn't end with _partials
+ expect(partials).not_to respond_to(:navigation)
+ end
+ end
+
+ describe 'getter methods' do
+ it 'calls the config with the full _partials attribute name' do
+ config.product_form_partials = ['partial1', 'partial2']
+
+ result = partials.product_form
+
+ expect(result).to eq(['partial1', 'partial2'])
+ end
+
+ it 'works with different partial types' do
+ config.dashboard_analytics_partials = ['analytics']
+ config.order_page_sidebar_partials = ['sidebar']
+
+ expect(partials.dashboard_analytics).to eq(['analytics'])
+ expect(partials.order_page_sidebar).to eq(['sidebar'])
+ end
+ end
+
+ describe 'setter methods' do
+ it 'sets the config with the full _partials attribute name' do
+ partials.product_form = ['custom_partial']
+
+ expect(config.product_form_partials).to eq(['custom_partial'])
+ end
+
+ it 'works with different partial types' do
+ partials.dashboard_analytics = ['new_analytics']
+ partials.order_page_sidebar = ['new_sidebar']
+
+ expect(config.dashboard_analytics_partials).to eq(['new_analytics'])
+ expect(config.order_page_sidebar_partials).to eq(['new_sidebar'])
+ end
+ end
+end
diff --git a/storefront/app/helpers/spree/storefront_helper.rb b/storefront/app/helpers/spree/storefront_helper.rb
index 1ed688d9d2..b4fba01acd 100644
--- a/storefront/app/helpers/spree/storefront_helper.rb
+++ b/storefront/app/helpers/spree/storefront_helper.rb
@@ -29,7 +29,7 @@ module Spree
# @param options [Hash] The options/variables to pass to the partials
# @return [String] The rendered partials
def render_storefront_partials(section, options = {})
- Rails.application.config.spree_storefront.send(section).map do |partial|
+ Spree::Storefront.partials.send(section.to_s.gsub('_partials', '').to_sym).map do |partial|
render partial, options
end.join.html_safe
end
diff --git a/storefront/lib/spree/storefront.rb b/storefront/lib/spree/storefront.rb
index ec339f7616..c5d56100d3 100644
--- a/storefront/lib/spree/storefront.rb
+++ b/storefront/lib/spree/storefront.rb
@@ -12,3 +12,15 @@ require 'turbo-rails'
require 'inline_svg'
require 'spree/storefront/engine'
+require 'spree/core/partials'
+
+module Spree
+ module Storefront
+ def self.partials
+ @partials ||= Spree::Core::Partials.new(
+ Rails.application.config.spree_storefront,
+ Spree::Storefront::Engine::Environment
+ )
+ end
+ end
+end
diff --git a/storefront/spec/helpers/spree/storefront_helper_spec.rb b/storefront/spec/helpers/spree/storefront_helper_spec.rb
index 0120399976..e996970fcf 100644
--- a/storefront/spec/helpers/spree/storefront_helper_spec.rb
+++ b/storefront/spec/helpers/spree/storefront_helper_spec.rb
@@ -113,4 +113,70 @@ describe Spree::StorefrontHelper, type: :helper do
expect(svg_country_icon('fr')).to include('fi fi-fr')
end
end
+
+ describe '#render_storefront_partials' do
+ before do
+ # Set up test partials
+ allow(Rails.application.config.spree_storefront).to receive(:cart_partials).and_return(['spree/storefront/cart/test_partial'])
+ end
+
+ context 'with new naming convention (without _partials suffix)' do
+ it 'renders partials for the given section' do
+ allow(helper).to receive(:render).with('spree/storefront/cart/test_partial', {}).and_return('partial content')
+
+ result = helper.render_storefront_partials(:cart)
+
+ expect(result).to eq('partial content')
+ end
+
+ it 'passes options to the partials' do
+ allow(helper).to receive(:render).with('spree/storefront/cart/test_partial', { order: 'test' }).and_return('partial with options')
+
+ result = helper.render_storefront_partials(:cart, { order: 'test' })
+
+ expect(result).to eq('partial with options')
+ end
+
+ it 'renders multiple partials' do
+ allow(Rails.application.config.spree_storefront).to receive(:cart_partials).and_return([
+ 'spree/storefront/cart/partial1',
+ 'spree/storefront/cart/partial2'
+ ])
+ allow(helper).to receive(:render).with('spree/storefront/cart/partial1', {}).and_return('content1')
+ allow(helper).to receive(:render).with('spree/storefront/cart/partial2', {}).and_return('content2')
+
+ result = helper.render_storefront_partials(:cart)
+
+ expect(result).to eq('content1content2')
+ end
+ end
+
+ context 'with old naming convention (with _partials suffix)' do
+ it 'renders partials for the given section (backward compatibility)' do
+ allow(helper).to receive(:render).with('spree/storefront/cart/test_partial', {}).and_return('partial content')
+
+ result = helper.render_storefront_partials(:cart_partials)
+
+ expect(result).to eq('partial content')
+ end
+
+ it 'passes options to the partials' do
+ allow(helper).to receive(:render).with('spree/storefront/cart/test_partial', { order: 'test' }).and_return('partial with options')
+
+ result = helper.render_storefront_partials(:cart_partials, { order: 'test' })
+
+ expect(result).to eq('partial with options')
+ end
+ end
+
+ context 'with empty partials array' do
+ it 'returns empty string' do
+ allow(Rails.application.config.spree_storefront).to receive(:product_partials).and_return([])
+
+ result = helper.render_storefront_partials(:product)
+
+ expect(result).to eq('')
+ end
+ end
+ end
end