From 97fbd785f46f7759416e9251d4a4042ee92e0cc3 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Tue, 14 Sep 2021 22:38:00 +0800 Subject: [PATCH] [#151] removed reloader and console overwrite of reload method --- .rubocop_todo.yml | 6 ------ lib/apartment/console.rb | 16 ---------------- lib/apartment/railtie.rb | 19 ------------------- lib/apartment/reloader.rb | 22 ---------------------- spec/unit/reloader_spec.rb | 24 ------------------------ 5 files changed, 87 deletions(-) delete mode 100644 lib/apartment/reloader.rb delete mode 100644 spec/unit/reloader_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3650e125..ce2bf148 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,7 +77,6 @@ RSpec/DescribedClass: - 'spec/tenant_spec.rb' - 'spec/unit/elevators/host_hash_spec.rb' - 'spec/unit/migrator_spec.rb' - - 'spec/unit/reloader_spec.rb' # Offense count: 5 # Cop supports --auto-correct. @@ -128,7 +127,6 @@ RSpec/FilePath: - 'spec/unit/elevators/host_spec.rb' - 'spec/unit/elevators/subdomain_spec.rb' - 'spec/unit/migrator_spec.rb' - - 'spec/unit/reloader_spec.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -159,7 +157,6 @@ RSpec/InstanceVariable: # Cop supports --auto-correct. RSpec/LeadingSubject: Exclude: - - 'spec/unit/reloader_spec.rb' # Offense count: 2 RSpec/LeakyConstantDeclaration: @@ -196,7 +193,6 @@ RSpec/NamedSubject: - 'spec/support/contexts.rb' - 'spec/support/requirements.rb' - 'spec/tenant_spec.rb' - - 'spec/unit/reloader_spec.rb' # Offense count: 24 RSpec/NestedGroups: @@ -217,7 +213,6 @@ RSpec/VerifiedDoubles: Exclude: - 'spec/integration/apartment_rake_integration_spec.rb' - 'spec/unit/elevators/first_subdomain_spec.rb' - - 'spec/unit/reloader_spec.rb' # Offense count: 17 Style/Documentation: @@ -233,7 +228,6 @@ Style/Documentation: - 'lib/apartment/migrator.rb' - 'lib/apartment/model.rb' - 'lib/apartment/railtie.rb' - - 'lib/apartment/reloader.rb' - 'lib/apartment/tasks/enhancements.rb' - 'lib/apartment/tasks/task_helper.rb' - 'lib/generators/apartment/install/install_generator.rb' diff --git a/lib/apartment/console.rb b/lib/apartment/console.rb index 91721222..6cc3900d 100644 --- a/lib/apartment/console.rb +++ b/lib/apartment/console.rb @@ -1,21 +1,5 @@ # frozen_string_literal: true -# A workaround to get `reload!` to also call Apartment::Tenant.init -# This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded - -# reloads the environment -# rubocop:disable Style/OptionalBooleanParameter -def reload!(print = true) - puts 'Reloading...' if print - - # This triggers the to_prepare callbacks - ActionDispatch::Callbacks.new(proc {}).call({}) - # Manually init Apartment again once classes are reloaded - Apartment::Tenant.init - true -end -# rubocop:enable Style/OptionalBooleanParameter - def st(schema_name = nil) if schema_name.nil? tenant_list.each { |t| puts t } diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index ba4e5728..efbe9c48 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -2,7 +2,6 @@ require 'rails' require 'apartment/tenant' -require 'apartment/reloader' module Apartment class Railtie < Rails::Railtie @@ -60,23 +59,5 @@ class Railtie < Rails::Railtie load 'tasks/apartment.rake' require 'apartment/tasks/enhancements' if Apartment.db_migrate_tenants end - - # - # The following initializers are a workaround to the fact that I can't properly hook into the rails reloader - # Note this is technically valid for any environment where cache_classes is false, for us, it's just development - # - if Rails.env.development? - - # Apartment::Reloader is middleware to initialize things properly on each request to dev - initializer 'apartment.init' do |app| - app.config.middleware.use Apartment::Reloader - end - - # Overrides reload! to also call Apartment::Tenant.init as well - # so that the reloaded classes have the proper table_names - console do - require 'apartment/console' - end - end end end diff --git a/lib/apartment/reloader.rb b/lib/apartment/reloader.rb deleted file mode 100644 index cd8b6861..00000000 --- a/lib/apartment/reloader.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -module Apartment - class Reloader - # Middleware used in development to init Apartment for each request - # Necessary due to code reload (annoying). When models are reloaded, they no longer have the proper table_name - # That is prepended with the schema (if using postgresql schemas) - # I couldn't figure out how to properly hook into the Rails reload process *after* files are reloaded - # so I've used this in the meantime. - # - # Also see apartment/console for the re-definition of reload! that re-init's Apartment - # - def initialize(app) - @app = app - end - - def call(env) - Tenant.init - @app.call(env) - end - end -end diff --git a/spec/unit/reloader_spec.rb b/spec/unit/reloader_spec.rb deleted file mode 100644 index b194a46c..00000000 --- a/spec/unit/reloader_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Apartment::Reloader do - context 'when using postgresql schemas' do - before do - Apartment.configure do |config| - config.excluded_models = ['Company'] - config.use_schemas = true - end - Apartment::Tenant.reload!(config) - Company.reset_table_name # ensure we're clean - end - - subject { Apartment::Reloader.new(double('Rack::Application', call: nil)) } - - it 'initializes apartment when called' do - expect(Company.table_name).not_to include('public.') - subject.call(double('env')) - expect(Company.table_name).to include('public.') - end - end -end