Module: Phlex::Rails::SGML

Included in:
HTML, SVG
Defined in:
lib/phlex/rails/sgml.rb

Defined Under Namespace

Modules: ClassMethods, State

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/phlex/rails/sgml.rb', line 10

def method_missing(name, ...)
	super
rescue NoMethodError => e
	if rendering? && view_context.respond_to?(name)
		const_name = name.to_s.gsub("?", "")

		module_name = Phlex::Rails::Helpers.constants.find do |mod|
			mod.name.underscore.gsub("domid", "dom_id") == const_name
		end

		# If we're missing a helper module, raise a NoMethodError with a helpful message,
		# otherwise re-raise the original error.
		if module_name
			raise NoMethodError.new(<<~MESSAGE)
				Try including `Phlex::Rails::Helpers::#{module_name}` in #{self.class.name}.
			MESSAGE
		else
			raise e
		end
	else
		raise e
	end
end

Instance Method Details

#capture_contextObject

If we’re rendered from view_component, we need to capture on the view_component context.



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

def capture_context
	context[:capture_context]
end

#enable_cache_reloading?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/phlex/rails/sgml.rb', line 135

def enable_cache_reloading?
	Rails.env.development?
end

#helpersObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/phlex/rails/sgml.rb', line 42

def helpers
	warn <<~MESSAGE
		The `helpers` method is deprecated and will be removed in the next
		minor version of phlex-rails.

		If you absolutely need to access the underlying Rails view context,
		you can do that via the `view_context` method, though this is
		not recommended.

		It is much safer to use one of the built-in helper adapters or to
		register your own adapter via `register_output_helper` or
		`register_value_helper`.

		See https://www.phlex.fun/rails/helpers
	MESSAGE

	view_context
end

#low_level_cacheObject



99
100
101
# File 'lib/phlex/rails/sgml.rb', line 99

def low_level_cache(...)
	Rails.application.config.action_controller.perform_caching ? super : yield
end

#partial(&block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/phlex/rails/sgml.rb', line 34

def partial(*, **, &block)
	if block
		Phlex::Rails::Partial.new(*, **) { capture(&block) }
	else
		Phlex::Rails::Partial.new(*, **)
	end
end

#render(renderable = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/phlex/rails/sgml.rb', line 76

def render(renderable = nil, &block)
	case renderable
	when Phlex::SGML, Proc, Method, String
		return super
	when Class
		return super if renderable < Phlex::SGML
	when Enumerable
		return super unless defined?(ActiveRecord::Relation) && ActiveRecord::Relation === renderable
	when nil
		return super
	end

	if renderable.respond_to?(:render_in) || renderable.respond_to?(:to_partial_path)
		if block
			raw capture_context.render(renderable) { |*a| capture(*a, &block) }
		else
			raw capture_context.render(renderable)
		end
	else
		super
	end
end

#render_in(view_context, &erb) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/phlex/rails/sgml.rb', line 103

def render_in(view_context, &erb)
	case view_context
	when defined?(ViewComponent::Base) && ViewComponent::Base
		rails_view_context = view_context.helpers
		capture_context = view_context
	else
		rails_view_context = view_context
		capture_context = view_context
	end

	context = {
		rails_view_context:,
		capture_context:,
	}

	fragments = if (request = context[:rails_view_context].request) && (fragment_header = request.headers["X-Fragments"])
		fragment_header.split(",").map(&:strip).presence
	end

	capture_context.capture do
		if erb
			call(context:, fragments:, &erb).html_safe
		else
			call(context:, fragments:).html_safe
		end
	end
end

#set_original_view_context(view_context) ⇒ Object



131
132
133
# File 'lib/phlex/rails/sgml.rb', line 131

def set_original_view_context(view_context)
	# no-op (see https://github.com/ViewComponent/view_component/issues/2207)
end

#view_contextObject



61
62
63
64
65
66
67
68
69
# File 'lib/phlex/rails/sgml.rb', line 61

def view_context
	if rendering?
		context[:rails_view_context]
	else
		raise Phlex::Rails::HelpersCalledBeforeRenderError.new(<<~MESSAGE)
			You can’t use Rails view helpers until after the component has started rendering.
		MESSAGE
	end
end