Skip to content

Commit

Permalink
Improve entity resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed May 18, 2024
1 parent 1370ee4 commit a2dc4e1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/lookbook/helpers/path_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@ def preview_target_path(preview_identifier, target_identifier, **kwargs)

lookbook.preview_target_path(preview, target, **kwargs)
end

def preview_embed_path(preview_identifier, target_identifier, **kwargs)
preview = Lookbook::Previews.resolve_preview(preview_identifier)
raise ArgumentError, "Could not resolve preview identifier" unless preview

target = preview.resolve_target(target_identifier)
raise ArgumentError, "Could not resolve inspector target identifier" unless target

lookbook.preview_embed_path(preview, target, **kwargs)
end
end
end
2 changes: 2 additions & 0 deletions lib/lookbook/previews/preview_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def inspector_targets
end

def resolve_target(identifier)
return identifier if identifier.is_a?(InspectorTargetEntity)

if identifier.is_a?(String) || identifier.is_a?(Symbol)
inspector_targets.find { [_1.id, _1.uuid].include?(identifier.to_s) }
else
Expand Down
2 changes: 2 additions & 0 deletions lib/lookbook/previews/previews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def load
end

def resolve_preview(identifier)
return identifier if identifier.is_a?(PreviewEntity)

if identifier.is_a?(Class)
store.find { _1.preview_class.to_s == identifier.to_s }
elsif identifier.is_a?(String) || identifier.is_a?(Symbol)
Expand Down
29 changes: 29 additions & 0 deletions test/controllers/previews_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
class PreviewsControllerTest < ActionDispatch::IntegrationTest
include Lookbook::PathHelper

describe "#inspect" do
before do
get Lookbook::Previews.first.inspector_targets.first.url_path
end

it "renders successfully" do
assert_response :success
end

it "displays the preview inspector component" do
assert_select "[data-component='preview-inspector']"
end
end

describe "#embed" do
before do
preview = Lookbook::Previews.first
get preview_embed_path(preview, preview.inspector_targets.first)
end

it "renders successfully" do
assert_response :success
end

it "displays the preview embed component" do
assert_select "[data-component='preview-embed']"
end
end

describe "#preview" do
describe "ViewComponent preview" do
it "renders" do
Expand Down

0 comments on commit a2dc4e1

Please sign in to comment.