Skip to content

Commit 1bdacf1

Browse files
authored
Namespace references to Rails using ::Rails to avoid conflicts with… (#1421)
* Namespace references to Rails using `::Rails` to avoid conflicts with other gems * Use uppercase JSONAPI module name
1 parent 26d280a commit 1bdacf1

14 files changed

+22
-22
lines changed

lib/generators/jsonapi/controller_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

3-
module Jsonapi
4-
class ControllerGenerator < Rails::Generators::NamedBase
3+
module JSONAPI
4+
class ControllerGenerator < ::Rails::Generators::NamedBase
55
source_root File.expand_path('../templates', __FILE__)
66

77
def create_resource

lib/generators/jsonapi/resource_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module Jsonapi
4-
class ResourceGenerator < Rails::Generators::NamedBase
4+
class ResourceGenerator < ::Rails::Generators::NamedBase
55
source_root File.expand_path('../templates', __FILE__)
66

77
def create_resource

lib/jsonapi-resources.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
require 'jsonapi/cached_response_fragment'
1313
require 'jsonapi/response_document'
1414
require 'jsonapi/acts_as_resource_controller'
15-
if Rails::VERSION::MAJOR >= 6
15+
if ::Rails::VERSION::MAJOR >= 6
1616
ActiveSupport.on_load(:action_controller_base) do
1717
require 'jsonapi/resource_controller'
1818
end

lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module JoinLeftActiveRecordAdapter
99
# example Post.joins(:comments).joins_left(comments: :author) will join the comments table twice,
1010
# once inner and once left in 5.2, but only as inner in earlier versions.
1111
def joins_left(*columns)
12-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
12+
if ::Rails::VERSION::MAJOR >= 6 || (::Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
1313
left_joins(columns)
1414
else
1515
join_dependency = ActiveRecord::Associations::JoinDependency.new(self, columns, [])

lib/jsonapi/active_relation_retrieval.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def apply_single_sort(records, field, direction, options)
722722

723723
# Assumes ActiveRecord's counting. Override if you need a different counting method
724724
def count_records(records)
725-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
725+
if ::Rails::VERSION::MAJOR >= 6 || (::Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
726726
records.count(:all)
727727
else
728728
records.count
@@ -870,7 +870,7 @@ def apply_filter(records, filter, value, options = {})
870870
end
871871

872872
def warn_about_unused_methods
873-
if Rails.env.development?
873+
if ::Rails.env.development?
874874
if !caching? && implements_class_method?(:records_for_populate)
875875
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
876876
end

lib/jsonapi/active_relation_retrieval_v09.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def join_relationship(records:, relationship:, resource_type: nil, join_type: :i
700700
end
701701

702702
def warn_about_unused_methods
703-
if Rails.env.development?
703+
if ::Rails.env.development?
704704
if !caching? && implements_class_method?(:records_for_populate)
705705
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
706706
end

lib/jsonapi/active_relation_retrieval_v10.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def apply_single_sort(records, field, direction, options)
703703

704704
# Assumes ActiveRecord's counting. Override if you need a different counting method
705705
def count_records(records)
706-
if (Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR >= 6
706+
if (::Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || ::Rails::VERSION::MAJOR >= 6
707707
records.count(:all)
708708
else
709709
records.count
@@ -847,7 +847,7 @@ def apply_filter(records, filter, value, options = {})
847847
end
848848

849849
def warn_about_unused_methods
850-
if Rails.env.development?
850+
if ::Rails.env.development?
851851
if !caching? && implements_class_method?(:records_for_populate)
852852
warn "#{self}: The `records_for_populate` method is not used when caching is disabled."
853853
end

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def resource_serializer_klass
160160
end
161161

162162
def base_url
163-
@base_url ||= "#{request.protocol}#{request.host_with_port}#{Rails.application.config.relative_url_root}"
163+
@base_url ||= "#{request.protocol}#{request.host_with_port}#{::Rails.application.config.relative_url_root}"
164164
end
165165

166166
def resource_klass_name
@@ -286,7 +286,7 @@ def handle_exceptions(e)
286286
request.env['action_dispatch.exception'] ||= e
287287

288288
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
289-
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
289+
::Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
290290
errors = internal_server_error.errors
291291
end
292292
end
@@ -298,7 +298,7 @@ def safe_run_callback(callback, error)
298298
begin
299299
callback.call(error)
300300
rescue => e
301-
Rails.logger.error { "Error in error handling callback: #{e.message} #{e.backtrace.join("\n")}" }
301+
::Rails.logger.error { "Error in error handling callback: #{e.message} #{e.backtrace.join("\n")}" }
302302
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
303303
return JSONAPI::ErrorsOperationResult.new(internal_server_error.errors[0].code, internal_server_error.errors)
304304
end
@@ -324,7 +324,7 @@ def on_server_error(*args, &callback_block)
324324
if self.respond_to? method
325325
send(method, error)
326326
else
327-
Rails.logger.warn("#{method} not defined on #{self}, skipping error callback")
327+
::Rails.logger.warn("#{method} not defined on #{self}, skipping error callback")
328328
end
329329
end
330330
end.compact

lib/jsonapi/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ def initialize
8888

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

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

9797
# List of classes that should not be rescued by the operations processor.
9898
# For example, if you use Pundit for authorization, you might

lib/jsonapi/exceptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def errors
5454
if JSONAPI.configuration.include_application_backtraces_in_errors
5555
meta ||= Hash.new
5656
meta[:exception] ||= exception.message
57-
meta[:application_backtrace] = exception.backtrace.select{|line| line =~ /#{Rails.root}/}
57+
meta[:application_backtrace] = exception.backtrace.select{|line| line =~ /#{::Rails.root}/}
5858
end
5959

6060
[create_error_object(code: JSONAPI::INTERNAL_SERVER_ERROR,

lib/jsonapi/resources/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module JSONAPI
44
module Resources
5-
class Railtie < Rails::Railtie
5+
class Railtie < ::Rails::Railtie
66
rake_tasks do
77
load 'tasks/check_upgrade.rake'
88
end

lib/tasks/check_upgrade.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace :jsonapi do
77
namespace :resources do
88
desc 'Checks application for orphaned overrides'
99
task :check_upgrade => :environment do
10-
Rails.application.eager_load!
10+
::Rails.application.eager_load!
1111

1212
resource_klasses = ObjectSpace.each_object(Class).select { |klass| klass.included_modules.include?(JSONAPI::ResourceCommon)}
1313

test/lib/generators/jsonapi/controller_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.expand_path('../../../../test_helper', __FILE__)
22
require 'generators/jsonapi/controller_generator'
33

4-
module Jsonapi
4+
module JSONAPI
55
class ControllerGeneratorTest < Rails::Generators::TestCase
66
tests ControllerGenerator
77
destination Rails.root.join('../controllers')

test/test_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

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

48-
class TestApp < Rails::Application
48+
class TestApp < ::Rails::Application
4949
config.eager_load = false
5050
config.root = File.dirname(__FILE__)
5151
config.session_store :cookie_store, key: 'session'
@@ -61,7 +61,7 @@ class TestApp < Rails::Application
6161
config.active_support.halt_callback_chains_on_return_false = false
6262
config.active_record.time_zone_aware_types = [:time, :datetime]
6363
config.active_record.belongs_to_required_by_default = false
64-
if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
64+
if ::Rails::VERSION::MAJOR == 5 && ::Rails::VERSION::MINOR == 2
6565
config.active_record.sqlite3.represent_boolean_as_integer = true
6666
end
6767

0 commit comments

Comments
 (0)