Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 05f24bd

Browse files
committed
Address #7
1 parent 24cbcce commit 05f24bd

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/react/component.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,27 @@ def component_will_unmount
6969
self.run_callback(:before_unmount)
7070
end
7171

72+
def p(*args, &block)
73+
if block || args.count == 0 || (args.count == 1 && args.first.is_a?(Hash))
74+
_p_tag(*args, &block)
75+
else
76+
Kernel.p(*args)
77+
end
78+
end
79+
7280
def method_missing(name, *args, &block)
73-
unless (React::HTML_TAGS.include?(name) || name == 'present')
81+
unless (React::HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag')
7482
return super
7583
end
7684

7785
if name == "present"
7886
name = args.shift
7987
end
8088

89+
if name == "_p_tag"
90+
name = "p"
91+
end
92+
8193
@buffer = [] unless @buffer
8294
if block
8395
current = @buffer

spec/component_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,47 @@ def render
536536
element = React.create_element(Foo)
537537
expect(React.render_to_static_markup(element)).to eq("<div></div>")
538538
end
539+
540+
it "should redefine `p` to make method missing work" do
541+
stub_const 'Foo', Class.new
542+
Foo.class_eval do
543+
include React::Component
544+
545+
def render
546+
p(class_name: "foo") do
547+
p
548+
div { "lorem ipsum" }
549+
p(id: "10")
550+
end
551+
end
552+
end
553+
554+
element = React.create_element(Foo)
555+
expect(React.render_to_static_markup(element)).to eq("<p class=\"foo\"><p></p><div>lorem ipsum</div><p id=\"10\"></p></p>")
556+
end
557+
558+
it "should only override `p` in render context" do
559+
stub_const 'Foo', Class.new
560+
Foo.class_eval do
561+
include React::Component
562+
563+
before_mount do
564+
p "first"
565+
end
566+
567+
after_mount do
568+
p "second"
569+
end
570+
571+
def render
572+
div
573+
end
574+
end
575+
576+
expect(Kernel).to receive(:p).with("first")
577+
expect(Kernel).to receive(:p).with("second")
578+
renderToDocument(Foo)
579+
end
539580
end
540581

541582
describe "isMounted()" do

0 commit comments

Comments
 (0)