Skip to content

Commit

Permalink
Namespace references to Rails using ::Rails to avoid conflicts with… (
Browse files Browse the repository at this point in the history
#1421)

* Namespace references to Rails using `::Rails` to avoid conflicts with other gems

* Use uppercase JSONAPI module name
  • Loading branch information
lgebhardt authored Nov 16, 2023
1 parent 26d280a commit 1bdacf1
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/generators/jsonapi/controller_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Jsonapi
class ControllerGenerator < Rails::Generators::NamedBase
module JSONAPI
class ControllerGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/jsonapi/resource_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Jsonapi
class ResourceGenerator < Rails::Generators::NamedBase
class ResourceGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi-resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 @@ -9,7 +9,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 Down
4 changes: 2 additions & 2 deletions lib/jsonapi/active_relation_retrieval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,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 >= 6 || (Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
if ::Rails::VERSION::MAJOR >= 6 || (::Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
records.count(:all)
else
records.count
Expand Down Expand Up @@ -870,7 +870,7 @@ def apply_filter(records, filter, value, options = {})
end

def warn_about_unused_methods
if Rails.env.development?
if ::Rails.env.development?
if !caching? && implements_class_method?(:records_for_populate)
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/active_relation_retrieval_v09.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def join_relationship(records:, relationship:, resource_type: nil, join_type: :i
end

def warn_about_unused_methods
if Rails.env.development?
if ::Rails.env.development?
if !caching? && implements_class_method?(:records_for_populate)
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/active_relation_retrieval_v10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,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 Expand Up @@ -847,7 +847,7 @@ def apply_filter(records, filter, value, options = {})
end

def warn_about_unused_methods
if Rails.env.development?
if ::Rails.env.development?
if !caching? && implements_class_method?(:records_for_populate)
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
end
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 @@ -88,11 +88,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
2 changes: 1 addition & 1 deletion lib/jsonapi/resources/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module JSONAPI
module Resources
class Railtie < Rails::Railtie
class Railtie < ::Rails::Railtie
rake_tasks do
load 'tasks/check_upgrade.rake'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/check_upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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.included_modules.include?(JSONAPI::ResourceCommon)}

Expand Down
2 changes: 1 addition & 1 deletion test/lib/generators/jsonapi/controller_generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.expand_path('../../../../test_helper', __FILE__)
require 'generators/jsonapi/controller_generator'

module Jsonapi
module JSONAPI
class ControllerGeneratorTest < Rails::Generators::TestCase
tests ControllerGenerator
destination Rails.root.join('../controllers')
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

puts "Testing With RAILS VERSION #{Rails.version}"

class TestApp < Rails::Application
class TestApp < ::Rails::Application
config.eager_load = false
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'session'
Expand All @@ -61,7 +61,7 @@ class TestApp < Rails::Application
config.active_support.halt_callback_chains_on_return_false = false
config.active_record.time_zone_aware_types = [:time, :datetime]
config.active_record.belongs_to_required_by_default = false
if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
if ::Rails::VERSION::MAJOR == 5 && ::Rails::VERSION::MINOR == 2
config.active_record.sqlite3.represent_boolean_as_integer = true
end

Expand Down

0 comments on commit 1bdacf1

Please sign in to comment.