Skip to content

Commit 0e5ded5

Browse files
author
Robert Mosolgo
committed
Merge pull request #387 from kaiwood/coffeescript_generator
Add --coffee option to component generator
2 parents 88c6b64 + 63d88cd commit 0e5ded5

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ For example:
269269
rails generate react:component Label label:string --es6
270270
```
271271

272+
**--coffee** : Generate the component using CoffeeScript syntax
273+
274+
For example:
275+
276+
```shell
277+
rails generate react:component Label label:string --coffee
278+
```
279+
272280
#### Arguments
273281

274282
The generator can use the following arguments to create basic propTypes:

lib/generators/react/component_generator.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
5151
:banner => "field[:type] field[:type] ..."
5252

5353
class_option :es6,
54-
type: :boolean,
55-
default: false,
56-
desc: 'Output es6 class based component'
54+
type: :boolean,
55+
default: false,
56+
desc: 'Output es6 class based component'
57+
58+
class_option :coffee,
59+
type: :boolean,
60+
default: false,
61+
desc: 'Output coffeescript based component'
5762

5863
REACT_PROP_TYPES = {
5964
"node" => 'React.PropTypes.node',
@@ -85,7 +90,15 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
8590
}
8691

8792
def create_component_file
88-
extension = options[:es6] ? "es6.jsx" : "js.jsx"
93+
extension = case
94+
when options[:es6]
95+
'es6.jsx'
96+
when options[:coffee]
97+
'js.jsx.coffee'
98+
else
99+
'js.jsx'
100+
end
101+
89102
file_path = File.join('app/assets/javascripts/components', "#{file_name}.#{extension}")
90103
template("component.#{extension}", file_path)
91104
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class @<%= file_name.camelize %> extends React.Component
2+
<% if attributes.size > 0 -%>
3+
@propTypes =
4+
<% attributes.each do |attribute| -%>
5+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %>
6+
<% end -%>
7+
8+
<% end -%>
9+
render: ->
10+
<% if attributes.size > 0 -%>
11+
`<div>
12+
<% attributes.each do |attribute| -%>
13+
<div><%= attribute[:name].titleize %>: {this.props.<%= attribute[:name].camelize(:lower) %>}</div>
14+
<% end -%>
15+
</div>`
16+
<% else -%>
17+
`<div />`
18+
<% end -%>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'test_helper'
2+
require 'generators/react/component_generator'
3+
4+
class CoffeeComponentGeneratorTest < Rails::Generators::TestCase
5+
destination File.join(Rails.root, 'tmp', 'component_generator_test_output')
6+
setup :prepare_destination
7+
tests React::Generators::ComponentGenerator
8+
9+
def filename
10+
'app/assets/javascripts/components/generated_component.js.jsx.coffee'
11+
end
12+
13+
def class_name
14+
'GeneratedComponent'
15+
end
16+
17+
test 'that it the uses CoffeeScript syntax' do
18+
run_generator %w(GeneratedComponent name --coffee)
19+
20+
assert_file filename, /^class @#{class_name}\sextends\sReact\.Component/
21+
end
22+
23+
test 'that propTypes get assigned' do
24+
run_generator %w(GeneratedComponent name --coffee)
25+
26+
assert_file filename, /@propTypes\s=/
27+
assert_file filename, /React.PropTypes/
28+
end
29+
30+
test 'that it generates working jsx' do
31+
expected_name_div = /React\.createElement\(\s*"div",\s*null,\s*"Name:\s*",\s*this\.props\.name\s*\)/x
32+
expected_shape_div = /React\.createElement\(\s*"div",\s*null,\s*"Address:\s*",\s*this\.props\.address\s*\)/x
33+
34+
run_generator %w(GeneratedComponent name:string address:shape --coffee)
35+
jsx = React::JSX.transform(CoffeeScript.compile(File.read(File.join(destination_root, filename))))
36+
37+
assert_match(Regexp.new(expected_name_div), jsx)
38+
assert_match(Regexp.new(expected_shape_div), jsx)
39+
end
40+
end

0 commit comments

Comments
 (0)