From 1bdacf1e9d9f1e4c2c56843815c65004b692aa2c Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Thu, 16 Nov 2023 09:30:04 -0500 Subject: [PATCH] =?UTF-8?q?Namespace=20references=20to=20Rails=20using=20`?= =?UTF-8?q?::Rails`=20to=20avoid=20conflicts=20with=E2=80=A6=20(#1421)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Namespace references to Rails using `::Rails` to avoid conflicts with other gems * Use uppercase JSONAPI module name --- lib/generators/jsonapi/controller_generator.rb | 4 ++-- lib/generators/jsonapi/resource_generator.rb | 2 +- lib/jsonapi-resources.rb | 2 +- .../adapters/join_left_active_record_adapter.rb | 2 +- lib/jsonapi/active_relation_retrieval.rb | 4 ++-- lib/jsonapi/active_relation_retrieval_v09.rb | 2 +- lib/jsonapi/active_relation_retrieval_v10.rb | 4 ++-- lib/jsonapi/acts_as_resource_controller.rb | 8 ++++---- lib/jsonapi/configuration.rb | 4 ++-- lib/jsonapi/exceptions.rb | 2 +- lib/jsonapi/resources/railtie.rb | 2 +- lib/tasks/check_upgrade.rake | 2 +- test/lib/generators/jsonapi/controller_generator_test.rb | 2 +- test/test_helper.rb | 4 ++-- 14 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/generators/jsonapi/controller_generator.rb b/lib/generators/jsonapi/controller_generator.rb index 3ebdece11..5747ab2eb 100644 --- a/lib/generators/jsonapi/controller_generator.rb +++ b/lib/generators/jsonapi/controller_generator.rb @@ -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 diff --git a/lib/generators/jsonapi/resource_generator.rb b/lib/generators/jsonapi/resource_generator.rb index 34957d1e2..847f7e0ba 100644 --- a/lib/generators/jsonapi/resource_generator.rb +++ b/lib/generators/jsonapi/resource_generator.rb @@ -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 diff --git a/lib/jsonapi-resources.rb b/lib/jsonapi-resources.rb index bf3799685..5d46099ab 100644 --- a/lib/jsonapi-resources.rb +++ b/lib/jsonapi-resources.rb @@ -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 diff --git a/lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb b/lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb index 2bac4569c..2d5702575 100644 --- a/lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb +++ b/lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb @@ -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, []) diff --git a/lib/jsonapi/active_relation_retrieval.rb b/lib/jsonapi/active_relation_retrieval.rb index 1942d85b3..1e1a72fb2 100644 --- a/lib/jsonapi/active_relation_retrieval.rb +++ b/lib/jsonapi/active_relation_retrieval.rb @@ -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 @@ -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 diff --git a/lib/jsonapi/active_relation_retrieval_v09.rb b/lib/jsonapi/active_relation_retrieval_v09.rb index f3d9ecf3d..0b9b38cd7 100644 --- a/lib/jsonapi/active_relation_retrieval_v09.rb +++ b/lib/jsonapi/active_relation_retrieval_v09.rb @@ -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 diff --git a/lib/jsonapi/active_relation_retrieval_v10.rb b/lib/jsonapi/active_relation_retrieval_v10.rb index 9464af768..bf8466f80 100644 --- a/lib/jsonapi/active_relation_retrieval_v10.rb +++ b/lib/jsonapi/active_relation_retrieval_v10.rb @@ -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 @@ -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 diff --git a/lib/jsonapi/acts_as_resource_controller.rb b/lib/jsonapi/acts_as_resource_controller.rb index e448fa0ea..86b39e6ca 100644 --- a/lib/jsonapi/acts_as_resource_controller.rb +++ b/lib/jsonapi/acts_as_resource_controller.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/jsonapi/configuration.rb b/lib/jsonapi/configuration.rb index 6d6c7850b..1d2c235e6 100644 --- a/lib/jsonapi/configuration.rb +++ b/lib/jsonapi/configuration.rb @@ -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 diff --git a/lib/jsonapi/exceptions.rb b/lib/jsonapi/exceptions.rb index e917118cf..6fb8b675f 100644 --- a/lib/jsonapi/exceptions.rb +++ b/lib/jsonapi/exceptions.rb @@ -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, diff --git a/lib/jsonapi/resources/railtie.rb b/lib/jsonapi/resources/railtie.rb index f94edfa2a..8e72eb01d 100644 --- a/lib/jsonapi/resources/railtie.rb +++ b/lib/jsonapi/resources/railtie.rb @@ -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 diff --git a/lib/tasks/check_upgrade.rake b/lib/tasks/check_upgrade.rake index 41cb8e0f9..89e0536a0 100644 --- a/lib/tasks/check_upgrade.rake +++ b/lib/tasks/check_upgrade.rake @@ -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)} diff --git a/test/lib/generators/jsonapi/controller_generator_test.rb b/test/lib/generators/jsonapi/controller_generator_test.rb index faed0637c..5a54d2fbd 100644 --- a/test/lib/generators/jsonapi/controller_generator_test.rb +++ b/test/lib/generators/jsonapi/controller_generator_test.rb @@ -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') diff --git a/test/test_helper.rb b/test/test_helper.rb index 60c2fbed9..834c85771 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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' @@ -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