Skip to content

Commit c3a66a1

Browse files
author
William Meleyal
committed
Merge branch 'master' of github.com:reactjs/react-rails into fix-events
2 parents 97394ce + b4051e1 commit c3a66a1

13 files changed

+115
-21
lines changed

Appraisals

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ appraise "rails-4.0-with-therubyracer" do
1717
gem 'rails', '~> 4.0'
1818
gem 'therubyracer', '0.12.0', :platform => :mri
1919
end
20+
21+
appraise "rails-4.1" do
22+
gem 'rails', '~> 4.1'
23+
end

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ As with all gem dependencies, we strongly recommend adding `react-rails` to your
2323
# If you missed a warning at the top of this README - this is still in development
2424
# which means this version is not pushed to rubygems.org
2525

26-
gem 'react-rails', '~> 1.0.0'
26+
gem 'react-rails', '~> 1.0.0.pre', github: 'reactjs/react-rails'
2727
```
2828

2929
## Usage
@@ -69,7 +69,7 @@ Component = React.createClass
6969

7070
```erb
7171
<!-- react_ujs will execute `React.renderComponent(HelloMessage({name:"Bob"}), element)` -->
72-
<div data-react-class="HelloMessage" data-react-props="<%= {:name => 'Bob'}.to_json %>" />
72+
<div data-react-class="HelloMessage" data-react-props="<%= {name: 'Bob'}.to_json %>" />
7373
```
7474

7575
`react_ujs` will also scan DOM elements and call `React.unmountComponentAtNode` on page unload. If you want to disable this behavior, remove `data-react-class` attribute in `componentDidMount`.
@@ -89,17 +89,17 @@ To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if
8989
There is a view helper method `react_component`. It is designed to work with `react_ujs` and takes a React class name, properties, and HTML options as arguments:
9090

9191
```ruby
92-
react_component('HelloMessage', :name => 'John')
92+
react_component('HelloMessage', name: 'John')
9393
# <div data-react-class="HelloMessage" data-react-props="{&quot;name&quot;:&quot;John&quot;}"></div>
9494
```
9595

9696
By default, a `<div>` element is used. Other tag and HTML attributes can be specified:
9797

9898
```ruby
99-
react_component('HelloMessage', {:name => 'John'}, :span)
99+
react_component('HelloMessage', {name: 'John'}, :span)
100100
# <span data-...></span>
101101

