app/controllers/lookbook/previews_controller.rb in lookbook-1.0.1 vs app/controllers/lookbook/previews_controller.rb in lookbook-1.0.2
- old
+ new
@@ -11,11 +11,11 @@
before_action :lookup_entities, only: [:preview, :show]
before_action :set_title
before_action :set_params
def preview
- if @example
+ if @target
begin
opts = {layout: @preview.layout}
if params[:lookbook_embed] == "true"
opts[:append_html] = "<script src=\"/lookbook-assets/js/embed.js?v=#{Lookbook.version}\"></script>".html_safe
end
@@ -30,11 +30,11 @@
show_404 layout: "lookbook/standalone"
end
end
def show
- if @example
+ if @target
begin
@main_panels = main_panels
@drawer_panels = drawer_panels
rescue => exception
render_in_layout "lookbook/error", layout: "lookbook/inspector", error: prettify_error(exception)
@@ -50,13 +50,13 @@
end
private
def lookup_entities
- @example = Lookbook.previews.find_example(params[:path])
- if @example.present?
- @preview = @example.preview
+ @target = Lookbook.previews.find_example(params[:path])
+ if @target.present?
+ @preview = @target.preview
if params[:path] == @preview&.lookup_path
redirect_to lookbook_inspect_path "#{params[:path]}/#{@preview.default_example.name}"
end
else
@preview = Lookbook.previews.find(params[:path])
@@ -83,39 +83,39 @@
end
render_in_layout "lookbook/404", layout: layout, **locals
end
def target_examples
- @example.type == :group ? @example.examples : [@example]
+ @target.type == :group ? @target.examples : [@target]
end
def set_title
- @title = @example.present? ? [@example&.label, @preview&.label].compact.join(" :: ") : "Not found"
+ @title = @target.present? ? [@target&.label, @preview&.label].compact.join(" :: ") : "Not found"
end
def set_params
- if @example
+ if @target
# cast known params to type
- @example.params.each do |param|
+ @target.params.each do |param|
if preview_controller.params.key?(param[:name])
preview_controller.params[param[:name]] = Lookbook::Params.cast(preview_controller.params[param[:name]], param[:type])
end
end
# set display and data params
preview_controller.params.merge!({
lookbook: {
- display: @example.display_params,
+ display: @target.display_params,
data: Lookbook.data
}
})
end
end
def preview_params
preview_controller.params.permit!
preview_controller.params.to_h.select do |key, value|
- !!@example.params.find { |param| param[:name] == key }
+ !!@target.params.find { |param| param[:name] == key }
end
end
def inspector_data
return @inspector_data if @inspector_data.present?
@@ -123,16 +123,11 @@
context_data = {
preview_params: preview_params,
path: params[:path]
}
- example = @example
preview = @preview
- preview.define_singleton_method(:params, proc {
- example.params
- })
-
examples = target_examples.map do |example|
render_args = @preview.render_args(example.name, params: preview_controller.params)
has_template = render_args[:template] != "view_components/preview"
output = preview_controller.process(:render_example_to_string, @preview, example.name)
source = has_template ? example.template_source(render_args[:template]) : example.method_source
@@ -142,15 +137,22 @@
example.define_singleton_method(:source, proc { source })
example.define_singleton_method(:source_lang, proc { source_lang })
example
end
+ target = @target.type == :group ? @target : examples.find { |e| e.lookup_path == @target.lookup_path }
+
+ preview.define_singleton_method(:params, proc {
+ target.params
+ })
+
@inspector_data ||= Lookbook::Store.new({
context: context_data,
preview: preview,
examples: examples,
- example: example,
+ example: examples.first,
+ target: target,
data: Lookbook.data,
app: Lookbook
})
end
@@ -196,10 +198,10 @@
def prettify_error(exception)
error_params = if exception.is_a?(ViewComponent::PreviewTemplateError)
{
file_path: @preview&.full_path,
line_number: 0,
- source_code: @example&.source
+ source_code: @target&.source
}
elsif exception.is_a?(ActionView::Template::Error) & exception.message.include?("implements a reserved method")
message_parts = exception.message.split("\n").first.split
component_class = message_parts.first.constantize
naughty_method = message_parts.last.delete("#").delete("`").delete(".")