Skip to content

Commit

Permalink
Support Phlex DSL on scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored and allmarkedup committed Feb 3, 2024
1 parent f56cb0f commit da802d9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
6 changes: 1 addition & 5 deletions app/views/lookbook/previews/preview.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<% if @render_args[:component] %>
<% if defined?(Phlex::SGML) && @render_args[:component].is_a?(Phlex::SGML) %>
<%= raw(@render_args[:component].call(view_context: self, &@render_args[:block])) %>
<% else %>
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
<% end %>
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
<% else %>
<%= render(@render_args[:template], **@render_args[:locals], &@render_args[:block]) %>
<% end %>
9 changes: 9 additions & 0 deletions lib/lookbook/phlex_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Lookbook
class PhlexPreview < Preview
def render(component, &block)
super do
component.instance_exec component, &block
end
end
end
end
11 changes: 11 additions & 0 deletions spec/dummy/app/views/phlex/card_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Views::Phlex
class CardExample < Phlex::HTML
def template(&content)
div class: "card", &content
end

def footer(&content)
div class: "card-footer", &content
end
end
end
18 changes: 18 additions & 0 deletions spec/dummy/test/components/previews/phlex_dsl_example_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PhlexDslExamplePreview < Lookbook::PhlexPreview
def default
render Views::Phlex::CardExample.new do |card|
h1 { "My card" }

div class: "card-body" do
render Views::Phlex::ListExample.new do |list|
list.item { "Hello" }
list.item { "World" }
end
end

card.footer do
span { "Hey ya!" }
end
end
end
end
11 changes: 11 additions & 0 deletions spec/requests/previews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
expect(html.has_css?("li", text: "World")).to be true
end
end

context "Phlex DSL" do
it "supports Phlex DSL" do
get lookbook_preview_path("phlex_dsl_example/default")

expect(html.has_css?(".card > h1", text: "My card")).to be true
expect(html.has_css?(".card > .card-body > ul > li", text: "Hello")).to be true
expect(html.has_css?(".card > .card-body > ul > li", text: "World")).to be true
expect(html.has_css?(".card > .card-footer > span", text: "Hey ya!")).to be true
end
end
end

context "View partials" do
Expand Down

0 comments on commit da802d9

Please sign in to comment.