Module: VcShortcut
- Defined in:
- lib/vc_shortcut.rb,
lib/vc_shortcut/railtie.rb,
lib/vc_shortcut/version.rb,
lib/vc_shortcut/register.rb,
lib/vc_shortcut/chain_context.rb,
lib/vc_shortcut/chain_manager.rb
Defined Under Namespace
Classes: ChainContext, ChainManager, Railtie
Constant Summary
collapse
- VERSION =
"1.0.0"
Class Method Summary
collapse
Class Method Details
.define_instantiate_shortcut ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/vc_shortcut/railtie.rb', line 25
def self.define_instantiate_shortcut
return unless VcShortcut.instantiate_shortcut
VcShortcut.register(VcShortcut.instantiate_shortcut,
process: ->(context) { context.component.new(*context.call_args, **context.call_kwargs) }
)
end
|
.define_render_shortcut ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/vc_shortcut/railtie.rb', line 16
def self.define_render_shortcut
return unless VcShortcut.render_shortcut
VcShortcut.register(VcShortcut.render_shortcut,
process: ->(context) { context.view_context.render(context.component.new(*context.call_args, **context.call_kwargs), &context.call_block) }
)
end
|
.register(shortcut, process:, find_component: nil, find: nil) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/vc_shortcut/register.rb', line 19
def self.register(shortcut, process:, find_component: nil, find: nil)
raise "You should only provide either find_component: or find:. Not both" if find_component && find
find_component ||= VcShortcut.find_component
find ||= ->(context) {
chain_camelized = context.chain_camelized
component = find_component.call(chain_camelized)
next component if component
:has_more if chain_camelized.safe_constantize
}
helper_module = Module.new do
define_method(shortcut) do |view_context = nil|
ChainManager.new(shortcut, process, find, view_context || self)
end
end
ActiveSupport.on_load(:action_view) do
include helper_module
end
ViewComponent::Base.class_eval do
define_method(shortcut) do
helpers.send(shortcut, self)
end
end if defined?(ViewComponent::Base)
Phlex::HTML.class_eval do
define_method(shortcut) do
helpers.send(shortcut, self)
end
end if defined?(Phlex::HTML)
helper_module
end
|