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.

Allow to use custom page blocks defined in section (#12683)

Damian Legawiec68f644df3294
diff --git a/admin/app/controllers/spree/admin/page_blocks_controller.rb b/admin/app/controllers/spree/admin/page_blocks_controller.rb
index 4309900b02..f97c1ca6ee 100644
--- a/admin/app/controllers/spree/admin/page_blocks_controller.rb
+++ b/admin/app/controllers/spree/admin/page_blocks_controller.rb
@@ -26,15 +26,23 @@ module Spree
end
def create
- allowed_types = Rails.application.config.spree.page_blocks.map(&:to_s)
page_block_type = params.dig(:page_block, :type)
- if allowed_types.include?(page_block_type) && page_block_type.safe_constantize.present?
+ if allowed_types.map(&:to_s).include?(page_block_type) && page_block_type.safe_constantize.present?
@page_block = page_block_type.constantize.new
@page_block.section = @page_section
@page_block.save!
end
end
+
+ private
+
+ def allowed_types
+ [
+ *Rails.application.config.spree.page_blocks,
+ *parent&.available_blocks_to_add
+ ].uniq.sort_by(&:name)
+ end
end
end
end
diff --git a/admin/spec/controllers/spree/admin/page_blocks_controller_spec.rb b/admin/spec/controllers/spree/admin/page_blocks_controller_spec.rb
index e2ae987431..0c0e5d8996 100644
--- a/admin/spec/controllers/spree/admin/page_blocks_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/page_blocks_controller_spec.rb
@@ -68,4 +68,42 @@ RSpec.describe Spree::Admin::PageBlocksController, type: :controller do
end
end
end
+
+ describe '#allowed_types' do
+ subject { controller.send(:allowed_types) }
+
+ let(:page_section) { create(:featured_taxon_page_section) }
+
+ before do
+ allow(controller).to receive(:parent).and_return(page_section)
+ end
+
+ it 'returns the section allowed types' do
+ expect(subject).to include(
+ Spree::PageBlocks::Buttons,
+ Spree::PageBlocks::Image,
+ Spree::PageBlocks::Text
+ )
+ end
+
+ context 'with custom page section class' do
+ class CustomPageBlock < Spree::PageBlock
+ def self.name
+ 'Spree::PageBlocks::Custom'
+ end
+ end
+
+ class CustomPageSection < Spree::PageSections::FeaturedTaxon
+ def available_blocks_to_add
+ [CustomPageBlock]
+ end
+ end
+
+ let(:page_section) { CustomPageSection.new }
+
+ it 'returns the section allowed types' do
+ expect(subject).to include(CustomPageBlock)
+ end
+ end
+ end
end