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

Commit e3df4bc

Browse files
catmandozetachang
authored andcommitted
look up non sibling ancestor components
1 parent 2bae76f commit e3df4bc

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/react/component/tags.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def method_missing(name, *params, &children)
6161
component = find_component(name)
6262
return React::RenderingContext.render(component, *params, &children) if component
6363
end
64-
puts "about to super for #{name}"
6564
Object.method_missing(name, *params, &children)
6665
end
6766

@@ -95,13 +94,13 @@ def find_name_and_parent(component)
9594
private
9695

9796
def find_component(name)
98-
component = self.class.const_get(name) if self.class.const_defined? name
99-
if component
100-
unless component.method_defined? :render
101-
raise "#{name} does not appear to be a react component."
102-
end
103-
component
104-
end
97+
scopes = self.class.name.split('::').inject([Module]) do |nesting, next_const|
98+
nesting + [nesting.last.const_get(next_const)]
99+
end.reverse
100+
scope = scopes.detect { |s| s.const_defined?(name) } || return
101+
component = scope.const_get(name)
102+
return component if component.method_defined?(:render)
103+
raise "#{name} does not appear to be a react component."
105104
end
106105
end
107106
end

spec/react/dsl_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ def render
116116
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div><p>hello</p></div>')
117117
end
118118

119+
it 'can do a method call on a class name that is not a direct sibling' do
120+
stub_const 'Mod', Module.new
121+
stub_const 'Mod::NestedMod', Module.new
122+
stub_const 'Mod::Comp', Class.new(React::Component::Base)
123+
Mod::Comp.class_eval do
124+
render { 'Mod::Comp' }
125+
end
126+
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
127+
Mod::NestedMod::NestedComp.class_eval do
128+
render do
129+
Comp()
130+
end
131+
end
132+
expect(React.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)))
133+
.to eq('<span>Mod::Comp</span>')
134+
end
135+
136+
it 'raises a meaningful error if a Constant Name is not actually a component' do
137+
stub_const 'Mod', Module.new
138+
stub_const 'Mod::NestedMod', Module.new
139+
stub_const 'Mod::Comp', Class.new
140+
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
141+
Mod::NestedMod::NestedComp.class_eval do
142+
backtrace :none
143+
render do
144+
Comp()
145+
end
146+
end
147+
expect { React.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)) }
148+
.to raise_error('Comp does not appear to be a react component.')
149+
end
150+
119151
it "will treat the component class name as a first class component name" do
120152
stub_const 'Mod::Bar', Class.new
121153
Mod::Bar.class_eval do

0 commit comments

Comments
 (0)