Module: Phlex::Rails::SGML::Overrides

Defined in:
lib/phlex/rails/sgml.rb

Instance Method Summary collapse

Instance Method Details

#captureObject



73
74
75
# File 'lib/phlex/rails/sgml.rb', line 73

def capture
	super&.html_safe
end

#helpersObject



13
14
15
16
17
18
19
# File 'lib/phlex/rails/sgml.rb', line 13

def helpers
	if defined?(ViewComponent::Base) && @_view_context.is_a?(ViewComponent::Base)
		@_view_context.helpers
	else
		@_view_context
	end
end

#render(*args, **kwargs, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/phlex/rails/sgml.rb', line 21

def render(*args, **kwargs, &block)
	renderable = args[0]

	case renderable
	when Phlex::SGML, Proc, Method
		return super
	when Class
		return super if renderable < Phlex::SGML
	when Enumerable
		return super unless renderable.is_a?(ActiveRecord::Relation)
	else
		if block
			@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
		else
			@_context.target << @_view_context.render(*args, **kwargs)
		end
	end

	nil
end

#render_in(view_context, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/phlex/rails/sgml.rb', line 42

def render_in(view_context, &block)
	fragments = if view_context.request && (fragment_header = view_context.request.headers["X-Fragment"])
		fragment_header.split
	end

	if block_given?
		call(view_context: view_context, fragments: fragments) do |*args|
			original_length = @_context.target.bytesize

			if args.length == 1 && Phlex::SGML === args[0] && !block.source_location&.[](0)&.end_with?(".rb")
				output = view_context.capture(
					args[0].unbuffered, &block
				)
			else
				output = view_context.capture(*args, &block)
			end

			unchanged = (original_length == @_context.target.bytesize)

			if unchanged
				case output
				when ActiveSupport::SafeBuffer
					@_context.target << output
				end
			end
		end.html_safe
	else
		call(view_context: view_context, fragments: fragments).html_safe
	end
end