Sha256: 03557f095619dad1bfc0060e8879f748ddca41f33b2ae8de1b1fd07002898e25

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'pathname'

module LokaliseRails
  # Util methods
  module Utils
    class << self
      # 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

      # 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lokalise_rails-8.0.0 lib/lokalise_rails/utils.rb
lokalise_rails-7.1.0 lib/lokalise_rails/utils.rb