lib/lokalise_rails/utils.rb in lokalise_rails-7.0.0 vs lib/lokalise_rails/utils.rb in lokalise_rails-7.1.0
- old
+ new
@@ -4,19 +4,31 @@
module LokaliseRails
# Util methods
module Utils
class << self
- # Current project root
+ # 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.
+ #
+ # @return [Pathname] A Pathname object pointing to the root directory of the project.
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
- # Tries to get Rails root if Rails is defined
+ # 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.
+ #
+ # @return [String, nil] the path to the root directory if Rails is defined, or nil if it cannot be determined.
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
end
end
end