lib/lookbook/preview.rb in lookbook-1.0.0.beta.2 vs lib/lookbook/preview.rb in lookbook-1.0.0.beta.3
- old
+ new
@@ -6,11 +6,11 @@
delegate :position, :group, :notes, :hidden?, to: :@preview_inspector
def initialize(preview)
@preview = preview
@preview_inspector = SourceInspector.new(@preview.name)
- super(preview_class_name(preview_class_basename(@preview.name)))
+ super(preview_class_path(@preview.name))
end
def id
@preview_inspector&.id || generate_id(lookup_path)
end
@@ -32,11 +32,11 @@
end
def examples
return @examples if @examples.present?
public_methods = @preview.public_instance_methods(false)
- public_method_objects = @preview_inspector&.methods&.filter { |m| public_methods.include?(m.name) }
+ public_method_objects = @preview_inspector&.methods&.select { |m| public_methods.include?(m.name) }
examples = (public_method_objects || []).map { |m| PreviewExample.new(m.name.to_s, self) }
sorted = Lookbook.config.sort_examples ? examples.sort_by(&:label) : examples
@examples = []
if @preview_inspector&.groups&.any?
sorted.group_by { |m| m.group }.each do |name, examples|
@@ -60,24 +60,20 @@
def default_example
examples.first
end
def full_path
- base_path = Array(preview_paths).detect do |preview_path|
+ base_path = Array(Lookbook.config.preview_paths).detect do |preview_path|
Dir["#{preview_path}/#{name.underscore}.rb"].first
end
Pathname.new(Dir["#{base_path}/#{name.underscore}.rb"].first)
end
def url_path
lookbook_inspect_path lookup_path
end
- def preview_paths
- ViewComponent::Base.preview_paths
- end
-
def parent_collections_names
File.dirname(path).split("/")
end
def hierarchy_depth
@@ -92,9 +88,30 @@
Lookbook.config.preview_display_params.deep_merge(@preview_inspector&.display_params)
end
def collapsible?
true
+ end
+
+ def component
+ components.first
+ end
+
+ def components
+ component_classes = @preview_inspector&.components.any? ? @preview_inspector&.components : [guess_component]
+ component_classes.map do |class_name|
+ Component.new(class_name.to_s)
+ end
+ end
+
+ protected
+
+ def guess_component
+ begin
+ name.chomp("Preview").constantize
+ rescue
+ nil
+ end
end
class << self
def find(path)
all.find { |p| p.lookup_path == path }