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 committed Jan 31, 2024
1 parent 60d5542 commit 46330f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/views/lookbook/previews/preview.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% 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])) %>
<%= render(@render_args[:component]) { @render_args[:component].instance_eval(&@render_args[:block]) } %>
<% else %>
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
<% end %>
Expand Down
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
17 changes: 17 additions & 0 deletions spec/dummy/test/components/previews/phlex_example_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,21 @@ def builder
list.item { "World" }
end
end

def phlex_dsl
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
9 changes: 9 additions & 0 deletions spec/requests/previews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
expect(html.has_css?("li", text: "Hello")).to be true
expect(html.has_css?("li", text: "World")).to be true
end

it "allows phlex DSL" do
get lookbook_preview_path("phlex_example/phlex_dsl")

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

Expand Down

0 comments on commit 46330f0

Please sign in to comment.