Module: MaquinaStream::Components

Defined in:
lib/maquina_stream/components.rb,
lib/maquina_stream/components/contract.rb

Overview

The component seam.

Every component the engine renders resolves through here, so extracting a vendored component into maquina_components is mechanical: publish the partial in that gem, drop the name from VENDORED_COMPONENTS, done. No call site changes.

MaquinaStream::Components.partial_for(:code_block, config: MaquinaStream.config)
# => "maquina_components/code_block"        when the gem defines it
# => "maquina_stream/components/code_block" otherwise

maquina_components is an optional dependency. Without it every component renders its vendored plain-Tailwind fallback, and config.components = :plain forces that even when the gem is installed.

Defined Under Namespace

Modules: Contract Classes: Library

Constant Summary collapse

DESTINATION_PREFIX =

Where a component lives once it has been extracted.

"maquina_components"
VENDORED_PREFIX =

Where the vendored copy lives while it is still ours.

"maquina_stream/components"
ENGINE_OWNED =

Engine-owned components. Permanent; they never resolve to the gem, even when a partial of the same name shows up there.

%i[shimmer source_citation].freeze

Class Method Summary collapse

Class Method Details

.default_library ⇒ Object



117
118
119
120
121
# File 'lib/maquina_stream/components.rb', line 117

def default_library
  return @default_library if defined?(@default_library) && !@default_library.nil?

  @default_library = Library.detect
end

.engine_owned?(name) ⇒ Boolean

True for a component that is permanently the engine's and never resolves to the gem, even if a partial of the same name shows up there.

Returns:

  • (Boolean)


84
85
86
# File 'lib/maquina_stream/components.rb', line 84

def engine_owned?(name)
  ENGINE_OWNED.include?(name.to_sym)
end

.fallback_active?(name, config: MaquinaStream.config, library: default_library) ⇒ Boolean

True when name renders from our own app/views right now.

Returns:

  • (Boolean)


95
96
97
# File 'lib/maquina_stream/components.rb', line 95

def fallback_active?(name, config: MaquinaStream.config, library: default_library)
  partial_for(name, config: config, library: library).start_with?(VENDORED_PREFIX)
end

.known?(name) ⇒ Boolean

True for any component this engine knows how to render, vendored or engine-owned.

Returns:

  • (Boolean)


90
91
92
# File 'lib/maquina_stream/components.rb', line 90

def known?(name)
  vendored?(name) || engine_owned?(name)
end

.partial_for(name, config: MaquinaStream.config, library: default_library) ⇒ Object

The partial path to render for name.

library is a seam: pass one in to test either side of the branch without touching the load path.



66
67
68
69
70
71
72
73
74
# File 'lib/maquina_stream/components.rb', line 66

def partial_for(name, config: MaquinaStream.config, library: default_library)
  name = name.to_sym

  return vendored_partial(name) if engine_owned?(name)
  return vendored_partial(name) unless config.components == :maquina
  return vendored_partial(name) unless library&.defines?(name)

  "#{DESTINATION_PREFIX}/#{name}"
end

.reset_library! ⇒ Object

Forgets the detected library. Only useful in tests.



124
125
126
# File 'lib/maquina_stream/components.rb', line 124

def reset_library!
  @default_library = nil
end

.styled_components ⇒ Object

The components we actually ship a stylesheet for — a component with no fallback markup yet has no fallback CSS to load either.



110
111
112
113
114
115
# File 'lib/maquina_stream/components.rb', line 110

def styled_components
  @styled_components ||= Pathname(__dir__).join("../../app/assets/stylesheets", VENDORED_PREFIX)
    .glob("*.css")
    .map { |path| path.basename(".css").to_s.to_sym }
    .sort
end

.stylesheets(config: MaquinaStream.config, library: default_library) ⇒ Object

Asset paths for the components currently rendering from our app/views. One stylesheet per component, and none for a component the gem is serving — otherwise the day it ships we emit its selectors twice.



102
103
104
105
106
# File 'lib/maquina_stream/components.rb', line 102

def stylesheets(config: MaquinaStream.config, library: default_library)
  styled_components
    .select { |name| fallback_active?(name, config: config, library: library) }
    .map { |name| "#{VENDORED_PREFIX}/#{name}" }
end

.vendored?(name) ⇒ Boolean

True for a component that is vendored here and destined for the gem.

Returns:

  • (Boolean)


77
78
79
# File 'lib/maquina_stream/components.rb', line 77

def vendored?(name)
  MaquinaStream::VENDORED_COMPONENTS.include?(name.to_sym)
end