Class: ReactOnRails::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/react_on_rails/engine.rb

Class Method Summary collapse

Class Method Details

.package_json_missing?Boolean

Check if package.json doesn’t exist yet

Returns:

  • (Boolean)

    true if package.json is missing



78
79
80
# File 'lib/react_on_rails/engine.rb', line 78

def self.package_json_missing?
  !File.exist?(VersionChecker::NodePackageVersion.package_json_path)
end

.running_generator?Boolean

Check if we’re running a Rails generator

Returns:

  • (Boolean)

    true if running a generator



72
73
74
# File 'lib/react_on_rails/engine.rb', line 72

def self.running_generator?
  !ARGV.empty? && ARGV.first&.in?(%w[generate g])
end

.skip_version_validation?Boolean

Note:

Thread Safety: ENV variables are process-global. In practice, Rails generators run in a single process, so concurrent execution is not a concern. If running generators concurrently (e.g., in parallel tests), ensure tests run in separate processes to avoid ENV variable conflicts.

Note:

Manual ENV Setting: While this ENV variable is designed to be set by generators, users can manually set it (e.g., ‘REACT_ON_RAILS_SKIP_VALIDATION=true rails server`) to bypass validation. This should only be done temporarily during debugging or setup scenarios. The validation helps catch version mismatches early, so bypassing it in production is not recommended.

Determine if version validation should be skipped

This method checks multiple conditions to determine if package version validation should be skipped. Validation is skipped during setup scenarios where the npm package isn’t installed yet (e.g., during generator execution).

Examples:

Testing with parallel processes

# In RSpec configuration:
config.before(:each) do |example|
  ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION")
end

Returns:

  • (Boolean)

    true if validation should be skipped



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/react_on_rails/engine.rb', line 44

def self.skip_version_validation?
  # Skip if explicitly disabled via environment variable (set by generators)
  # Using ENV variable instead of ARGV because Rails can modify/clear ARGV during
  # initialization, making ARGV unreliable for detecting generator context. The ENV
  # variable persists through the entire Rails initialization process.
  if ENV["REACT_ON_RAILS_SKIP_VALIDATION"] == "true"
    Rails.logger.debug "[React on Rails] Skipping validation - disabled via environment variable"
    return true
  end

  # Check package.json first as it's cheaper and handles more cases
  if package_json_missing?
    Rails.logger.debug "[React on Rails] Skipping validation - package.json not found"
    return true
  end

  # Skip during generator runtime since packages are installed during execution
  # This is a fallback check in case ENV wasn't set, though ENV is the primary mechanism
  if running_generator?
    Rails.logger.debug "[React on Rails] Skipping validation during generator runtime"
    return true
  end

  false
end