Skip to content

Commit

Permalink
use templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Judahmeek committed Nov 28, 2016
1 parent cf93bde commit 8fccc92
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
1 change: 0 additions & 1 deletion lib/generators/react_on_rails/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def template_base_files
Procfile.dev
app/views/hello_world/index.html.erb
package.json
client/app/bundles/HelloWorld/components/HelloWorldApp.jsx
client/package.json).each { |file| template(base_path + file + ".tt", file) }
end

Expand Down
16 changes: 11 additions & 5 deletions lib/generators/react_on_rails/react_no_redux_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ class ReactNoReduxGenerator < Rails::Generators::Base
include GeneratorHelper
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("../templates", __FILE__))

def template_appropriate_version_of_hello_world_app
filename = "registration.jsx"
location = "client/app/bundles/HelloWorld/startup"
template("no_redux/base/#{location}/registration.jsx.tt", "#{location}/#{filename}")

def create_appropriate_templates
base_path = "base/base/"
location = "client/app/bundles/HelloWorld/"
source = base_path + location
config = {
class_name: "HelloWorldApp",
app_relative_path: "../components/HelloWorldApp"
}
template(source + "/startup/registration.jsx" + ".tt", location + "/startup/registration.jsx", config)
template(source + "/components/HelloWorld.jsx" + ".tt", location + "/components/HelloWorldApp.jsx", config)
end
end
end
Expand Down
17 changes: 12 additions & 5 deletions lib/generators/react_on_rails/react_with_redux_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ def copy_base_redux_files
client/app/bundles/HelloWorld/constants/helloWorldConstants.jsx
client/app/bundles/HelloWorld/reducers/helloWorldReducer.jsx
client/app/bundles/HelloWorld/store/helloWorldStore.jsx
client/app/bundles/HelloWorld/startup/registration.jsx).each do |file|
client/app/bundles/HelloWorld/startup/HelloWorldApp.jsx).each do |file|
copy_file(base_path + file, file)
end
end

def template_appropriate_version_of_hello_world_app
filename = "HelloWorldApp.jsx"
location = "client/app/bundles/HelloWorld/startup"
template("redux/base/#{location}/HelloWorldApp.jsx.tt", "#{location}/#{filename}")
def create_appropriate_templates
base_path = "base/base/"
destination = "client/app/bundles/HelloWorld/"
config = {
class_name: "HelloWorld",
app_relative_path: "./HelloWorldApp"
}
%w(/startup/registration.jsx
/components/HelloWorld.jsx).each do |file|
template(base_path + destination + file + ".tt", destination + file, config)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { PropTypes } from 'react';

export default class HelloWorldApp extends React.Component {
export default class <%= config[:class_name] %> extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired, // this is passed from the Rails view
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReactOnRails from 'react-on-rails';

import HelloWorldApp from '../components/HelloWorldApp';
import HelloWorldApp from '<%= config[:app_relative_path] %>';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
shared_examples "no_redux_generator" do
it "copies non-redux base files" do
it "creates appropriate templates" do
assert_file("client/app/bundles/HelloWorld/startup/registration.jsx") do |contents|
assert_match("HelloWorldApp", contents)
assert_match("../components/HelloWorldApp", contents)
end
assert_file("client/app/bundles/HelloWorld/components/HelloWorldApp.jsx") do |contents|
assert_match("class HelloWorldApp extends", contents)
end
end

it "copies react files" do
assert_file("client/app/bundles/HelloWorld/startup/registration.jsx")
end

it "does not place react folders in root" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
%w(actions constants reducers store).each { |dir| assert_directory("client/app/bundles/HelloWorld/#{dir}") }
end

it "creates appropriate templates" do
assert_file("client/app/bundles/HelloWorld/startup/registration.jsx") do |contents|
assert_match("./HelloWorldApp", contents)
end
assert_file("client/app/bundles/HelloWorld/components/HelloWorld.jsx") do |contents|
assert_match("class HelloWorld extends", contents)
end
end

it "copies base redux files" do
%w(client/app/bundles/HelloWorld/actions/helloWorldActionCreators.jsx
client/app/bundles/HelloWorld/containers/HelloWorldContainer.jsx
client/app/bundles/HelloWorld/constants/helloWorldConstants.jsx
client/app/bundles/HelloWorld/reducers/helloWorldReducer.jsx
client/app/bundles/HelloWorld/store/helloWorldStore.jsx
client/app/bundles/HelloWorld/startup/HelloWorldApp.jsx
client/app/bundles/HelloWorld/startup/registration.jsx).each { |file| assert_file(file) }
client/app/bundles/HelloWorld/startup/HelloWorldApp.jsx).each { |file| assert_file(file) }
end
end

0 comments on commit 8fccc92

Please sign in to comment.