-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module SomeNamespace | ||
class CrazyComponent < Phlex::HTML | ||
This comment has been minimized.
Sorry, something went wrong. |
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def template | ||
article do | ||
h1 { "Hello, #{@name}" } | ||
yield | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,21 @@ def call(...) | |
new(...).call | ||
end | ||
|
||
def inherited(subclass) | ||
return unless subclass.name | ||
return if subclass.name.start_with?("Sus::") | ||
return if subclass.name.start_with?("Phlex::") | ||
constant_path = subclass.name.split("::") | ||
name = constant_path.pop | ||
namespace = Module.const_get(constant_path.join("::")) | ||
This comment has been minimized.
Sorry, something went wrong.
joeldrapper
Author
Collaborator
|
||
|
||
namespace.define_singleton_method(name) do |*args, **kwargs, &block| | ||
Thread.current[:__phlex_current__].send(:render, | ||
This comment has been minimized.
Sorry, something went wrong.
joeldrapper
Author
Collaborator
|
||
subclass.new(*args, **kwargs), &block | ||
) | ||
end | ||
end | ||
|
||
# Create a new instance of the component. | ||
# @note The block will not be delegated {#initialize}. Instead, it will be sent to {#template} when rendering. | ||
def new(*args, **kwargs, &block) | ||
|
@@ -103,6 +118,8 @@ def call(...) | |
|
||
# @api private | ||
def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, fragment: nil, &block) | ||
previous_rendering_component = Thread.current[:__phlex_current__] | ||
Thread.current[:__phlex_current__] = self | ||
This comment has been minimized.
Sorry, something went wrong.
joeldrapper
Author
Collaborator
|
||
@_buffer = buffer | ||
@_context = context | ||
@_view_context = view_context | ||
|
@@ -133,6 +150,9 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, | |
end | ||
|
||
buffer << context.buffer unless parent | ||
|
||
ensure | ||
Thread.current[:__phlex_current__] = previous_rendering_component | ||
end | ||
|
||
# Access the current render context data | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "crazy_example" | ||
This comment has been minimized.
Sorry, something went wrong.
joeldrapper
Author
Collaborator
|
||
|
||
class Example < Phlex::HTML | ||
def template | ||
SomeNamespace::CrazyComponent("Joel") do | ||
strong { "This is mad" } | ||
end | ||
end | ||
end | ||
|
||
describe Example do | ||
it "renders a crazy example" do | ||
expect(Example.new.call).to be == %(<article><h1>Hello, Joel</h1><strong>This is mad</strong></article>) | ||
end | ||
end |
Defining this component here, defines the
CrazyComponent
method onSomeNamespace
.