lib/lookbook/preview_example.rb in lookbook-1.0.4 vs lib/lookbook/preview_example.rb in lookbook-1.0.5

- old
+ new

@@ -1,14 +1,14 @@ module Lookbook class PreviewExample < Entity attr_reader :name, :preview delegate :params, :position, :group, :notes, :hidden?, :source, :tags, :tag, to: :@example_inspector - def initialize(name, preview) + def initialize(name, preview, code_object) @name = name @preview = preview - @example_inspector = SourceInspector.new("#{@preview.name}##{name}") + @example_inspector = SourceInspector.new(code_object) super("#{@preview.path}/#{name}") end def id @example_inspector&.id || generate_id(@preview.id, name) @@ -33,11 +33,12 @@ def lang Lookbook::Lang.find(:ruby) end def template_source(template_path) - File.read(full_template_path(template_path)) + source_path = full_template_path(template_path) + source_path ? File.read(source_path) : nil end def template_lang(template_path) Lookbook::Lang.guess(full_template_path(template_path)) || Lookbook::Lang.find(:html) end @@ -54,14 +55,20 @@ @preview.hierarchy_depth + 1 end protected + def strip_ext(path) + path.sub(/\..*$/, "") + end + def full_template_path(template_path) + template_path = strip_ext template_path base_path = Array(Lookbook.config.preview_paths).detect do |p| Dir["#{p}/#{template_path}.html.*"].first end - Pathname.new(Dir["#{base_path}/#{template_path}.html.*"].first) + path = Dir["#{base_path}/#{template_path}.html.*"].first + path ? Pathname.new(path) : nil end class << self def all Preview.all.map { |preview| preview.examples }.flatten