Sha256: 7c3fb76229c28ad2765eb99256ae790db1e306bac24f5b3c7a4e0ca41105430b

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Phlex::Rails::HelperMacros
	# Register a Rails helper that returns safe HTML to be pushed to the output buffer.
	def register_output_helper(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs, &block)
  			context = @_context
  			return if context.fragments && !context.in_target_fragment

				output = if block
					helpers.#{method_name}(*args, **kwargs) { capture(&block) }
				else
					helpers.#{method_name}(*args, **kwargs)
				end

				case output
				when ActiveSupport::SafeBuffer
					@_context.target << output
				end

				nil
			end
		RUBY
	end

	# Register a Rails helper that returns a value that shouldn’t be pushed to the output buffer.
	def register_value_helper(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs, &block)
				if block
					helpers.#{method_name}(*args, **kwargs) { capture(&block) }
				else
					helpers.#{method_name}(*args, **kwargs)
				end
			end
		RUBY
	end

	# @api private
	def register_builder_yielding_helper(method_name, builder)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs)
  			context = @_context
  			return if context.fragments && !context.in_target_fragment
				output = if block_given?
					helpers.#{method_name}(*args, **kwargs) { |form|
						capture do
							yield(
								#{builder.name}.new(form,
									view: self
								)
							)
						end
					}
				else
					helpers.#{method_name}(*args, **kwargs)
				end

				case output
				when ActiveSupport::SafeBuffer
					@_context.target << output
				end

				nil
			end
		RUBY
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phlex-rails-1.2.2 lib/phlex/rails/helper_macros.rb
phlex-rails-1.2.1 lib/phlex/rails/helper_macros.rb
phlex-rails-1.2.0 lib/phlex/rails/helper_macros.rb