Class: ReactOnRails::Generators::ReactWithReduxGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorHelper
Defined in:
lib/generators/react_on_rails/react_with_redux_generator.rb

Instance Method Summary collapse

Methods included from GeneratorHelper

#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #destination_config_path, #detect_react_version, #empty_directory_with_keep_file, #gem_in_lockfile?, #keep_file, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file, #use_pro?, #use_rsc?, #using_rspack?, #using_swc?

Instance Method Details

#add_redux_npm_dependenciesObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 76

def add_redux_npm_dependencies
  # Add Redux dependencies as regular dependencies
  regular_packages = %w[redux react-redux]

  # Try using GeneratorHelper first (package manager agnostic)
  success = add_npm_dependencies(regular_packages)

  # Fallback to package manager detection if GeneratorHelper fails
  return if success

  package_manager = GeneratorMessages.detect_package_manager
  return unless package_manager

  install_packages_with_fallback(regular_packages, dev: false, package_manager: package_manager)
end

#add_redux_specific_messagesObject



92
93
94
95
96
97
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 92

def add_redux_specific_messages
  # Append Redux-specific post-install instructions
  GeneratorMessages.add_info(
    GeneratorMessages.helpful_message_after_installation(component_name: "HelloWorldApp", route: "hello_world")
  )
end

#copy_base_filesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 30

def copy_base_files
  base_js_path = "redux/base"
  ext = component_extension(options)

  # Copy Redux-connected component to auto-bundling structure
  copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.client.#{ext}",
            "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.#{ext}")
  copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/startup/HelloWorldApp.server.#{ext}",
            "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.server.#{ext}")
  copy_file("#{base_js_path}/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css",
            "app/javascript/src/HelloWorldApp/components/HelloWorld.module.css")

  # Update import paths in client component
  ror_client_file = "app/javascript/src/HelloWorldApp/ror_components/HelloWorldApp.client.#{ext}"
  gsub_file(ror_client_file, "../store/helloWorldStore", "../store/helloWorldStore")
  gsub_file(ror_client_file, "../containers/HelloWorldContainer",
            "../containers/HelloWorldContainer")
end

#copy_base_redux_filesObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 49

def copy_base_redux_files
  base_hello_world_path = "redux/base/app/javascript/bundles/HelloWorld"
  redux_extension = options.typescript? ? "ts" : "js"

  # Copy Redux infrastructure files with appropriate extension
  %W[actions/helloWorldActionCreators.#{redux_extension}
     containers/HelloWorldContainer.#{redux_extension}
     constants/helloWorldConstants.#{redux_extension}
     reducers/helloWorldReducer.#{redux_extension}
     store/helloWorldStore.#{redux_extension}
     components/HelloWorld.#{component_extension(options)}].each do |file|
       copy_file("#{base_hello_world_path}/#{file}",
                 "app/javascript/src/HelloWorldApp/#{file}")
     end
end

#create_appropriate_templatesObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 65

def create_appropriate_templates
  base_path = "base/base"
  config = {
    component_name: "HelloWorldApp"
  }

  # Only create the view template - no manual bundle needed for auto-bundling
  template("#{base_path}/app/views/hello_world/index.html.erb.tt",
           "app/views/hello_world/index.html.erb", config)
end

#create_redux_directoriesObject



21
22
23
24
25
26
27
28
# File 'lib/generators/react_on_rails/react_with_redux_generator.rb', line 21

def create_redux_directories
  # Create auto-bundling directory structure for Redux
  empty_directory("app/javascript/src/HelloWorldApp/ror_components")

  # Create Redux support directories within the component directory
  dirs = %w[actions constants containers reducers store components]
  dirs.each { |name| empty_directory("app/javascript/src/HelloWorldApp/#{name}") }
end