Skip to content

Commit

Permalink
Refactor component_with_doc, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Spone committed Aug 13, 2018
1 parent 289524b commit 0ae68fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/komponent/komponent_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ def komponent_components
def component_with_doc(component_name, locals = {}, options = {}, &block)
captured_output = component(component_name, locals, options, &block)

if locals.present?
pretty_locals = JSON.pretty_generate(locals).gsub(/^(\s+)"(\w+)":/, "\\1\\2:")
end

captured_doc = capture do
content_tag :pre, class: "komponent-code" do
content_tag :code do
"= component \"#{component_name}\"" + (pretty_locals ? ", #{pretty_locals}" : "")
"= component \"#{component_name}\"" + (locals.present? ? ", #{pretty_locals(locals)}" : "")
end
end
end

captured_output + captured_doc
end
alias :cdoc :component_with_doc

private

def pretty_locals(locals)
return nil if locals.blank?
JSON.pretty_generate(locals).gsub(/^(\s+)"(\w+)":/, "\\1\\2:")
end
end
10 changes: 10 additions & 0 deletions test/komponent/komponent_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ def test_helper_renders_yield_args
%(<div class="foo-bar">Foo Bar</div>),
component('foo_bar') { |x| x }.chomp
end

def test_helper_renders_with_doc
assert_equal \
%(<div class="all">🌎 😎</div>
<pre class="komponent-code"><code>= component &quot;all&quot;, {
world: &quot;🌎&quot;,
sunglasses: &quot;😎&quot;
}</code></pre>),
component_with_doc('all', world: "🌎", sunglasses: "😎").chomp
end
end

0 comments on commit 0ae68fb

Please sign in to comment.