Skip to content

Commit bb1e45d

Browse files
committed
fix redux generator for auto-registration
- Implement auto-registration structure for Redux (HelloWorldApp component) - Fix component naming consistency (HelloWorld → HelloWorldApp for Redux) - Update directory structure to src/HelloWorldApp/ror_components/ - Fix import paths for new auto-registration structure - Remove manual bundle creation (eliminate hello-world-bundle.js) - Add cleanup of conflicting base generator files this is because before it, manual registration and non redux HelloWorld component were created even when specifying --redux
1 parent e596538 commit bb1e45d

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

lib/generators/react_on_rails/generator_messages.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ def clear
3838
@output = []
3939
end
4040

41-
def helpful_message_after_installation
41+
def helpful_message_after_installation(component_name: "HelloWorld")
4242
<<~MSG
4343
4444
What to do next:
4545
4646
- See the documentation on https://github.com/shakacode/shakapacker#webpack-configuration
4747
for how to customize the default webpack configuration.
4848
49-
- Include your webpack assets to your application layout.
49+
- No manual pack tags needed with auto-registration! Your layout should use empty pack tags:
5050
51-
<%= javascript_pack_tag 'hello-world-bundle' %>
51+
<%= javascript_pack_tag %>
52+
<%= stylesheet_pack_tag %>
5253
5354
- To start Rails server run:
5455
@@ -61,7 +62,7 @@ def helpful_message_after_installation
6162
- To server render, change this line app/views/hello_world/index.html.erb to
6263
`prerender: true` to see server rendering (right click on page and select "view source").
6364
64-
<%= react_component("HelloWorld", props: @hello_world_props, prerender: true) %>
65+
<%= react_component("#{component_name}", props: @hello_world_props, prerender: true) %>
6566
6667
Alternative steps to run the app:
6768

lib/generators/react_on_rails/react_with_redux_generator.rb

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@ class ReactWithReduxGenerator < Rails::Generators::Base
99
source_root(File.expand_path("templates", __dir__))
1010

1111
def create_redux_directories
12-
dirs = %w[actions constants containers reducers store startup]
13-
dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") }
12+
# Create auto-registration directory structure for Redux
13+
empty_directory("app/javascript/src/HelloWorldApp/ror_components")
14+
15+
# Create Redux support directories within the component directory
16+
dirs = %w[actions constants containers reducers store]
17+
dirs.each { |name| empty_directory("app/javascript/src/HelloWorldApp/#{name}") }
1418
end
1519

1620
def copy_base_files
1721
base_js_path = "redux/base"
18-
base_files = %w[app/javascript/bundles/HelloWorld/components/HelloWorld.jsx
19-
app/javascript/bundles/HelloWorld/components/HelloWorld.module.css]
20-
base_files.each { |file| copy_file("#{base_js_path}/#{file}", file) }
21-
22-
# Also copy Redux-connected component to auto-registration structure
22+
23+
# Copy Redux-connected component to auto-registration structure
2324
copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.client.jsx",
24-
"app/javascript/src/HelloWorld/ror_components/HelloWorld.client.jsx")
25+
"app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.jsx")
2526
copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.server.jsx",
26-
"app/javascript/src/HelloWorld/ror_components/HelloWorld.server.jsx")
27+
"app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.server.jsx")
2728
copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css",
28-
"app/javascript/src/HelloWorld/HelloWorld.module.css")
29+
"app/javascript/src/HelloWorldApp/HelloWorldApp.module.css")
2930

3031
# Update import paths in client component
31-
ror_client_file = "app/javascript/src/HelloWorld/ror_components/HelloWorld.client.jsx"
32-
gsub_file(ror_client_file, "../store/helloWorldStore", "../../../bundles/HelloWorld/store/helloWorldStore")
32+
ror_client_file = "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.jsx"
33+
gsub_file(ror_client_file, "../store/helloWorldStore", "../store/helloWorldStore")
3334
gsub_file(ror_client_file, "../containers/HelloWorldContainer",
34-
"../../../bundles/HelloWorld/containers/HelloWorldContainer")
35+
"../containers/HelloWorldContainer")
3536
end
3637

3738
def copy_base_redux_files
@@ -40,29 +41,42 @@ def copy_base_redux_files
4041
containers/HelloWorldContainer.js
4142
constants/helloWorldConstants.js
4243
reducers/helloWorldReducer.js
43-
store/helloWorldStore.js
44-
startup/HelloWorldApp.client.jsx
45-
startup/HelloWorldApp.server.jsx].each do |file|
44+
store/helloWorldStore.js].each do |file|
4645
copy_file("#{base_hello_world_path}/#{file}",
47-
"app/javascript/bundles/HelloWorld/#{file}")
46+
"app/javascript/src/HelloWorldApp/#{file}")
4847
end
4948
end
5049

5150
def create_appropriate_templates
5251
base_path = "base/base"
53-
base_js_path = "#{base_path}/app/javascript"
5452
config = {
55-
component_name: "HelloWorld",
56-
app_relative_path: "../bundles/HelloWorld/startup/HelloWorldApp.client"
53+
component_name: "HelloWorldApp"
5754
}
5855

59-
template("#{base_js_path}/packs/registration.js.tt", "app/javascript/packs/hello-world-bundle.js", config)
60-
template("#{base_path}/app/views/hello_world/index.html.erb.tt", "app/views/hello_world/index.html.erb", config)
56+
# Only create the view template - no manual bundle needed for auto registration
57+
template("#{base_path}/app/views/hello_world/index.html.erb.tt",
58+
"app/views/hello_world/index.html.erb", config)
6159
end
6260

6361
def add_redux_yarn_dependencies
6462
run "yarn add redux react-redux"
6563
end
64+
65+
def cleanup_base_generator_files
66+
# Remove files created by base generator since Redux uses different structure
67+
remove_file "app/javascript/src/HelloWorld/HelloWorld.module.css"
68+
remove_dir "app/javascript/src/HelloWorld/ror_components" if Dir.exist?("app/javascript/src/HelloWorld/ror_components") && Dir.empty?("app/javascript/src/HelloWorld/ror_components")
69+
remove_dir "app/javascript/src/HelloWorld" if Dir.exist?("app/javascript/src/HelloWorld") && Dir.entries("app/javascript/src/HelloWorld").size <= 2 # only . and ..
70+
end
71+
72+
def add_redux_specific_messages
73+
# Override the generic messages with Redux-specific instructions
74+
require_relative "generator_messages"
75+
GeneratorMessages.output.clear
76+
GeneratorMessages.add_info(
77+
GeneratorMessages.helpful_message_after_installation(component_name: "HelloWorldApp")
78+
)
79+
end
6680
end
6781
end
6882
end

0 commit comments

Comments
 (0)