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

Allow string as class name #1

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ The output:
</div>
```

You can pass the class name as a string for cases where the class isn't evaluated yet, such as with inner classes. For example:
```ruby
class BlogComponent < Phlex::HTML
include Phlex::Slotable

# This will not work
slot :header, HeaderComponent # uninitialized constant BlogComponent::HeaderComponent
# You should do this
slot :header, "HeaderComponent"

private

class HeaderComponent < Phlex::HTML
# ...
end
end
```

## Roadmap

[] Accepts Strings as view class name
Expand Down
4 changes: 4 additions & 0 deletions lib/phlex/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def slot(slot_name, callable = nil, many: false)
value = case callable
when nil
block
when String
self.class.const_get(callable).new(*args, **kwargs, &block)
else
callable.new(*args, **kwargs, &block)
end
Expand All @@ -40,6 +42,8 @@ def slot(slot_name, callable = nil, many: false)
value = case callable
when nil
block
when String
self.class.const_get(callable).new(*args, **kwargs, &block)
else
callable.new(*args, **kwargs, &block)
end
Expand Down
61 changes: 61 additions & 0 deletions test/phlex/test_multi_slot_with_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "test_helper"

class Phlex::TestMultiSlotWithString < Minitest::Test
class Blog < Phlex::HTML
include Phlex::Slotable

slot :post, "PostComponent", many: true

def template
if post_slots?
main do
post_slots.each do |slot|
render slot
end
end
end

footer { post_slots.size }
end

private

class PostComponent < Phlex::HTML
def initialize(featured: false)
@featured = featured
end

def template(&content)
p(class: @featured ? "featured" : nil, &content)
end
end
end

def test_slots
output = Blog.new.call do |c|
c.with_post(featured: true) { "Post A" }
c.with_post { "Post B" }
c.with_post { "Post C" }
end

assert_equal output, <<~HTML.join_lines
<main>
<p class="featured">Post A</p>
<p>Post B</p>
<p>Post C</p>
</main>

<footer>
3
</footer>
HTML
end

def test_empty_slots
output = Blog.new.call

assert_equal output, "<footer>0</footer>"
end
end
54 changes: 54 additions & 0 deletions test/phlex/test_single_slot_with_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require "test_helper"

class Phlex::TestSingleSlotWithString < Minitest::Test
class Blog < Phlex::HTML
include Phlex::Slotable

slot :header, "HeaderComponent"

def template
if header_slot?
div id: "header" do
render header_slot if header_slot?
end
end

main { "My posts" }
end

private

class HeaderComponent < Phlex::HTML
def initialize(size:)
@size = size
end

def template(&content)
h1(class: "text-#{@size}", &content)
end
end
end

def test_slot
output = Blog.new.call do |c|
c.with_header(size: "lg") { "Hello World!" }
end

assert_equal output, <<~HTML.join_lines
<div id="header">
<h1 class="text-lg">Hello World!</h1>
</div>
<main>
My posts
</main>
HTML
end

def test_empty_slot
output = Blog.new.call

assert_equal output, "<main>My posts</main>"
end
end