Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Phlex DSL on scenarios #585

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]) %>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this was raw + call, but changing to render component, args, block worked on specs and on my project.
Using render Rails calls component.render_in(self, &block). Ref: https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/rendering_helper.rb#L42

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephannv the discussion in #400 should help explain the rationale for this, ugly as it is!

<% 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
Loading