Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Ruby 3.2.2 and Rails 7 undefined method `exists?' for File:Class (NoMethodError) #39

Open
fernandoiwamoto opened this issue May 9, 2024 · 1 comment

Comments

@fernandoiwamoto
Copy link

fernandoiwamoto commented May 9, 2024

/home/fernando/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/logging-rails-0.6.0/lib/logging/rails/railtie.rb:18:in block in <class:Railtie>': undefined method exists?' for File:Class (NoMethodError)

  load file if File.exists? file
                   ^^^^^^^^

Did you mean? exist?
from /home/fernando/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.0.8.1/lib/rails/initializable.rb:32:in `instance_exec'

I am using 0.6.0 of this gem + rails 7.0.8.1 and ruby 3.2.2

I think the exists is removed from ruby 3.0.0 / rails 7.

@fernandoiwamoto fernandoiwamoto changed the title For Ruby 3.2.2 undefined method `exists?' for File:Class (NoMethodError) For Ruby 3.2.2 and Rails 7 undefined method `exists?' for File:Class (NoMethodError) May 9, 2024
@lilfaf
Copy link

lilfaf commented May 29, 2024

Hi @fernandoiwamoto here is a workaround for this issue

lib/logging_railtie_patch.rb:

# frozen_string_literal: true

require 'logging/rails/railtie'

module LoggingRailtiePatch
  def self.override_initializer!
    Logging::Rails::Railtie.class_eval do
      # Remove the existing initializer
      initializers.delete_if { |initializer| initializer.name == 'logging.configure' }

      # Add the new initializer
      initializer 'logging.configure', before: 'initialize_logger' do |app|
        file = ::Rails.root.join('config/logging.rb')
        load file if File.exist? file
        ::Logging::Rails.configuration&.call(app.config)
      end
    end
  end
end

LoggingRailtiePatch.override_initializer!

config/application.rb:

# frozen_string_literal: true

require_relative 'boot'

require 'rails'
# ...

Bundler.require(*Rails.groups)

# Import patch after bundler and before app init
require File.expand_path('../lib/logging_railtie_patch', __dir__)

class Application < Rails::Application
  # ...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants