Skip to content

Commit

Permalink
fix: qualify Rails as ::Rails to avoid namespace clashes with other g…
Browse files Browse the repository at this point in the history
…ems, fixes cerebris#1413
  • Loading branch information
Mike Whittemore committed Oct 10, 2023
1 parent a3a2a7a commit c1cc2d5
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/jsonapi-resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require 'jsonapi/cached_response_fragment'
require 'jsonapi/response_document'
require 'jsonapi/acts_as_resource_controller'
if Rails::VERSION::MAJOR >= 6
if ::Rails::VERSION::MAJOR >= 6
ActiveSupport.on_load(:action_controller_base) do
require 'jsonapi/resource_controller'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module JoinLeftActiveRecordAdapter
# example Post.joins(:comments).joins_left(comments: :author) will join the comments table twice,
# once inner and once left in 5.2, but only as inner in earlier versions.
def joins_left(*columns)
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
if ::Rails::VERSION::MAJOR >= 6 || (::Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
left_joins(columns)
else
join_dependency = ActiveRecord::Associations::JoinDependency.new(self, columns, [])
Expand All @@ -23,4 +23,4 @@ def joins_left(*columns)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/jsonapi/active_relation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def apply_single_sort(records, field, direction, options)

# Assumes ActiveRecord's counting. Override if you need a different counting method
def count_records(records)
if (Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR >= 6
if (::Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || ::Rails::VERSION::MAJOR >= 6
records.count(:all)
else
records.count
Expand Down
8 changes: 4 additions & 4 deletions lib/jsonapi/acts_as_resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def resource_serializer_klass
end

def base_url
@base_url ||= "#{request.protocol}#{request.host_with_port}#{Rails.application.config.relative_url_root}"
@base_url ||= "#{request.protocol}#{request.host_with_port}#{::Rails.application.config.relative_url_root}"
end

def resource_klass_name
Expand Down Expand Up @@ -286,7 +286,7 @@ def handle_exceptions(e)
request.env['action_dispatch.exception'] ||= e

internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
::Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
errors = internal_server_error.errors
end
end
Expand All @@ -298,7 +298,7 @@ def safe_run_callback(callback, error)
begin
callback.call(error)
rescue => e
Rails.logger.error { "Error in error handling callback: #{e.message} #{e.backtrace.join("\n")}" }
::Rails.logger.error { "Error in error handling callback: #{e.message} #{e.backtrace.join("\n")}" }
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
return JSONAPI::ErrorsOperationResult.new(internal_server_error.errors[0].code, internal_server_error.errors)
end
Expand All @@ -324,7 +324,7 @@ def on_server_error(*args, &callback_block)
if self.respond_to? method
send(method, error)
else
Rails.logger.warn("#{method} not defined on #{self}, skipping error callback")
::Rails.logger.warn("#{method} not defined on #{self}, skipping error callback")
end
end
end.compact
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def initialize

# Whether or not to include exception backtraces in JSONAPI error
# responses. Defaults to `false` in anything other than development or test.
self.include_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)
self.include_backtraces_in_errors = (::Rails.env.development? || ::Rails.env.test?)

# Whether or not to include exception application backtraces in JSONAPI error
# responses. Defaults to `false` in anything other than development or test.
self.include_application_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)
self.include_application_backtraces_in_errors = (::Rails.env.development? || ::Rails.env.test?)

# List of classes that should not be rescued by the operations processor.
# For example, if you use Pundit for authorization, you might
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def errors
if JSONAPI.configuration.include_application_backtraces_in_errors
meta ||= Hash.new
meta[:exception] ||= exception.message
meta[:application_backtrace] = exception.backtrace.select{|line| line =~ /#{Rails.root}/}
meta[:application_backtrace] = exception.backtrace.select{|line| line =~ /#{::Rails.root}/}
end

[create_error_object(code: JSONAPI::INTERNAL_SERVER_ERROR,
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/resources/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module JSONAPI
module Resources
class Railtie < Rails::Railtie
class Railtie < ::Rails::Railtie
rake_tasks do
load 'tasks/check_upgrade.rake'
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tasks/check_upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace :jsonapi do
namespace :resources do
desc 'Checks application for orphaned overrides'
task :check_upgrade => :environment do
Rails.application.eager_load!
::Rails.application.eager_load!

resource_klasses = ObjectSpace.each_object(Class).select { |klass| klass < JSONAPI::Resource}

Expand Down

0 comments on commit c1cc2d5

Please sign in to comment.