react_rails_webpack 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/CODE_OF_CONDUCT.md +49 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +179 -0
  7. data/Rakefile +2 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +8 -0
  10. data/lib/react_rails_webpack/app/controllers/react_example_controller.rb +6 -0
  11. data/lib/react_rails_webpack/app/helpers/react_helper.rb +21 -0
  12. data/lib/react_rails_webpack/app/views/react_example/greeting.html.erb +2 -0
  13. data/lib/react_rails_webpack/client/environment.json +3 -0
  14. data/lib/react_rails_webpack/client/package.json +48 -0
  15. data/lib/react_rails_webpack/client/src/app/DevTools.jsx +22 -0
  16. data/lib/react_rails_webpack/client/src/app/availableComponents.js +15 -0
  17. data/lib/react_rails_webpack/client/src/app/initializeReduxStore.js +22 -0
  18. data/lib/react_rails_webpack/client/src/app/renderingComponentsInDivs.js +36 -0
  19. data/lib/react_rails_webpack/client/src/app/wrapWithProvider.jsx +28 -0
  20. data/lib/react_rails_webpack/client/src/app.js +22 -0
  21. data/lib/react_rails_webpack/client/src/app.sass +0 -0
  22. data/lib/react_rails_webpack/client/src/components/Hello.jsx +20 -0
  23. data/lib/react_rails_webpack/client/src/components/HelloWithRedux.jsx +30 -0
  24. data/lib/react_rails_webpack/client/src/redux/reducer.js +20 -0
  25. data/lib/react_rails_webpack/client/test/redux/actions/set_greeting_spec.js +17 -0
  26. data/lib/react_rails_webpack/client/test/test_helper.js +17 -0
  27. data/lib/react_rails_webpack/client/webpack.config.js +32 -0
  28. data/lib/react_rails_webpack/create_example_page_generator.rb +38 -0
  29. data/lib/react_rails_webpack/install_generator.rb +90 -0
  30. data/lib/react_rails_webpack/lib/react_cell.rb +22 -0
  31. data/lib/react_rails_webpack/package.json +9 -0
  32. data/lib/react_rails_webpack/trailblazer_integration_generator.rb +15 -0
  33. data/lib/react_rails_webpack/version.rb +3 -0
  34. data/lib/react_rails_webpack.rb +11 -0
  35. data/react_rails_webpack.gemspec +27 -0
  36. metadata +135 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2afcde60582db2cc8f2515626631b4487a623e52
