Class: ViewComponent::Preview

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
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.



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

def all
  load_previews if descendants.empty?
  descendants
end

.componentObject

Returns the component object class associated to the preview.



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

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

.example_exists?(example) ⇒ Boolean

Returns true if the example of the component preview exists.

Returns:

  • (Boolean)


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

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

.examplesObject

Returns all of the available examples for the component preview.



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

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

.exists?(preview) ⇒ Boolean

Returns true if the preview exists.

Returns:

  • (Boolean)


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

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

.find(preview) ⇒ Object

Find a component preview by its underscored class name.



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

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

.layout(layout_name) ⇒ Object

Setter for layout name.



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

def layout(layout_name)
  @layout = layout_name
end

.preview_nameObject

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



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

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

.render_args(example) ⇒ Object

Returns the arguments for rendering of the component in its layout



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

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

Instance Method Details

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



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

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