Skip to content

Commit 06e4c2d

Browse files
author
William Meleyal
committed
integrate master
2 parents b7079ea + d0d62c5 commit 06e4c2d

35 files changed

+516
-152
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
22
language: ruby
3+
sudo: false
34
rvm:
5+
- 2.2
6+
- 2.1
47
- 2.0.0
58
- 1.9.3
69
- jruby-19mode
7-
before_install:
8-
- gem install bundler --version '>= 1.2.2'
9-
before_script: 'bundle exec rake appraisal:install'
10+
before_script: 'bundle exec appraisal install'
1011
script: 'bundle exec rake appraisal test'

Appraisals

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ end
2121
appraise "rails-4.1" do
2222
gem 'rails', '~> 4.1'
2323
end
24+
25+
appraise "rails-4.2" do
26+
gem 'rails', '~> 4.2'
27+
end

README.md

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
# react-rails [![Build Status](https://travis-ci.org/reactjs/react-rails.png)](https://travis-ci.org/reactjs/react-rails) [![Code Climate](https://codeclimate.com/github/reactjs/react-rails.png)](https://codeclimate.com/github/reactjs/react-rails)
1+
[![Gem](https://img.shields.io/gem/v/react-rails.svg?style=flat-square)](http://rubygems.org/gems/react-rails)
2+
[![Build Status](https://img.shields.io/travis/reactjs/react-rails/master.svg?style=flat-square)](https://travis-ci.org/reactjs/react-rails)
3+
[![Gemnasium](https://img.shields.io/gemnasium/reactjs/react-rails.svg?style=flat-square)](https://gemnasium.com/reactjs/react-rails)
4+
[![Code Climate](https://img.shields.io/codeclimate/github/reactjs/react-rails.svg?style=flat-square)](https://codeclimate.com/github/reactjs/react-rails)
5+
6+
* * *
7+
8+
# react-rails
29

310
**react-rails version disclaimer**
411
*This README is for `1.x` branch which is still in development. Please switch to latest `0.x` branch for stable version.*
@@ -51,32 +58,38 @@ You can `require` it in your manifest:
5158
Alternatively, you can include it directly as a separate script tag:
5259

5360
```erb
54-
# app/views/layouts/application.erb.html
61+
# app/views/layouts/application.html.erb
5562
5663
<%= javascript_include_tag "react" %>
5764
```
5865

5966
### JSX
6067

61-
To transform your JSX into JS, simply create `.js.jsx` files, and ensure that the file has the `/** @jsx React.DOM */` docblock. These files will be transformed on request, or precompiled as part of the `assets:precompile` task.
68+
To transform your JSX into JS, simply create `.js.jsx` files. These files will be transformed on request, or precompiled as part of the `assets:precompile` task.
6269

63-
CoffeeScript files can also be used, by creating `.js.jsx.coffee` files. You must use this form of the docblock at the top of each file: `###* @jsx React.DOM ###`. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
70+
CoffeeScript files can also be used, by creating `.js.jsx.coffee` files. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
6471

6572
```coffee
66-
###* @jsx React.DOM ###
67-
6873
Component = React.createClass
6974
render: ->
7075
`<ExampleComponent videos={this.props.videos} />`
7176
```
7277

78+
You can use the `--harmony` or `--strip-types` options by adding a configuration to `application.rb`:
79+
80+
```ruby
81+
config.react.jsx_transform_options = {
82+
harmony: true,
83+
strip_types: true, # for removing Flow type annotations
84+
}
85+
```
7386

7487
### Unobtrusive JavaScript
7588

76-
`react_ujs` will call `React.renderComponent` for every element with `data-react-class` attribute. React properties can be specified by `data-react-props` attribute in JSON format. For example:
89+
`react_ujs` will call `React.render` for every element with `data-react-class` attribute. React properties can be specified by `data-react-props` attribute in JSON format. For example:
7790

7891
```erb
79-
<!-- react_ujs will execute `React.renderComponent(HelloMessage({name:"Bob"}), element)` -->
92+
<!-- react_ujs will execute `React.render(HelloMessage({name:"Bob"}), element)` -->
8093
<div data-react-class="HelloMessage" data-react-props="<%= {name: 'Bob'}.to_json %>" />
8194
```
8295

@@ -154,11 +167,9 @@ which generates JSON like this:
154167
This is not suitable for ReactJS props, which is expected to be a key-value object. You will need to wrap your index.json.jbuilder node with a root node, like so:
155168

156169
```ruby
157-
json.messages do |json|
158-
json.array!(@messages) do |message|
159-
json.extract! message, :id, :name
160-
json.url message_url(message, format: :json)
161-
end
170+
json.messages(@messages) do |message|
171+
json.extract! message, :id, :name
172+
json.url message_url(message, format: :json)
162173
end
163174
```
164175

@@ -193,8 +204,6 @@ In order for us to render your React components, we need to be able to find them
193204
This will bring in all files located in the `app/assets/javascripts/components` directory. You can organize your code however you like, as long as a request for `/assets/javascripts/components.js` brings in a concatenated file containing all of your React components, and each one has to be available in the global scope (either `window` or `global` can be used). For `.js.jsx` files this is not a problem, but if you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:
194205

195206
```coffee
196-
###* @jsx React.DOM ###
197-
198207
Component = React.createClass
199208
render: ->
200209
`<ExampleComponent videos={this.props.videos} />`
@@ -211,6 +220,61 @@ react_component('HelloMessage', {name: 'John'}, {prerender: true})
211220
```
212221
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.
213222

223+
### Component Generator
224+
225+
react-rails ships with a Rails generator to help you get started with a simple component scaffold. You can run it using `rails generate react:component ComponentName`. The generator takes an optional list of arguments for default propTypes, which follow the conventions set in the [Reusable Components](http://facebook.github.io/react/docs/reusable-components.html) section of the React documentation.
226+
227+
For example:
228+
229+
```shell
230+
rails generate react:component Post title:string body:string published:bool published_by:instanceOf{Person}
231+
```
232+
233+
would generate the following in `app/assets/javascripts/components/post.js.jsx`:
234+
235+
```jsx
236+
var Post = React.createClass({
237+
propTypes: {
238+
title: React.PropTypes.string,
239+
body: React.PropTypes.string,
240+
published: React.PropTypes.bool,
241+
publishedBy: React.PropTypes.instanceOf(Person)
242+
},
243+
244+
render: function() {
245+
return (
246+
<div>
247+
<div>Title: {this.props.title}</div>
248+
<div>Body: {this.props.body}</div>
249+
<div>Published: {this.props.published}</div>
250+
<div>Published By: {this.props.published_by}</div>
251+
</div>
252+
);
253+
}
254+
});
255+
```
256+
257+
The generator can use the following arguments to create basic propTypes:
258+
259+
* any
260+
* array
261+
* bool
262+
* element
263+
* func
264+
* number
265+
* object
266+
* node
267+
* shape
268+
* string
269+
270+
The following additional arguments have special behavior:
271+
272+
* `instanceOf` takes an optional class name in the form of {className}
273+
* `oneOf` behaves like an enum, and takes an optional list of strings in the form of `'name:oneOf{one,two,three}'`.
274+
* `oneOfType` takes an optional list of react and custom types in the form of `'model:oneOfType{string,number,OtherType}'`
275+
276+
Note that the arguments for `oneOf` and `oneOfType` must be enclosed in single quotes to prevent your terminal from expanding them into an argument list.
277+
214278
## Configuring
215279

216280
### Variants
@@ -260,8 +324,6 @@ end
260324
It is possible to use JSX with CoffeeScript. The caveat is that you will still need to include the docblock. Since CoffeeScript doesn't allow `/* */` style comments, we need to do something a little different. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
261325

262326
```coffee
263-
###* @jsx React.DOM ###
264-
265327
Component = React.createClass
266328
render: ->
267329
`<ExampleComponent videos={this.props.videos} />`

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Rake::TestTask.new(:test) do |t|
1414
t.libs << 'test'
1515
t.pattern = ENV['TEST_PATTERN'] || 'test/**/*_test.rb'
1616
t.verbose = ENV['TEST_VERBOSE'] == '1'
17+
t.warning = true
1718
end
1819

1920
task default: :test

gemfiles/rails_3.1.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ source "http://rubygems.org"
55
gem "rails", "~> 3.1"
66
gem "sprockets", ">= 2.2.2"
77

8-
gemspec :path=>"../"
8+
gemspec :path => "../"

gemfiles/rails_3.2.gemfile

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

55
gem "rails", "~> 3.2"
66

7-
gemspec :path=>"../"
7+
gemspec :path => "../"

gemfiles/rails_4.0.gemfile

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

55
gem "rails", "~> 4.0"
66

7-
gemspec :path=>"../"
7+
gemspec :path => "../"

gemfiles/rails_4.0_with_therubyracer.gemfile

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

55
gem "rails", "~> 4.0"
6-
gem "therubyracer", "0.12.0", :platform=>:mri
6+
gem "therubyracer", "0.12.0", :platform => :mri
77

8-
gemspec :path=>"../"
8+
gemspec :path => "../"

gemfiles/rails_4.1.gemfile

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

55
gem "rails", "~> 4.1"
66

7-
gemspec :path=>"../"
7+
gemspec :path => "../"

gemfiles/rails_4.2.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.2"
6+
7+
gemspec :path => "../"

0 commit comments

Comments
 (0)