4
+ data.tar.gz: dd31b30aaf1daad94c2bb6d42ad03bf310bc0949
5
+ SHA512:
6
+ metadata.gz: 541cd90bdfcede1cd55f82bb1b43901eb9af7db2fdc5f6fb0c00aec6daea50082306878765b43ecd040de91a19639263915331875db0f39753b33d28d75321e8
7
+ data.tar.gz: 1f73ba6a30b9bab83c02c9f996e88ad44ec98e5b73c8def14f9831ade03e2b93db669cb37a9fe5143a06ceeda8852588eceb2c9dc910d6b4bd6d16ab83337917
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /lib/react_rails_webpack/client/node_modules
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at neurodynamicdev@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in react_rails_webpack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Neurodynamic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # ReactRailsWebpack
2
+
3
+ A set of Rails generators that create all the necessary boilerplate code for integrating React, Webpack, and Redux libraries into Rails 4.0+ apps.
4
+
5
+ ## Quick Start
6
+
7
+ ### Install
8
+
9
+ 1. Make sure you have the [requirements](#requirements) installed
10
+ 2. Add `gem 'react_rails_webpack'` to your Gemfile
11
+ 3. Run `bundle install`
12
+ 4. Run `rails g react_rails_webpack:install` ([further explanation of what this does](#generating-the-integration))
13
+ 5. Run `npm run install`
14
+ 6. Run `npm run build`
15
+ 7. Set the dev server hostname ([instructions](#setting-the-dev-server-hostname))
16
+
17
+ ### Add example page
18
+
19
+ 1. Run `rails g react_rails_webpack:create_example_page`
20
+ 2. Start the Rails server (run `rails server`)
21
+ 3. Go to [localhost:3000/greeting](http://localhost:3000/greeting) to see your React/Rails integration in action
22
+
23
+ ### Suggested Development Workflow
24
+
25
+ 1. Write and test new components by running the webpack dev server with the command `npm run start` (note: the base html page for the webpack dev server is based on the html provided to `context.defaultTemplate` in [the webpack.config.js file](lib/react_rails_webpack/client/webpack.config.js))
26
+ 2. Add any components you want to spin up with the `react_component` helper in Rails to [the available components file](lib/react_rails_webpack/client/src/app/availableComponents.js) ([further explanation](#creating-and-using-your-own-components))
27
+ 3. Run the `npm run build` command to compile your javascript files and add them to the Rails assets folder
28
+ 4. Add calls to the `react_component` helper method in your Rails app to render your React components in views
29
+ 5. Make sure they still work, repeat steps 1-5 until satisfied
30
+
31
+ ## About
32
+
33
+ When I looked over the available gems for react/rails integrations, none of them were as customizable as I wanted, so I decided to build my own. Since I wanted it to be as customizable as possible, I made my gem a set of generators, instead of a bunch of under-the-hood gem code. You can customize the inner workings of this integration to your heart's content, since all the files used for the integration will be added directly to your app. You can edit them, delete them, and add or remove any npm packages you like, because this integration allows you to use any npm packages you could use in a static front-end react app.
34
+
35
+ ### Pros:
36
+
37
+ - Webpack integration
38
+ - Hot-reloading webpack development server
39
+ - Ability to see dev server output on any computer (or mobile device) on your network
40
+ - Built-in redux integration
41
+ - Generators provide example code for basic react components and react-redux components
42
+ - Highly customizable
43
+ - Ability to use any npm packages that you could use on a static front-end app
44
+
45
+ ### Cons:
46
+
47
+ - This gem does not do server-side rendering, so if you need to do SEO stuff within your react components, it's probably the wrong choice
48
+
49
+ ## Installing the Gem
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ ```ruby
54
+ gem 'react_rails_webpack'
55
+ ```
56
+
57
+ And then execute:
58
+
59
+ $ bundle
60
+
61
+ Or install it yourself as:
62
+
63
+ $ gem install react_rails_webpack
64
+
65
+ ## Requirements
66
+
67
+ - [Rails](http://rubyonrails.org/) 4.0+
68
+ - [node](https://nodejs.org/) (tested on 4.2.2)
69
+ - [npm](https://www.npmjs.com/) (tested on 3.4.0)
70
+
71
+ ## Generating the Integration
72
+
73
+ Run the install generator like this:
74
+
75
+ $ rails g react_rails_webpack:install
76
+
77
+ This will setup a basic react integration with some example components (one standard react component and one using react with redux) under [a client folder](lib/react_rails_webpack/client) in your project's root. Once the generator's run is done, run the commands below (from your project's root):
78
+
79
+ **Note**: Make sure you have [node](https://nodejs.org/) and [npm](https://www.npmjs.com/) installed on your machine before running these.
80
+
81
+ $ npm run install # installs the needed npm packages
82
+ $ npm run build # uses webpack to compile your javascript code to your assets folder
83
+
84
+ Then set the dev server hostname ([instructions](#setting-the-dev-server-hostname)).
85
+
86
+ ## Setting the dev server hostname
87
+
88
+ Then go to [the client/environment.json file](lib/react_rails_webpack/client/environment.json) and fill in your computer's network name. If you're using a Mac, to find out what your computer's network name is, you can go to `System Preferences`, and then the `Sharing` menu, and you should see it in the `Computer Name` field. `environment.json` should end up looking something like this:
89
+
90
+ ```
91
+ {
92
+ "hostname": "my_mac.local"
93
+ }
94
+ ```
95
+
96
+ ## Generating an Example Page
97
+
98
+ If you want to setup an example page in your Rails app that uses the provided React components, run:
99
+
100
+ $ rails g react_rails_webpack:create_example_page
101
+
102
+ Then run the rails server and go to [the greeting page](localhost:3000/greeting) to see everything in action.
103
+
104
+ ## Creating and using your own components
105
+
106
+ Let's say you want to make a checkout form component with react. Here's what you'd need to do to use it from Rails:
107
+
108
+ - Create the component in the [client/src/components](lib/react_rails_webpack/client/src/components) directory
109
+ - Add the component to the list of components in the [client/src/app/availableComponents.js](lib/react_rails_webpack/client/src/app/availableComponents.js) file (this file is what makes components available to Rails)
110
+
111
+ For example, if you called your component `CheckoutForm`, your `client/src/app/availableComponents.js` file might look like this:
112
+
113
+ ```javascript
114
+ import CheckoutForm from '../components/CheckoutForm'
115
+
116
+ export default {
117
+ CheckoutForm: {
118
+ class: CheckoutForm
119
+ }
120
+ }
121
+ ```
122
+
123
+ Or like this if it's a component that uses redux:
124
+
125
+ ```javascript
126
+ import {CheckoutFormContainer} from '../components/CheckoutForm'
127
+
128
+ export default {
129
+ CheckoutForm: {
130
+ redux: true,
131
+ class: CheckoutFormContainer
132
+ }
133
+ }
134
+ ```
135
+
136
+ - Wherever you want this component to render in your view, put a call to the `react_component` helper method with the components name and props, like so:
137
+
138
+ ```ruby
139
+ render_component "CheckoutForm", { customerName: 'Harper' }
140
+ ```
141
+
142
+ ## Adding Trailblazer Integration
143
+
144
+ Running this generator:
145
+
146
+ $ rails g react_rails_webpack:trailblazer_integration
147
+
148
+ Will add a [trailblazer](https://github.com/apotonick/trailblazer) cell for react components to your `lib` folder.
149
+
150
+
151
+ ## Working with the Webpack Dev Server
152
+
153
+ `npm run start` will start a webpack development server with hot reloading that is completely independent of your Rails app. You can see the output of this server on any computer or mobile device on your local network by going to `hostname:3000` (replace `hostname` with whatever you set it to in the `hostname.json` file).
154
+
155
+ ## Gotchas
156
+
157
+ ### Forgetting to run `npm run build`
158
+
159
+ Remember, while your changes to components will hot reload when you use the webpack dev server, they will not show up at all in your Rails app until you run the `npm run build` command.
160
+
161
+ ### Forgetting to add components to the `client/src/app/availableComponents.js` file
162
+
163
+ Components will not be accessible from Rails if you forget to add them here.
164
+
165
+ ## Development
166
+
167
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
168
+
169
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
170
+
171
+ ## Contributing
172
+
173
+ Bug reports and pull requests are welcome on GitHub at https://github.com/neurodynamic/react_rails_webpack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
174
+
175
+
176
+ ## License
177
+
178
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
179
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "react_rails_webpack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ class ReactExampleController < ApplicationController
2
+ def greeting
3
+ @props_for_hello_component = { details: 'My initial greeting was set with getInitialState in the client/src/components/Hello.jsx file.' }
4
+ @props_for_hello_with_redux_component = { details: 'My initial greeting was set with initialState in the client/src/redux/reducer.js file.' }
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ require 'json'
2
+
3
+ module ReactHelper
4
+ include ActionView::Helpers::JavaScriptHelper
5
+
6
+ # Props should be a Ruby hash
7
+ def react_component(component_name, props = {})
8
+ # Give second argument to content_tag some text
9
+ # if you want to test to see if React is properly
10
+ # replacing its content with a component
11
+ content_tag(
12
+ :div,
13
+ javascript_tag('renderLastComponentDiv()'),
14
+ class: 'react-component-target',
15
+ data: {
16
+ componentName: component_name,
17
+ componentProps: props.to_json
18
+ }
19
+ )
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ <%= react_component "Hello", @props_for_hello_component %>
2
+ <%= react_component "HelloWithRedux", @props_for_hello_with_redux_component %>
@@ -0,0 +1,3 @@
1
+ {
2
+ "hostname": "YOUR_COMPUTERS_NAME_ON_YOUR_NETWORK.local"
3
+ }
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "react-client",
3
+ "version": "1.0.0",
4
+ "description": "react client-side",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "webpack-dev-server",
8
+ "prebuild": "rm -rf ../app/assets/webpack && mkdir ../app/assets/webpack",
9
+ "build": "NODE_ENV=production webpack",
10
+ "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js 'test/**/*.@(js|jsx)'"
11
+ },
12
+ "author": "",
13
+ "license": "ISC",
14
+ "dependencies": {
15
+ "autoprefixer": "^6.0.3",
16
+ "babel": "^5.8.23",
17
+ "babel-loader": "^5.3.2",
18
+ "css-loader": "^0.21.0",
19
+ "file-loader": "^0.8.4",
20
+ "history": "^1.12.5",
21
+ "hjs-webpack": "neurodynamic/hjs-webpack",
22
+ "json-loader": "^0.5.3",
23
+ "postcss-loader": "^0.7.0",
24
+ "react": "^0.14.6",
25
+ "react-dom": "^0.14.0",
26
+ "react-hot-loader": "^1.3.0",
27
+ "react-redux": "^4.0.6",
28
+ "react-script-loader": "0.0.1",
29
+ "react-tap-event-plugin": "^0.2.1",
30
+ "redux": "^3.0.4",
31
+ "redux-form": "^4.0.7",
32
+ "style-loader": "^0.13.0",
33
+ "url-loader": "^0.5.6",
34
+ "webpack": "^1.12.6",
35
+ "webpack-dev-server": "^1.12.1"
36
+ },
37
+ "devDependencies": {
38
+ "chai": "^3.4.0",
39
+ "chai-immutable": "^1.3.0",
40
+ "jsdom": "^7.0.2",
41
+ "mocha": "^2.3.3",
42
+ "node-sass": "^3.4.2",
43
+ "redux-devtools": "^3.0.1",
44
+ "redux-devtools-dock-monitor": "^1.0.1",
45
+ "redux-devtools-log-monitor": "^1.0.2",
46
+ "sass-loader": "^3.1.1"
47
+ }
48
+ }
@@ -0,0 +1,22 @@
1
+ import React from 'react'
2
+
3
+ // Exported from redux-devtools
4
+ import { createDevTools } from 'redux-devtools'
5
+
6
+ // Monitors are separate packages, and you can make a custom one
7
+ import LogMonitor from 'redux-devtools-log-monitor'
8
+ import DockMonitor from 'redux-devtools-dock-monitor'
9
+
10
+ // createDevTools takes a monitor and produces a DevTools component
11
+ const DevTools = createDevTools(
12
+ // Monitors are individually adjustable with props.
13
+ // Consult their repositories to learn about those props.
14
+ // Here, we put LogMonitor inside a DockMonitor.
15
+ <DockMonitor toggleVisibilityKey='ctrl-h'
16
+ changePositionKey='ctrl-q'
17
+ defaultIsVisible={false}>
18
+ <LogMonitor theme='tomorrow' />
19
+ </DockMonitor>
20
+ )
21
+
22
+ export default DevTools
@@ -0,0 +1,15 @@
1
+ import Hello from '../components/Hello'
2
+ import {HelloWithReduxContainer} from '../components/HelloWithRedux'
3
+
4
+ // Edit this list with your own React components.
5
+ // Set the redux key to true to integrate them
6
+ // with redux for application state management.
7
+ export default {
8
+ HelloWithRedux: {
9
+ redux: true,
10
+ class: HelloWithReduxContainer
11
+ },
12
+ Hello: {
13
+ class: Hello
14
+ }
15
+ }
@@ -0,0 +1,22 @@
1
+ import {compose, createStore, combineReducers} from 'redux'
2
+ import DevTools from './DevTools'
3
+
4
+ import exampleReducer from '../redux/reducer'
5
+
6
+ // If you have more than one reducer, combine them here
7
+ const reducer = combineReducers({
8
+ exampleReducer: exampleReducer
9
+ })
10
+
11
+ const finalCreateStore = compose(
12
+ // Middleware you want to use in development:
13
+ // applyMiddleware(d1, d2, d3),
14
+ // Required! Enable Redux DevTools with the monitors you chose
15
+ DevTools.instrument()
16
+ )(createStore)
17
+
18
+ export default function () {
19
+ const store = finalCreateStore(reducer)
20
+
21
+ return store
22
+ }
@@ -0,0 +1,36 @@
1
+ import React from 'react'
2
+ import ReactDOM from 'react-dom'
3
+
4
+ import wrapWithProvider from './wrapWithProvider'
5
+
6
+ // Used in findAndCreateComponent
7
+ import components from './availableComponents'
8
+
9
+ // This function gets called by the react_rails_webpack Rails helper
10
+ // It renders the specified component in the div from which the
11
+ // function is called.
12
+ export function renderLastComponentDiv () {
13
+ const elements = document.getElementsByClassName('react-component-target')
14
+ const lastElementIndex = elements.length - 1
15
+ renderReactComponentInDiv(elements[lastElementIndex])
16
+ }
17
+
18
+ export function renderReactComponentInDiv (targetDiv) {
19
+ const componentName = targetDiv.getAttribute('data-componentname')
20
+ const componentProps = JSON.parse(targetDiv.getAttribute('data-componentprops'))
21
+
22
+ ReactDOM.render(
23
+ findAndCreateComponent(componentName, componentProps),
24
+ targetDiv)
25
+ }
26
+
27
+ function findAndCreateComponent (componentName, props) {
28
+ const component = components[componentName]
29
+
30
+ if (component.redux) {
31
+ const element = React.createElement(component.class, props)
32
+ return wrapWithProvider(element)
33
+ } else {
34
+ return React.createElement(component.class, props)
35
+ }
36
+ }
@@ -0,0 +1,28 @@
1
+ import React from 'react'
2
+ import DevTools from './DevTools'
3
+ import initializeReduxStore from './initializeReduxStore'
4
+ import {Provider} from 'react-redux'
5
+
6
+ export default function wrapWithProvider (component, devTools = true) {
7
+ // Add DevTools if this is NOT a production app
8
+ if (process.env.NODE_ENV !== 'production' && devTools) {
9
+ component = wrapWithDevTools(component)
10
+ }
11
+
12
+ // return component wrapped in Provider
13
+ // with initialized react-redux store
14
+ return React.createElement(
15
+ Provider,
16
+ {
17
+ store: initializeReduxStore(),
18
+ children: component
19
+ }
20
+ )
21
+ }
22
+
23
+ export function wrapWithDevTools (component) {
24
+ return <div className='debug-panel-and-content-wrapper'>
25
+ {component}
26
+ <DevTools />
27
+ </div>
28
+ }
@@ -0,0 +1,22 @@
1
+ import {
2
+ renderReactComponentInDiv, renderLastComponentDiv
3
+ } from './app/renderingComponentsInDivs'
4
+
5
+ import './app.sass'
6
+ import injectTapEventPlugin from 'react-tap-event-plugin'
7
+
8
+ injectTapEventPlugin()
9
+
10
+ // In development, render things automatically
11
+ if (process.env.NODE_ENV !== 'production') {
12
+ const componentDivs = document.getElementsByClassName('react-component-target')
13
+
14
+ Array.prototype.forEach.call(componentDivs, (div) => {
15
+ renderReactComponentInDiv(div)
16
+ })
17
+ } else {
18
+ // In production, expose these methods
19
+ // so they can be called by the app
20
+ window.renderLastComponentDiv = renderLastComponentDiv
21
+ document.renderLastComponentDiv = renderLastComponentDiv
22
+ }
File without changes
@@ -0,0 +1,20 @@
1
+ import React from 'react'
2
+
3
+ export default React.createClass({
4
+ propTypes: {
5
+ details: React.PropTypes.string.isRequired
6
+ },
7
+ getInitialState: function () {
8
+ return { greeting: 'Hello there, friend!' }
9
+ },
10
+ render () {
11
+ return <div>
12
+ <h2>This is the "Hello" component from 'components/Hello.jsx'</h2>
13
+ <p><strong>Details</strong>: {this.props.details}</p>
14
+ <p>Current greeting: {this.state.greeting}</p>
15
+
16
+ <label htmlFor='greeting'>Change the greeting here: </label>
17
+ <input type='text' id='greeting' value={this.state.greeting} onChange={(e) => this.setState({ greeting: e.target.value })}/>
18
+ </div>
19
+ }
20
+ })
@@ -0,0 +1,30 @@
1
+ import React from 'react'
2
+ import {connect} from 'react-redux'
3
+ import * as actionCreators from '../redux/reducer'
4
+
5
+ export const HelloWithRedux = React.createClass({
6
+ propTypes: {
7
+ greeting: React.PropTypes.string.isRequired,
8
+ details: React.PropTypes.string.isRequired,
9
+ setGreeting: React.PropTypes.func.isRequired
10
+ },
11
+ render () {
12
+ return <div>
13
+ <h2>This is the "HelloWithRedux" component from 'components/HelloWithRedux.jsx'</h2>
14
+ <p><strong>Details</strong>: {this.props.details}</p>
15
+ <p>Current greeting: {this.props.greeting}</p>
16
+
17
+ <label htmlFor='greeting'>Change the greeting here: </label>
18
+ <input type='text' id='greeting' value={this.props.greeting} onChange={(e) => this.props.setGreeting(e.target.value)}/>
19
+ </div>
20
+ }
21
+ })
22
+
23
+ function mapStateToProps (state) {
24
+ return { greeting: state.exampleReducer.greeting }
25
+ }
26
+
27
+ export const HelloWithReduxContainer = connect(
28
+ mapStateToProps,
29
+ actionCreators
30
+ )(HelloWithRedux)
@@ -0,0 +1,20 @@
1
+ // Example redux reducer
2
+ // Reducer style reference: https://github.com/erikras/ducks-modular-redux
3
+
4
+ const SET_GREETING = 'SET_GREETING'
5
+
6
+ const initialState = { greeting: 'Salutations!' }
7
+
8
+ export default function reducer (state = initialState, action) {
9
+ switch (action.type) {
10
+ case SET_GREETING:
11
+ return { greeting: action.greeting }
12
+
13
+ default:
14
+ return state
15
+ }
16
+ }
17
+
18
+ export function setGreeting (greeting) {
19
+ return { type: SET_GREETING, greeting: greeting }
20
+ }
@@ -0,0 +1,17 @@
1
+ import {expect} from 'chai'
2
+
3
+ import reducer from '../../../src/redux/reducer'
4
+ import {setGreeting} from '../../../src/redux/reducer'
5
+
6
+ describe('setGreeting', () => {
7
+ it('sets the greeting', () => {
8
+ const initialState = 'Heck off'
9
+
10
+ const action = setGreeting('Lovely to meet you')
11
+
12
+ const nextState = reducer(initialState, action)
13
+ const expectedState = 'Lovely to meet you'
14
+
15
+ expect(nextState).to.equal(expectedState)
16
+ })
17
+ })
@@ -0,0 +1,17 @@
1
+ import jsdom from 'jsdom'
2
+ import chai from 'chai'
3
+ import chaiImmutable from 'chai-immutable'
4
+
5
+ const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
6
+ const win = doc.defaultView
7
+
8
+ global.document = doc
9
+ global.window = win
10
+
11
+ Object.keys(window).forEach((key) => {
12
+ if (!(key in global)) {
13
+ global[key] = window[key]
14
+ }
15
+ })
16
+
17
+ chai.use(chaiImmutable)
@@ -0,0 +1,32 @@
1
+ require('webpack')
2
+ var getConfig = require('hjs-webpack')
3
+ var environment = require('./environment.json')
4
+
5
+ var config = getConfig({
6
+ in: 'src/app.js',
7
+ out: '../app/assets/webpack',
8
+ minify: false,
9
+ html: function (context) {
10
+ return {
11
+ 'index.html':
12
+ context.defaultTemplate(
13
+ {
14
+ // title: 'Site Title'
15
+ // head: '<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>',
16
+ html: "<div class=\"react-component-target\" data-componentname=\"Hello\" data-componentprops='{ \"details\": \"My initial greeting was set with getInitialState in the client/src/components/Hello.jsx file.\" }'></div>" +
17
+ "<div class=\"react-component-target\" data-componentname=\"HelloWithRedux\" data-componentprops='{ \"details\": \"My initial greeting was set with initialState in the client/src/redux/reducer.js file.\" }'></div>"
18
+ }
19
+ )
20
+ }
21
+ },
22
+
23
+ // environment.hostname is set in your environment.json file
24
+ // Set it to your computer's name on your network
25
+ // On a Mac, to see your computer's name on your network,
26
+ // go to System Preferences, and then the "Sharing" menu,
27
+ // and you should see a "Computer Name" input field
28
+ hostname: environment.hostname,
29
+ isDev: process.env.NODE_ENV !== 'production'
30
+ })
31
+
32
+ module.exports = config
@@ -0,0 +1,38 @@
1
+ require 'rails/generators/base'
2
+ require 'fileutils'
3
+
4
+ module ReactRailsWebpack
5
+ class CreateExamplePageGenerator < Rails::Generators::Base
6
+ desc "This generator sets up a demo page for react/rails integration in a Rails app."
7
+
8
+ source_root(File.join(File.expand_path(File.dirname(__FILE__))))
9
+
10
+ def add_files
11
+ inside 'app' do
12
+ inside 'controllers' do
13
+ copy_file 'react_example_controller.rb'
14
+ end
15
+
16
+ inside 'views' do
17
+ FileUtils.mkdir_p 'react_example'
18
+
19
+ inside 'react_example' do
20
+ copy_file 'greeting.html.erb'
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def add_route
27
+ insert_into_file(
28
+ 'config/routes.rb',
29
+ " get 'greeting' => 'react_example#greeting'\n",
30
+ after: "Rails.application.routes.draw do\n"
31
+ )
32
+ end
33
+
34
+ def print_next_steps
35
+ puts "\n\nDone! Start your Rails server and go to localhost:3000/greeting to see your React/Rails integration in action!"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,90 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ReactRailsWebpack
4
+ class InstallGenerator < Rails::Generators::Base
5
+ desc "This generator sets up the files for a rails and react integration using webpack."
6
+
7
+ source_root(File.join(File.expand_path(File.dirname(__FILE__))))
8
+
9
+ def setup_client_folder
10
+ puts 'Adding client folder...'
11
+ directory 'client'
12
+ end
13
+
14
+ def add_root_package_dot_json
15
+ copy_file 'package.json'
16
+ end
17
+
18
+ def add_react_helper
19
+ puts 'Adding react_helper.rb...'
20
+ inside 'app' do
21
+ inside 'helpers' do
22
+ copy_file 'react_helper.rb'
23
+ end
24
+ end
25
+ end
26
+
27
+ def update_gitignore
28
+ puts 'updating .gitignore...'
29
+ append_to_file '.gitignore' do
30
+ "\n\n\# react_rails_webpack ignores\nclient/node_modules\nclient/environment.json\nnpm-debug.log"
31
+ end
32
+ end
33
+
34
+ def add_webpack_asset_inclusion
35
+ puts 'Adding asset includes...'
36
+ # Add webpack folder to application asset paths
37
+ app_dot_rb_insert = " config.assets.paths << \"\#\{config.root\}/app/assets/webpack\"\n"
38
+ insert_into_file(
39
+ 'config/application.rb',
40
+ app_dot_rb_insert,
41
+ after: " class Application < Rails::Application\n"
42
+ ) unless file_already_contains?(app_dot_rb_insert, 'config/application.rb')
43
+
44
+ # Add webpack folder require to application.js
45
+ app_dot_js_insert = "//= require_tree ../webpack\n"
46
+ insert_into_file(
47
+ 'app/assets/javascripts/application.js',
48
+ app_dot_js_insert,
49
+ after: "//= require_tree .\n"
50
+ ) unless file_already_contains?(app_dot_js_insert, 'app/assets/javascripts/application.js')
51
+
52
+ # Add webpack folder require to application.css or .scss or .sass
53
+ if File.exist?('app/assets/stylesheets/application.css')
54
+ insert_into_file(
55
+ 'app/assets/stylesheets/application.css',
56
+ "\n *= require_tree ../webpack\n",
57
+ before: ' *= require_self'
58
+ ) unless file_already_contains?("\n *= require_tree ../webpack\n", 'app/assets/stylesheets/application.css')
59
+ end
60
+
61
+ ensure_prepended "@import '../webpack/*\n", 'app/assets/stylesheets/application.scss' if File.exist?('app/assets/stylesheets/application.scss')
62
+ ensure_prepended "@import '../webpack/*\n", 'app/assets/stylesheets/application.sass' if File.exist?('app/assets/stylesheets/application.sass')
63
+ end
64
+
65
+ def print_reminders
66
+ puts "\n\nDone! Now make sure you have node and npm installed, and then run the 'npm run install' and 'npm run build' commands to finish setting up."
67
+ puts "Also, don't forget to set your hostname in the client/environment.json file. Instructions for this can be found here: https://github.com/neurodynamic/react_rails_webpack#setting-the-dev-server-hostname"
68
+ end
69
+
70
+
71
+ private
72
+
73
+ def current_directory_path
74
+ File.join(File.expand_path(File.dirname(__FILE__)))
75
+ end
76
+
77
+ def ensure_prepended(line, file)
78
+ return if file_already_contains?(line, file)
79
+ prepend_to_file(file, line)
80
+ end
81
+
82
+ def file_missing?(file)
83
+ !File.exist?(file)
84
+ end
85
+
86
+ def file_already_contains?(string, file)
87
+ File.readlines(file).join.include?(string)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+
3
+ class ReactCell < Cell::Concept
4
+ include ActionView::Helpers::JavaScriptHelper
5
+ include ActionView::Helpers::TagHelper
6
+
7
+ # Props should be a Ruby hash
8
+ def react_component(component_name, props = {})
9
+ # Give second argument to content_tag some text
10
+ # if you want to test to see if React is properly
11
+ # replacing its content with a component
12
+ content_tag(
13
+ :div,
14
+ javascript_tag('renderLastComponentDiv()'),
15
+ class: 'react-component-target',
16
+ data: {
17
+ componentName: component_name,
18
+ componentProps: props.to_json
19
+ }
20
+ )
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ {
2
+ "scripts": {
3
+ "start": "cd client && npm run start",
4
+ "prebuild": "cd client && npm run prebuild",
5
+ "build": "cd client && npm run build",
6
+ "install": "cd client && npm install",
7
+ "test": "cd client && npm run test"
8
+ }
9
+ }
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ReactRailsWebpack
4
+ class TrailblazerIntegrationGenerator < Rails::Generators::Base
5
+ desc "This generator adds a react_cell file to use in a Trailblazer/Rails app."
6
+
7
+ source_root(File.join(File.expand_path(File.dirname(__FILE__))))
8
+
9
+ def add_react_cell_file_to_lib
10
+ inside 'lib' do
11
+ copy_file 'react_cell.rb'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module ReactRailsWebpack
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'react_rails_webpack/version'
2
+
3
+ module ReactRailsWebpack
4
+ class ReactRailsWebpackRailtie < Rails::Railtie
5
+ generators do
6
+ require_relative 'react_rails_webpack/install_generator'
7
+ require_relative 'react_rails_webpack/create_example_page_generator'
8
+ require_relative 'react_rails_webpack/trailblazer_integration_generator'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'react_rails_webpack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "react_rails_webpack"
8
+ spec.version = ReactRailsWebpack::VERSION
9
+ spec.authors = ["Neurodynamic"]
10
+ spec.email = ["neurodynamicdev@gmail.com"]
11
+
12
+ spec.summary = %q{A gem supplying generators to setup flexible integrations of react, webpack, and redux with Rails}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/neurodynamic/react_rails_webpack"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ # spec.add_dependency("railties", ">= 4.1.0", "< 5.1")
23
+ spec.add_dependency("rails", ">= 4")
24
+ spec.add_dependency "colorize", ">= 0.7.7"
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: react_rails_webpack
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Neurodynamic
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - neurodynamicdev@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/react_rails_webpack.rb
85
+ - lib/react_rails_webpack/app/controllers/react_example_controller.rb
86
+ - lib/react_rails_webpack/app/helpers/react_helper.rb
87
+ - lib/react_rails_webpack/app/views/react_example/greeting.html.erb
88
+ - lib/react_rails_webpack/client/environment.json
89
+ - lib/react_rails_webpack/client/package.json
90
+ - lib/react_rails_webpack/client/src/app.js
91
+ - lib/react_rails_webpack/client/src/app.sass
92
+ - lib/react_rails_webpack/client/src/app/DevTools.jsx
93
+ - lib/react_rails_webpack/client/src/app/availableComponents.js
94
+ - lib/react_rails_webpack/client/src/app/initializeReduxStore.js
95
+ - lib/react_rails_webpack/client/src/app/renderingComponentsInDivs.js
96
+ - lib/react_rails_webpack/client/src/app/wrapWithProvider.jsx
97
+ - lib/react_rails_webpack/client/src/components/Hello.jsx
98
+ - lib/react_rails_webpack/client/src/components/HelloWithRedux.jsx
99
+ - lib/react_rails_webpack/client/src/redux/reducer.js
100
+ - lib/react_rails_webpack/client/test/redux/actions/set_greeting_spec.js
101
+ - lib/react_rails_webpack/client/test/test_helper.js
102
+ - lib/react_rails_webpack/client/webpack.config.js
103
+ - lib/react_rails_webpack/create_example_page_generator.rb
104
+ - lib/react_rails_webpack/install_generator.rb
105
+ - lib/react_rails_webpack/lib/react_cell.rb
106
+ - lib/react_rails_webpack/package.json
107
+ - lib/react_rails_webpack/trailblazer_integration_generator.rb
108
+ - lib/react_rails_webpack/version.rb
109
+ - react_rails_webpack.gemspec
110
+ homepage: https://github.com/neurodynamic/react_rails_webpack
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.5.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: A gem supplying generators to setup flexible integrations of react, webpack,
134
+ and redux with Rails
135
+ test_files: []