Module: LokaliseRails::Utils

Defined in:
lib/lokalise_rails/utils.rb

Overview

Util methods

Class Method Summary collapse

Class Method Details

.rails_rootString?

Attempts to determine the root directory of a Rails project by checking the presence of Rails and its root method. If Rails is older and does not have the root method, it falls back to the RAILS_ROOT constant if defined.

Returns:

  • (String, nil)

    the path to the root directory if Rails is defined, or nil if it cannot be determined.



24
25
26
27
28
29
30
31
32
# File 'lib/lokalise_rails/utils.rb', line 24

def rails_root
  # First, check if Rails.root is defined and return its path if available.
  return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
  # Fallback to the RAILS_ROOT constant from older Rails versions.
  return RAILS_ROOT.to_s if defined?(RAILS_ROOT)

  # Returns nil if none of the above are defined, indicating Rails is not present.
  nil
end

.rootPathname

Retrieves the root directory of the current project. It attempts to find the Rails root directory if Rails is loaded. If Rails is not present, it defaults to the current working directory of the process.

Returns:

  • (Pathname)

    A Pathname object pointing to the root directory of the project.



14
15
16
17
# File 'lib/lokalise_rails/utils.rb', line 14

def root
  # Uses Pathname to create a robust path object from the rails_root or the current working directory.
  Pathname.new(rails_root || Dir.getwd)
end