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_shortcutObject



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_shortcutObject



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

Parameters:

  • shortcut (Symbol)

    What the shortcut helper is going to be called by

  • process (Proc)

    Proc to process what to do with the leaf component. E.g instantiating a new class or rendering a component. It’s invoked with a single arguments: An instance of VcHelper::Context. See it’s doc for what’s available.

  • find_component (Proc) (defaults to: nil)

    Proc to find the component class given the current camelized name Lower level than find. If you supply find_component, you shouldn’t supply find. And vice-versa.

  • find (Proc) (defaults to: nil)

    Proc to find the component class given the current chain Optional. If you don’t pass it, we’ll try to camelize the chain, and try finding a component or module. You might want to define this if you have got some custom lookup logic. It’s invoked with a single argument: An instance of VcHelper::Context. See it’s doc for what’s available If you return :has_more, we’ll assume there’s another component coming up in the chain If you return a non-nil value, we’ll assume this is the leaf component. If you return nil, we’ll assume we couldn’t find any component for the given chain and raise an error



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