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

Commit 1100e83

Browse files
committed
ready to release
1 parent 3ffaa5a commit 1100e83

File tree

7 files changed

+54
-42
lines changed

7 files changed

+54
-42
lines changed

lib/react/component/class_methods.rb

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module React
22
module Component
3+
# class level methods (macros) for components
34
module ClassMethods
45
def backtrace(*args)
56
@dont_catch_exceptions = (args[0] == :none)
@@ -9,39 +10,34 @@ def backtrace(*args)
910
def process_exception(e, component, reraise = nil)
1011
message = ["Exception raised while rendering #{component}"]
1112
if e.backtrace && e.backtrace.length > 1 && !@backtrace_off
12-
message << " #{e.backtrace[0]}"
13-
message += e.backtrace[1..-1].collect { |line| line }
13+
append_backtrace(message, e.backtrace)
1414
else
1515
message[0] += ": #{e.message}"
1616
end
17-
message = message.join("\n")
18-
`console.error(message)`
17+
`console.error(#{message.join("\n")})`
1918
raise e if reraise || @dont_catch_exceptions
2019
end
2120

21+
def append_backtrace(message_array, backtrace)
22+
message_array << " #{backtrace[0]}"
23+
backtrace[1..-1].each { |line| message_array << line }
24+
end
25+
2226
def deprecation_warning(message)
2327
@deprecation_messages ||= []
24-
message = "Warning: Deprecated feature used in #{self.name}. #{message}"
28+
message = "Warning: Deprecated feature used in #{name}. #{message}"
2529
unless @deprecation_messages.include? message
2630
@deprecation_messages << message
2731
IsomorphicHelpers.log message, :warning
2832
end
2933
end
3034

31-
def render(container=nil, params={}, &block)
32-
if container
33-
if block
34-
define_method :render do
35-
send(container, params) { instance_eval &block }
36-
end
35+
def render(container = nil, params = {}, &block)
36+
define_method :render do
37+
if container
38+
React::RenderingContext.render(container, params) { instance_eval(&block) if block }
3739
else
38-
define_method :render do
39-
send(container, params)
40-
end
41-
end
42-
else
43-
define_method :render do
44-
instance_eval &block
40+
instance_eval(&block)
4541
end
4642
end
4743
end
@@ -191,15 +187,19 @@ def static_call_backs
191187
end
192188

193189
def export_component(opts = {})
194-
export_name = (opts[:as] || name).split("::")
190+
export_name = (opts[:as] || name).split('::')
195191
first_name = export_name.first
196-
Native(`window`)[first_name] = add_item_to_tree(Native(`window`)[first_name], [React::API.create_native_react_class(self)] + export_name[1..-1].reverse).to_n
192+
Native(`window`)[first_name] = add_item_to_tree(
193+
Native(`window`)[first_name],
194+
[React::API.create_native_react_class(self)] + export_name[1..-1].reverse
195+
).to_n
197196
end
198197

199198
def imports(component_name)
200-
React::API.import_native_component(self,
201-
React::API.eval_native_react_component(component_name))
202-
render {} # define a dummy render method - will never be called...
199+
React::API.import_native_component(
200+
self, React::API.eval_native_react_component(component_name)
201+
)
202+
define_method(:render) {} # define a dummy render method - will never be called...
203203
rescue Exception => e # rubocop:disable Lint/RescueException : we need to catch everything!
204204
raise "#{self} cannot import '#{component_name}': #{e.message}."
205205
# rubocop:enable Lint/RescueException
@@ -209,9 +209,11 @@ def imports(component_name)
209209

210210
def add_item_to_tree(current_tree, new_item)
211211
if Native(current_tree).class != Native::Object || new_item.length == 1
212-
new_item.inject { |memo, sub_name| { sub_name => memo } }
212+
new_item.inject { |a, e| { e => a } }
213213
else
214-
Native(current_tree)[new_item.last] = add_item_to_tree(Native(current_tree)[new_item.last], new_item[0..-2])
214+
Native(current_tree)[new_item.last] = add_item_to_tree(
215+
Native(current_tree)[new_item.last], new_item[0..-2]
216+
)
215217
current_tree
216218
end
217219
end

