-
Notifications
You must be signed in to change notification settings - Fork 751
Description
In #64, I laid the foundation for adding Rails generators. I'd like to discuss what other generators are possible, and what they might look like.
rails generate react:component [ComponentName]
I get really tired of typing /** @jsx React.DOM */ and all the other boilerplate that goes with creating a new component. Making a simple generator to scaffold up a new component is very straightforward.
rails generate react:component [ComponentName] [field:type] [field:type]
Similar to rails g scaffold
, I would propose that this style automatically add an appropriate propTypes
object as well as scaffolding out the JSX in a Rails-y way.
Adapting one of the Rails scaffold examples, a react-rails component generator would look something like this:
rails generate react:component post title body:text published:boolean
which would generate something like this:
// app/assets/javascripts/components/post.js.jsx
/** @jsx React.DOM */
var Post = React.createClass({
propTypes: {
id: React.PropTypes.number,
title: React.PropTypes.string,
body: React.PropTypes.string,
published: React.PropTypes.bool
},
render: function() {
return <div className="post">
<p>
<strong>Title:</strong>
<span>{this.props.title}</span>
</p>
<p>
<strong>Body:</strong>
<span>{this.props.body}</span>
</p>
<p>
<strong>Published:</strong>
<span>{this.props.published}</span>
</p>
</div>;
}
});
Note: I wrote this code without trying to run it, totally possible that I made some mistakes.
Wildly throwing spitballs:
Taking this even further, I wonder if it would be useful to create an entire scaffold generator? The addition of namespaced components makes this pretty intriguing to me. Running rails generate react:scaffold_component post title body:text published:boolean
could create a set of components, something like Posts.Post, Posts.List, Posts.Edit, Posts.New, and Posts.Form, very similar to how Rails scaffolding currently works.
Please comment and leave suggestions or requests! I'd love to build this next week, but I want to make sure it's both "React-y" and "Rails-y".