class ReactOnRails::Generators::BaseGenerator
Constants
- CONFIGURE_RSPEC_TO_COMPILE_ASSETS
Public Instance Methods
add_base_gems_to_gemfile()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 75 def add_base_gems_to_gemfile run "bundle" end
add_hello_world_route()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 20 def add_hello_world_route route "get 'hello_world', to: 'hello_world#index'" end
add_js_dependencies()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 79 def add_js_dependencies major_minor_patch_only = /\A\d+\.\d+\.\d+\z/ if ReactOnRails::VERSION.match?(major_minor_patch_only) package_json.manager.add(["react-on-rails@#{ReactOnRails::VERSION}"]) else # otherwise add latest puts "Adding the latest react-on-rails NPM module. Double check this is correct in package.json" package_json.manager.add(["react-on-rails"]) end puts "Adding React dependencies" package_json.manager.add([ "react", "react-dom", "@babel/preset-react", "prop-types", "babel-plugin-transform-react-remove-prop-types", "babel-plugin-macros" ]) puts "Adding CSS handlers" package_json.manager.add(%w[ css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader ]) puts "Adding dev dependencies" package_json.manager.add([ "@pmmmwh/react-refresh-webpack-plugin", "react-refresh" ], type: :dev) end
append_to_spec_rails_helper()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 115 def append_to_spec_rails_helper rails_helper = File.join(destination_root, "spec/rails_helper.rb") if File.exist?(rails_helper) add_configure_rspec_to_compile_assets(rails_helper) else spec_helper = File.join(destination_root, "spec/spec_helper.rb") if File.exist?(spec_helper) add_configure_rspec_to_compile_assets(spec_helper) else # rubocop:disable Layout/EmptyLinesAroundArguments GeneratorMessages.add_info( <<-MSG.strip_heredoc We did not find a spec/rails_helper.rb or spec/spec_helper.rb to add the React on Rails Test helper, which ensures that if we are running js tests, then we are using latest webpack assets. You can later add this to your rspec config: # This will use the defaults of :js and :server_rendering meta tags ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) MSG ) # rubocop:enable Layout/EmptyLinesAroundArguments end end end
copy_base_files()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 29 def copy_base_files base_path = "base/base/" base_files = %w[app/controllers/hello_world_controller.rb app/views/layouts/hello_world.html.erb] base_templates = %w[config/initializers/react_on_rails.rb Procfile.dev Procfile.dev-static] base_files.each { |file| copy_file("#{base_path}#{file}", file) } base_templates.each do |file| template("#{base_path}/#{file}.tt", file, { packer_type: ReactOnRails::PackerUtils.packer_type }) end end
copy_js_bundle_files()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 42 def copy_js_bundle_files base_path = "base/base/" base_files = %w[app/javascript/packs/server-bundle.js app/javascript/bundles/HelloWorld/components/HelloWorldServer.js app/javascript/bundles/HelloWorld/components/HelloWorld.module.css] base_files.each { |file| copy_file("#{base_path}#{file}", file) } end
copy_packer_config()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 68 def copy_packer_config puts "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config" base_path = "base/base/" config = "config/shakapacker.yml" copy_file("#{base_path}#{config}", config) end
copy_webpack_config()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 50 def copy_webpack_config puts "Adding Webpack config" base_path = "base/base" base_files = %w[babel.config.js config/webpack/clientWebpackConfig.js config/webpack/commonWebpackConfig.js config/webpack/test.js config/webpack/development.js config/webpack/production.js config/webpack/serverWebpackConfig.js config/webpack/webpack.config.js config/webpack/webpackConfig.js] config = { message: "// The source code including full typescript support is available at:" } base_files.each { |file| template("#{base_path}/#{file}.tt", file, config) } end
create_react_directories()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 24 def create_react_directories dirs = %w[components] dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") } end
Private Instance Methods
add_configure_rspec_to_compile_assets(helper_file)
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 177 def add_configure_rspec_to_compile_assets(helper_file) search_str = "RSpec.configure do |config|" gsub_file(helper_file, search_str, CONFIGURE_RSPEC_TO_COMPILE_ASSETS) end
app_const()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 173 def app_const @app_const ||= "#{app_const_base}::Application" end
app_const_base()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 169 def app_const_base @app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, "_").squeeze("_").camelize end
app_name()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 153 def app_name @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root)) .tr("\\", "").tr(". ", "_") end
defined_app_const_base()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 162 def defined_app_const_base Rails.respond_to?(:application) && defined?(Rails::Application) && Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "") end
Also aliased as: defined_app_const_base?
defined_app_name()
click to toggle source
# File lib/generators/react_on_rails/base_generator.rb, line 158 def defined_app_name defined_app_const_base.underscore end