class ViewComponent::Preview

Public Class Methods

all() click to toggle source

Returns all component preview classes.

# File lib/view_component/preview.rb, line 16
def all
  load_previews if descendants.empty?
  descendants
end
component() click to toggle source

Returns the component object class associated to the preview.

# File lib/view_component/preview.rb, line 27
def component
  name.chomp("Preview").constantize
end
example_exists?(example) click to toggle source

Returns true if the example of the component preview exists.

# File lib/view_component/preview.rb, line 37
def example_exists?(example)
  examples.include?(example)
end
examples() click to toggle source

Returns all of the available examples for the component preview.

# File lib/view_component/preview.rb, line 32
def examples
  public_instance_methods(false).map(&:to_s).sort
end
exists?(preview) click to toggle source

Returns true if the preview exists.

# File lib/view_component/preview.rb, line 42
def exists?(preview)
  all.any? { |p| p.preview_name == preview }
end
find(preview) click to toggle source

Find a component preview by its underscored class name.

# File lib/view_component/preview.rb, line 47
def find(preview)
  all.find { |p| p.preview_name == preview }
end
layout(layout_name) click to toggle source

Setter for layout name.

# File lib/view_component/preview.rb, line 57
def layout(layout_name)
  @layout = layout_name
end
preview_name() click to toggle source

Returns the underscored name of the component preview without the suffix.

# File lib/view_component/preview.rb, line 52
def preview_name
  name.chomp("Preview").underscore
end
render_args(example) click to toggle source

Returns the arguments for rendering of the component in its layout

# File lib/view_component/preview.rb, line 22
def render_args(example)
  new.public_send(example).merge(layout: @layout)
end

Private Class Methods

load_previews() click to toggle source
# File lib/view_component/preview.rb, line 63
def load_previews
  if preview_path
    Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
  end
end
preview_path() click to toggle source
# File lib/view_component/preview.rb, line 69
def preview_path
  Base.preview_path
end
show_previews() click to toggle source
# File lib/view_component/preview.rb, line 73
def show_previews
  Base.show_previews
end

Public Instance Methods

render(component, **args, &block) click to toggle source
# File lib/view_component/preview.rb, line 10
def render(component, **args, &block)
  { component: component, args: args, block: block }
end