From 5c99be1338384bdb31c05cadf8122034abd5e60e Mon Sep 17 00:00:00 2001 From: ydah Date: Mon, 15 Dec 2025 00:57:36 +0900 Subject: [PATCH] Refactor: Extract block capturing logic to helper method Extracted the block capturing logic from build_tag into a new private method capture_block_content. This resolves the TODO comment about creating a helper for universal capturing, improving code readability and maintainability. The new method encapsulates the logic for choosing between Rails' capture method (when disable_capture is true) and Slim's native yielding, making it reusable and easier to maintain. --- lib/slim/splat/builder.rb | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/slim/splat/builder.rb b/lib/slim/splat/builder.rb index c7463a21..e5d18383 100644 --- a/lib/slim/splat/builder.rb +++ b/lib/slim/splat/builder.rb @@ -48,25 +48,7 @@ def build_tag(&block) tag = @attrs.delete('tag').to_s tag = @options[:default_tag] if tag.empty? if block - # This is a bit of a hack to get a universal capturing. - # - # TODO: Add this as a helper somewhere to solve these capturing issues - # once and for all. - # - # If we have Slim capturing disabled and the scope defines the method `capture` (i.e. Rails) - # we use this method to capture the content. - # - # otherwise we just use normal Slim capturing (yield). - # - # See https://github.com/slim-template/slim/issues/591 - # https://github.com/slim-template/slim#helpers-capturing-and-includes - # - content = - if @options[:disable_capture] && (scope = block.binding.eval('self')).respond_to?(:capture) - scope.capture(&block) - else - yield - end + content = capture_block_content(&block) "<#{tag}#{build_attrs}>#{content}" else "<#{tag}#{build_attrs} />" @@ -90,6 +72,23 @@ def build_attrs private + # Captures block content using the appropriate capturing mechanism. + # + # If we have Slim capturing disabled and the scope defines the method `capture` (i.e. Rails) + # we use this method to capture the content. + # + # Otherwise we just use normal Slim capturing (yield). + # + # See https://github.com/slim-template/slim/issues/591 + # https://github.com/slim-template/slim#helpers-capturing-and-includes + def capture_block_content(&block) + if @options[:disable_capture] && (scope = block.binding.eval('self')).respond_to?(:capture) + scope.capture(&block) + else + yield + end + end + def hyphen_attr(name, escape, value) if Hash === value if @options[:hyphen_underscore_attrs]