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

Add support for rendering with block instead of partial #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./no_template.scss";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.no-template {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NoTemplateComponent
extend ComponentHelper

render do |block|
content_tag :div, class: "no-template #{@additional_class}" do
block.call
end
end
end
6 changes: 6 additions & 0 deletions lib/komponent/component_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ def property(name, options = {})
def self.extended(component)
component.properties = {}
end

def render(&block)
@render_block = block
end

attr_reader :render_block
end
6 changes: 5 additions & 1 deletion lib/komponent/component_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def _render(component, locals = {}, options = {}, &block)
define_singleton_method(:block_given_to_component) { block }
end

@context.render("components/#{component}/#{parts.join('_')}", &block)
if component_module.respond_to?(:render_block) && component_module.render_block
@context.instance_exec(block, &component_module.render_block)
else
@context.render("components/#{component}/#{parts.join('_')}", &block)
end
end

def resolved_component_path(component)
Expand Down
2 changes: 1 addition & 1 deletion test/komponent/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_all_returns_components
all = Komponent::Component.all

assert all.is_a?(Hash)
assert_equal all.count, 13
assert_equal all.count, 14
assert all["foo"].is_a?(Komponent::Component)
end

Expand Down
7 changes: 7 additions & 0 deletions test/komponent/komponent_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def test_helper_lists_components
'genius_button',
'hello',
'namespaced/button',
'no_template',
'partial/universe',
'partial/universe_button',
'ping',
Expand All @@ -108,4 +109,10 @@ def test_helper_renders_with_doc
}</code></pre>),
component_with_doc('all', world: "🌎", sunglasses: "😎").chomp
end

def test_helper_renders_without_template
assert_equal \
%(<div class="no-template classy">🌎</div>),
component('no_template', additional_class: "classy") { "🌎" }.chomp
end
end