Class: ViewComponent::Preview

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/view_component/preview.rb

Direct Known Subclasses

ActionView::Component::Preview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject

Returns all component preview classes.



16
17
18
19
# File 'lib/view_component/preview.rb', line 16

def all
  load_previews if descendants.empty?
  descendants
end

.componentObject

Returns the component object class associated to the preview.



27
28
29
# File 'lib/view_component/preview.rb', line 27

def component
  name.chomp("Preview").constantize
end

.example_exists?(example) ⇒ Boolean

Returns true if the example of the component preview exists.

Returns:

  • (Boolean)


37
38
39
# File 'lib/view_component/preview.rb', line 37

def example_exists?(example)
  examples.include?(example)
end

.examplesObject

Returns all of the available examples for the component preview.



32
33
34
# File 'lib/view_component/preview.rb', line 32

def examples
  public_instance_methods(false).map(&:to_s).sort
end

.exists?(preview) ⇒ Boolean

Returns true if the preview exists.

Returns:

  • (Boolean)


42
43
44
# File 'lib/view_component/preview.rb', line 42

def exists?(preview)
  all.any? { |p| p.preview_name == preview }
end

.find(preview) ⇒ Object

Find a component preview by its underscored class name.



47
48
49
# File 'lib/view_component/preview.rb', line 47

def find(preview)
  all.find { |p| p.preview_name == preview }
end

.layout(layout_name) ⇒ Object

Setter for layout name.



57
58
59
# File 'lib/view_component/preview.rb', line 57

def layout(layout_name)
  @layout = layout_name
end

.preview_nameObject

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



52
53
54
# File 'lib/view_component/preview.rb', line 52

def preview_name
  name.chomp("Preview").underscore
end

.render_args(example) ⇒ Object

Returns the arguments for rendering of the component in its layout



22
23
24
# File 'lib/view_component/preview.rb', line 22

def render_args(example)
  new.public_send(example).merge(layout: @layout)
end

Instance Method Details

#render(component, **args, &block) ⇒ Object



10
11
12
# File 'lib/view_component/preview.rb', line 10

def render(component, **args, &block)
  { component: component, args: args, block: block }
end