Module: ComponentHelper

Defined in:
app/helpers/component_helper.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, **kwargs, &block) ⇒ Object

Create a component in a view with shorthand syntax. This is the same as calling component with the same arguments.

Examples:


= header do |c|
  - c.title = "Hello"


48
49
50
51
52
# File 'app/helpers/component_helper.rb', line 48

def method_missing symbol, *args, **kwargs, &block
  super unless respond_to?(symbol)

  component symbol, *args, **kwargs, &block
end

Instance Method Details

#component(name, **kwargs, &block) ⇒ Object Also known as: comp

Create a component in a view.

other components inside this block. You can set locals inside this block.

Examples:


= component :header do |c|
  - c.title = "Hello"
  p My Extra Content

Parameters:

  • name (Symbol)

    The name of the component.

  • kwargs (Hash)

    The locals to pass to the component.

  • block (Proc)

    The block to pass to the component. You can use



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/component_helper.rb', line 16

def component name, **kwargs, &block
  component = Component.new(name, kwargs, lookup_context, nil, block)

  component._renderer = proc do |partial, locals, captured|
    render partial, locals do
      captured
    end
  end

  component._capture = proc do |component, block|
    if block
      capture do
        block.call(component)
      end
    end
  end

  component._capture_self
  component._yield_renderer
end

#respond_to_missing?(symbol, *args) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


55
56
57
58
# File 'app/helpers/component_helper.rb', line 55

def respond_to_missing? symbol, *args
  lookup_context.exists?("components/#{symbol}/#{symbol}", [], true) ||
    lookup_context.exists?("components/#{symbol}", [], true)
end