Module: MaquinaStream::ComponentsHelper

Defined in:
app/helpers/maquina_stream/components_helper.rb

Overview

The view side of the component seam. Engine views and the render pipeline call component; nothing renders a vendored partial by path.

Instance Method Summary collapse

Instance Method Details

#component(name, **locals, &block) ⇒ Object

Renders one component through the seam.

<%= component(:code_block, lang: "ruby", source: raw, css_classes: "…") %>

name resolves to a maquina_components partial when that gem defines it, and to the vendored fallback otherwise. Locals go through MaquinaStream::Components::Contract on the way in.

The partial is generic; the engine's DOM contract and labels are added here, by MaquinaStream::Components::Contract. That is what lets a vendored partial leave for maquina_components without carrying data-ms-* or the maquina_stream.* locale namespace with it.



21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/maquina_stream/components_helper.rb', line 21

def component(name, **locals, &block)
  config = maquina_stream_config
  partial = MaquinaStream::Components.partial_for(name, config: config)
  locals = MaquinaStream::Components::Contract.apply(name, locals, config: config)

  if block
    render(layout: partial, locals: locals, &block)
  else
    render(partial: partial, locals: locals)
  end
end

#component_classes(*tokens) ⇒ Object



66
67
68
# File 'app/helpers/maquina_stream/components_helper.rb', line 66

def component_classes(*tokens)
  tokens.flatten.compact.map(&:to_s).reject(&:empty?).join(" ").presence
end

#component_data(own, provided = nil) ⇒ Object

Merges caller-supplied data attributes with the component's own.

The component wins its identity keys — component, variant, size and anything ending in _part — because those are what the CSS selects on. controller and action concatenate, component tokens first. Every other key belongs to the caller.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/maquina_stream/components_helper.rb', line 39

def component_data(own, provided = nil)
  own = own.compact
  provided = (provided || {}).transform_keys { |key| key.to_s.tr("-", "_").to_sym }

  merged = provided.merge(own) do |key, caller_value, own_value|
    if %i[controller action].include?(key)
      [own_value, caller_value].compact.join(" ").strip
    else
      own_value
    end
  end

  merged.reject { |_, value| value.nil? }
end

#component_html_options(html_options, own_data: {}, css_classes: nil, base_classes: nil) ⇒ Object

Same-shaped merge for the non-data attributes a caller passes through **html_options, with css_classes folded into class.



56
57
58
59
60
61
62
63
64
# File 'app/helpers/maquina_stream/components_helper.rb', line 56

def component_html_options(html_options, own_data: {}, css_classes: nil, base_classes: nil)
  options = html_options.dup
  provided_data = options.delete(:data) || options.delete("data")

  options[:data] = component_data(own_data, provided_data)
  classes = component_classes(base_classes, css_classes)
  options[:class] = classes if classes.present?
  options
end

#component_stylesheets ⇒ Object

Stylesheet paths for the components currently rendering from our own app/views. Empty once maquina_components serves all of them.



72
73
74
# File 'app/helpers/maquina_stream/components_helper.rb', line 72

def component_stylesheets
  MaquinaStream::Components.stylesheets(config: maquina_stream_config)
end

#maquina_stream_config ⇒ Object



76
77
78
# File 'app/helpers/maquina_stream/components_helper.rb', line 76

def maquina_stream_config
  MaquinaStream.config
end