102-
react_component('HelloMessage', {:name => 'John'}, {:id => 'hello', :class => 'foo', :tag => :span})
102+
react_component('HelloMessage', {name: 'John'}, {id: 'hello', class: 'foo', tag: :span})
103103
# <span class="foo" id="hello" data-...></span>
104104
```
105105

@@ -139,10 +139,10 @@ window.Component = Component
139139

140140
#### View Helper
141141

142-
To take advantage of server rendering, use the same view helper `react_component`, and pass in `:prerender => true` in the `options` hash.
142+
To take advantage of server rendering, use the same view helper `react_component`, and pass in `prerender: true` in the `options` hash.
143143

144144
```erb
145-
react_component('HelloMessage', {:name => 'John'}, {:prerender => true})
145+
react_component('HelloMessage', {name: 'John'}, {prerender: true})
146146
```
147147
This will return the fully rendered component markup, and as long as you have included the `react_ujs` script in your page, then the component will also be instantiated and mounted on the client.
148148

gemfiles/rails_3.1.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
source "http://rubygems.org"
44

55
gem "rails", "~> 3.1"
6-
gem "sprockets", ">= 2.2.1"
6+
gem "sprockets", ">= 2.2.2"
77

88
gemspec :path=>"../"

gemfiles/rails_4.1.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file was generated by Appraisal
2+
3+
source "http://rubygems.org"
4+
5+
gem "rails", "~> 4.1"
6+
7+
gemspec :path=>"../"

lib/react/rails/railtie.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class Railtie < ::Rails::Railtie
1919
app.config.watchable_files.concat Dir["#{app.root}/app/assets/javascripts/**/*.jsx*"]
2020
end
2121

22+
# Include the react-rails view helper lazily
23+
initializer "react_rails.setup_view_helpers" do
24+
ActiveSupport.on_load(:action_view) do
25+
include ::React::Rails::ViewHelper
26+
end
27+
end
28+
2229
# run after all initializers to allow sprockets to pick up react.js and
2330
# jsxtransformer.js from end-user to override ours if needed
2431
initializer "react_rails.setup_vendor", :after => "sprockets.environment", group: :all do |app|
@@ -56,13 +63,15 @@ class Railtie < ::Rails::Railtie
5663
config.after_initialize do |app|
5764
# Server Rendering
5865
# Concat component_filenames together for server rendering
59-
app.config.react.components_js = app.config.react.component_filenames.map do |filename|
60-
app.assets[filename].to_s
61-
end.join(";")
66+
app.config.react.components_js = lambda {
67+
app.config.react.component_filenames.map do |filename|
68+
app.assets[filename].to_s
69+
end.join(";")
70+
}
6271

6372
do_setup = lambda do
6473
cfg = app.config.react
65-
React::Renderer.setup!( cfg.react_js.call, cfg.components_js,
74+
React::Renderer.setup!( cfg.react_js, cfg.components_js,
6675
{:size => cfg.size, :timeout => cfg.timeout})
6776
end
6877

lib/react/rails/view_helper.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,3 @@ def react_component(name, args = {}, options = {}, &block)
2323
end
2424
end
2525
end
26-
27-
ActionView::Base.class_eval do
28-
include ::React::Rails::ViewHelper
29-
end

lib/react/renderer.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def self.setup!(react_js, components_js, args={})
1010
@@react_js = react_js
1111
@@components_js = components_js
1212
@@pool.shutdown{} if @@pool
13+
reset_combined_js!
1314
@@pool = ConnectionPool.new(:size => args[:size]||10, :timeout => args[:timeout]||20) { self.new }
1415
end
1516

@@ -19,8 +20,8 @@ def self.render(component, args={})
1920
end
2021
end
2122

22-
def self.combined_js
23-
@@combined_js ||= <<-CODE
23+
def self.setup_combined_js
24+
<<-CODE
2425
var global = global || this;
2526
2627
var console = global.console || {};
@@ -30,12 +31,20 @@ def self.combined_js
3031
}
3132
});
3233
33-
#{@@react_js};
34+
#{@@react_js.call};
3435
React = global.React;
35-
#{@@components_js};
36+
#{@@components_js.call};
3637
CODE
3738
end
3839

40+
def self.reset_combined_js!
41+
@@combined_js = setup_combined_js
42+
end
43+
44+
def self.combined_js
45+
@@combined_js
46+
end
47+
3948
def context
4049
@context ||= ExecJS.compile(self.class.combined_js)
4150
end

react-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
2323

2424
s.add_dependency 'execjs'
2525
s.add_dependency 'rails', '>= 3.1'
26-
s.add_dependency 'react-source', '0.10.0'
26+
s.add_dependency 'react-source', '0.11.1'
2727
s.add_dependency 'connection_pool'
2828

2929
s.files = Dir[
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** @jsx React.DOM */
2+
3+
TodoList = React.createClass({
4+
getInitialState: function() {
5+
return({mounted: "nope"});
6+
},
7+
componentWillMount: function() {
8+
this.setState({mounted: 'yep'});
9+
},
10+
render: function() {
11+
return (
12+
<ul>
13+
<li>Updated</li>
14+
<li id='status'>{this.state.mounted}</li>
15+
{this.props.todos.map(function(todo, i) {
16+
return (<Todo key={i} todo={todo} />)
17+
})}
18+
</ul>
19+
)
20+
}
21+
})
22+

test/react_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ReactTest < ActionDispatch::IntegrationTest
3131

3232
assert_response :success
3333
assert_equal "'test_confirmation_token_react_content';\n", @response.body
34+
35+
React::Renderer.reset_combined_js!
3436
end
3537

3638
end

0 commit comments

Comments
 (0)