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

Improve logic when Rails class/module is defined but Rails (rails gem) is not present #1256

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,10 @@ Airbrake Changelog

### master

### [][] ()

* Added Def class to improve on recognising Rails gem presence vs Rails class.

### [v13.0.3][v13.0.3] (September 20, 2022)

* Fixed `tie_rails_4_or_below_with_active_record': uninitialized constant
Expand Down
3 changes: 2 additions & 1 deletion lib/airbrake.rb
Expand Up @@ -8,12 +8,13 @@
require 'airbrake-ruby'

require 'airbrake/version'
require 'airbrake/def'

# Automatically load needed files for the environment the library is running in.
if defined?(Rack)
require 'airbrake/rack'

require 'airbrake/rails' if defined?(Rails)
require 'airbrake/rails' if Airbrake::Def.rails?
end

require 'airbrake/rake' if defined?(Rake::Task)
Expand Down
7 changes: 7 additions & 0 deletions lib/airbrake/def.rb
@@ -0,0 +1,7 @@
module Airbrake
class Def
def self.rails?
defined?(Rails) && defined?(Rails.version)
end
end
end
2 changes: 1 addition & 1 deletion lib/airbrake/rack/route_filter.rb
Expand Up @@ -17,7 +17,7 @@ def call(notice)
return unless (request = notice.stash[:rack_request])

notice[:context][:route] =
if action_dispatch_request?(request)
if action_dispatch_request?(request) && Def.rails?
rails_route(request)
elsif sinatra_request?(request)
sinatra_route(request)
Expand Down
2 changes: 1 addition & 1 deletion lib/airbrake/rails/backtrace_cleaner.rb
Expand Up @@ -14,7 +14,7 @@ def self.clean(backtrace)
end
end

if defined?(Rails)
if Airbrake::Def.rails?
# Silence own frames to let the cleaner proceed to the next line (and probably
# find the correct call-site coming from the app code rather this library).
Rails.backtrace_cleaner.add_silencer do |line|
Expand Down
4 changes: 2 additions & 2 deletions lib/airbrake/rake/tasks.rb
Expand Up @@ -4,7 +4,7 @@

namespace :airbrake do
desc 'Verify your gem installation by sending a test exception'
task test: (:environment if defined?(Rails)) do
task test: (:environment if Airbrake::Def.rails?) do
raise Airbrake::Error, 'airbrake-ruby is not configured' unless Airbrake.configured?

require 'pp'
Expand Down Expand Up @@ -38,7 +38,7 @@

desc 'Notify Airbrake of a new deploy'
task :deploy do
if defined?(Rails)
if Airbrake::Def.rails?
initializer = Rails.root.join('config', 'initializers', 'airbrake.rb')

# Avoid loading the environment to speed up the deploy task and try guess
Expand Down