lib/react/component/tags.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def present_as_node(component, *params, &children)
3030
define_method(tag) do |*params, &children|
3131
React::RenderingContext.render(tag, *params, &children)
3232
end
33+
alias_method tag.upcase, tag
34+
const_set tag.upcase, tag
3335
# handle deprecated _as_node style
3436
define_method("#{tag}_as_node") do |*params, &children|
3537
React::RenderingContext.build_only(tag, *params, &children)

lib/react/top_level.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ def self.unmount_component_at_node(node)
7171
end
7272

7373
Element.instance_eval do
74-
class Element
75-
class DummyContext < React::Component::Base
76-
end
77-
end
78-
7974
def self.find(selector)
8075
selector = begin
8176
selector.dom_node
@@ -89,11 +84,11 @@ def self.[](selector)
8984
find(selector)
9085
end
9186

92-
define_method :render do |&block|
93-
React.render(
94-
React::RenderingContext.render(nil) do
95-
::Element::DummyContext.new.instance_eval(&block)
96-
end, self
97-
)
87+
define_method :render do |container = nil, params = {}, &block|
88+
klass = Class.new(React::Component::Base)
89+
klass.class_eval do
90+
render(container, params, &block)
91+
end
92+
React.render(React.create_element(klass), self)
9893
end
9994
end if Object.const_defined?('Element')

lib/reactive-ruby/rails/controller_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActionController
44
# adds render_component helper to ActionControllers
55
class Base
66
def render_component(*args)
7-
@component_name = ((args[0].is_a? Hash) || args.empty?) ? params[:action].camelize : args.shift
7+
@component_name = (args[0].is_a? Hash) || args.empty? ? params[:action].camelize : args.shift
88
@render_params = args.shift || {}
99
options = args[0] || {}
1010
render inline: '<%= react_component @component_name, @render_params, '\

spec/react/dsl_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Bar < React::Component::Base
4848

4949
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>hello fred</span>')
5050
end
51-
5251
end
5352

5453
it "will turn the last string in a block into a element" do
@@ -63,6 +62,18 @@ def render
6362
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
6463
end
6564

65+
it "can use the upcase version of builtin tags" do
66+
stub_const 'Foo', Class.new
67+
Foo.class_eval do
68+
include React::Component
69+
def render
70+
DIV { "hello" }
71+
end
72+
end
73+
74+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
75+
end
76+
6677
it "has a .span short hand String method" do
6778
stub_const 'Foo', Class.new
6879
Foo.class_eval do

spec/react/native_library_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module NativeLibraryTestModule
77
class Component < React::Component::Base
88
param :time_stamp
99
backtrace :none
10-
render { puts "about to render Component"; NativeComponent(name: "There - #{params.time_stamp}") }
10+
render { NativeComponent(name: "There - #{params.time_stamp}") }
1111
end
1212

1313
class NestedComponent < React::Component::Base
1414
param :time_stamp
1515
backtrace :none
16-
render { puts "about to render NestedComponent"; NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") }
16+
render { NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") }
1717
end
1818
end
1919

spec/react/opal_jquery_extensions_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
React::API.clear_component_class_cache
77
end
88

9-
it 'responds to render' do
10-
expect(Element['body']).to respond_to :render
9+
it 'renders a top level component using render' do
10+
test_div = Element.new(:div)
11+
test_div.render(:span, id: :render_test_span) { 'hello' }
12+
expect(Element[test_div].find('#render_test_span').html).to eq('hello')
1113
end
1214

1315
it 'will find the DOM node given a react element' do

0 commit comments

Comments
 (0)