lib/lookbook/preview.rb in lookbook-0.2.4 vs lib/lookbook/preview.rb in lookbook-0.3.0.beta.0
- old
+ new
@@ -1,9 +1,13 @@
module Lookbook
module Preview
include Taggable
+ def id
+ lookbook_path.tr("_", "-")
+ end
+
# Examples::FooBarComponent::Preview -> "Foo Bar"
def lookbook_label
super.presence || lookbook_path.split("/").last.titleize
end
@@ -17,13 +21,25 @@
def lookbook_examples
return @lookbook_examples if @lookbook_examples.present?
public_methods = public_instance_methods(false)
public_method_objects = code_object.meths.filter { |m| public_methods.include?(m.name) }
- examples = public_method_objects.map { |m| PreviewExample.new(m.name.to_s, self) }
- examples.reject!(&:hidden?)
- @lookbook_examples ||= Lookbook.config.sort_examples ? examples.sort_by(&:label) : examples
+ visible = public_method_objects.map { |m| PreviewExample.new(m.name.to_s, self) }.reject(&:hidden?)
+ sorted = Lookbook.config.sort_examples ? visible.sort_by(&:label) : visible
+ @lookbook_examples = []
+ if code_object.groups.any?
+ sorted.group_by { |m| m.group }.each do |name, examples|
+ if name.nil?
+ @lookbook_examples += examples
+ else
+ @lookbook_examples << PreviewGroup.new(name.underscore, self, examples)
+ end
+ end
+ else
+ @lookbook_examples = sorted
+ end
+ @lookbook_examples
end
# Examples::FooBarComponentPreview -> "Examples::FooBar"
# Examples::FooBarComponent::Preview -> "Examples::FooBar"
def lookbook_name
@@ -55,10 +71,14 @@
def lookbook_id
lookbook_path.tr("_", "-")
end
+ def lookbook_layout
+ defined?(@layout) ? @layout : nil
+ end
+
class << self
def all
ViewComponent::Preview.all
end
@@ -67,9 +87,11 @@
end
def exists?(path)
!!find(path)
end
+
+
end
private
def taggable_object_path