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

Commit 2bae76f

Browse files
catmandozetachang
authored andcommitted
ready to release
# Conflicts: # lib/reactive-ruby/rails/controller_helper.rb
1 parent 66f077c commit 2bae76f

File tree

7 files changed

+63
-51
lines changed

7 files changed

+63
-51
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
@@ -176,15 +172,19 @@ def static_call_backs
176172
end
177173

178174
def export_component(opts = {})
179-
export_name = (opts[:as] || name).split("::")
175+
export_name = (opts[:as] || name).split('::')
180176
first_name = export_name.first
181-
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
177+
Native(`window`)[first_name] = add_item_to_tree(
178+
Native(`window`)[first_name],
179+
[React::API.create_native_react_class(self)] + export_name[1..-1].reverse
180+
).to_n
182181
end
183182

184183
def imports(component_name)
185-
React::API.import_native_component(self,
186-
React::API.eval_native_react_component(component_name))
187-
render {} # define a dummy render method - will never be called...
184+
React::API.import_native_component(
185+
self, React::API.eval_native_react_component(component_name)
186+
)
187+
define_method(:render) {} # define a dummy render method - will never be called...
188188
rescue Exception => e # rubocop:disable Lint/RescueException : we need to catch everything!
189189
raise "#{self} cannot import '#{component_name}': #{e.message}."
190190
# rubocop:enable Lint/RescueException
@@ -194,9 +194,11 @@ def imports(component_name)
194194

195195
def add_item_to_tree(current_tree, new_item)
196196
if Native(current_tree).class != Native::Object || new_item.length == 1
197-
new_item.inject { |memo, sub_name| { sub_name => memo } }
197+
new_item.inject { |a, e| { e => a } }
198198
else
199-
Native(current_tree)[new_item.last] = add_item_to_tree(Native(current_tree)[new_item.last], new_item[0..-2])
199+
Native(current_tree)[new_item.last] = add_item_to_tree(
200+
Native(current_tree)[new_item.last], new_item[0..-2]
201+
)
200202
current_tree
201203
end
202204
end

lib/react/component/tags.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def present_as_node(component, *params, &children)
4141
React::RenderingContext.render(tag, *params, &children)
4242
end
4343
end
44+
alias_method tag.upcase, tag
45+
const_set tag.upcase, tag
4446
# handle deprecated _as_node style
4547
define_method("#{tag}_as_node") do |*params, &children|
4648
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
@@ -105,11 +105,6 @@ def self.unmount_component_at_node(node)
105105
end
106106

107107
Element.instance_eval do
108-
class Element
109-
class DummyContext < React::Component::Base
110-
end
111-
end
112-
113108
def self.find(selector)
114109
selector = begin
115110
selector.dom_node
@@ -123,11 +118,11 @@ def self.[](selector)
123118
find(selector)
124119
end
125120

126-
define_method :render do |&block|
127-
React.render(
128-
React::RenderingContext.render(nil) do
129-
::Element::DummyContext.new.instance_eval(&block)
130-
end, self
131-
)
121+
define_method :render do |container = nil, params = {}, &block|
122+
klass = Class.new(React::Component::Base)
123+
klass.class_eval do
124+
render(container, params, &block)
125+
end
126+
React.render(React.create_element(klass), self)
132127
end
133128
end if Object.const_defined?('Element')
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
require 'action_controller'
22

3-
module ReactiveRuby
4-
module Rails
5-
class ActionController::Base
6-
def render_component(*args)
7-
@component_name = ((args[0].is_a? Hash) || args.empty?) ? params[:action].camelize : args.shift
8-
@render_params = args.shift || {}
9-
options = args[0] || {}
10-
layout = options.key?(:layout) ? options[:layout].to_s : :default
11-
render inline: "<%= react_component @component_name, @render_params, { prerender: !params[:no_prerender] } %>", layout: layout
12-
end
3+
module ActionController
4+
# adds render_component helper to ActionControllers
5+
class Base
6+
def render_component(*args)
7+
@component_name = (args[0].is_a? Hash) || args.empty? ? params[:action].camelize : args.shift
8+
@render_params = args.shift || {}
9+
options = args[0] || {}
10+
render inline: '<%= react_component @component_name, @render_params, '\
11+
'{ prerender: !params[:no_prerender] } %>',
12+
layout: options.key?(:layout) ? options[:layout].to_s : :default
1313
end
1414
end
1515
end

spec/react/dsl_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<span>hello fred</span>')
4444
end
45-
4645
end
4746

4847
it "will turn the last string in a block into a element" do
@@ -57,6 +56,18 @@ def render
5756
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
5857
end
5958

59+
it "can use the upcase version of builtin tags" do
60+
stub_const 'Foo', Class.new
61+
Foo.class_eval do
62+
include React::Component
63+
def render
64+
DIV { "hello" }
65+
end
66+
end
67+
68+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>hello</div>')
69+
end
70+
6071
it "has a .span short hand String method" do
6172
stub_const 'Foo', Class.new
6273
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)