Skip to content

Commit

Permalink
addressed some of the rubocop offenses and added todo
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbaltazar committed Jul 16, 2020
1 parent ab15ca7 commit 5566b8f
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- 'gemfiles/**/*.gemfile'
Expand Down
73 changes: 73 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-07-16 04:15:41 UTC using RuboCop version 0.88.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/MixedRegexpCaptureTypes:
Exclude:
- 'lib/apartment/elevators/domain.rb'

# Offense count: 2
# Cop supports --auto-correct.
Lint/NonDeterministicRequireOrder:
Exclude:
- 'spec/spec_helper.rb'

# Offense count: 7
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 33

# Offense count: 3
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 102

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 151

# Offense count: 6
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
Metrics/MethodLength:
Max: 24

# Offense count: 17
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/apartment/adapters/jdbc_mysql_adapter.rb'
- 'lib/apartment/adapters/postgis_adapter.rb'
- 'lib/apartment/adapters/postgresql_adapter.rb'
- 'lib/apartment/adapters/sqlite3_adapter.rb'
- 'lib/apartment/custom_console.rb'
- 'lib/apartment/deprecation.rb'
- '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'

# Offense count: 3
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'Rakefile'
- 'lib/apartment.rb'
- 'lib/apartment/tenant.rb'

# Offense count: 12
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 171
15 changes: 0 additions & 15 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,4 @@ guard :rspec do
watch(%r{^lib/apartment/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
watch(%r{^lib/apartment/(.+)\.rb$}) { |m| "spec/integration/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }

# # Rails example
# watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
# watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
# watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
# watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
# watch('config/routes.rb') { "spec/routing" }
# watch('app/controllers/application_controller.rb') { "spec/controllers" }

# # Capybara features specs
# watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }

# # Turnip features and steps
# watch(%r{^spec/acceptance/(.+)\.feature$})
# watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# rubocop:disable Metrics/BlockLength
begin
require 'bundler'
rescue StandardError
Expand Down Expand Up @@ -117,6 +118,7 @@ namespace :mysql do
`mysqladmin #{params.join(' ')} drop #{my_config['database']} --force`
end
end
# rubocop:enable Metrics/BlockLength

# TODO: clean this up
def config
Expand Down
4 changes: 2 additions & 2 deletions lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
require 'active_record'
require 'apartment/tenant'

# require_relative 'apartment/arel/visitors/postgresql'

require_relative 'apartment/active_record/log_subscriber'

if ActiveRecord.version.release >= Gem::Version.new('6.0')
require_relative 'apartment/active_record/connection_handling'
end
Expand All @@ -18,6 +17,7 @@
require_relative 'apartment/active_record/internal_metadata'
end

# Apartment main definitions
module Apartment
class << self
extend Forwardable
Expand Down
3 changes: 3 additions & 0 deletions lib/apartment/active_record/connection_handling.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

module ActiveRecord
# This is monkeypatching activerecord to ensure that whenever a new connection is established it
# switches to the same tenant as before the connection switching. This problem is more evident when
# using read replica in Rails 6
module ConnectionHandling
def connected_to_with_tenant(database: nil, role: nil, prevent_writes: false, &blk)
current_tenant = Apartment::Tenant.current
Expand Down
4 changes: 3 additions & 1 deletion lib/apartment/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

module ActiveRecord
# Supports the logging configuration to prepend the database and schema in the ActiveRecord log
class LogSubscriber
def apartment_log
return unless Apartment.active_record_log

database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true)
schema = nil
unless Apartment.connection.schema_search_path.nil?
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true)
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ",
ActiveSupport::LogSubscriber::YELLOW, true)
end
"#{database}#{schema}"
end
Expand Down
6 changes: 3 additions & 3 deletions lib/apartment/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Apartment
module Adapters
# rubocop:disable Metrics/ClassLength
# Abstract adapter from which all the Apartment DB related adapters will inherit the base logic
class AbstractAdapter
include ActiveSupport::Callbacks
define_callbacks :create, :switch
Expand Down Expand Up @@ -240,7 +240,8 @@ def db_connection_config(tenant)
def with_neutral_connection(tenant, &_block)
if Apartment.with_multi_server_setup
# neutral connection is necessary whenever you need to create/remove a database from a server.
# example: when you use postgresql, you need to connect to the default postgresql database before you create your own.
# example: when you use postgresql, you need to connect to the default postgresql database before you create
# your own.
SeparateDbConnectionHandler.establish_connection(multi_tenantify(tenant, false))
yield(SeparateDbConnectionHandler.connection)
SeparateDbConnectionHandler.connection.close
Expand Down Expand Up @@ -269,5 +270,4 @@ class SeparateDbConnectionHandler < ::ActiveRecord::Base
end
end
end
# rubocop:enable Metrics/ClassLength
end
1 change: 1 addition & 0 deletions lib/apartment/adapters/abstract_jdbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Apartment
module Adapters
# JDBC Abstract adapter
class AbstractJDBCAdapter < AbstractAdapter
private

Expand Down
1 change: 1 addition & 0 deletions lib/apartment/adapters/jdbc_postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'apartment/adapters/postgresql_adapter'

module Apartment
# JDBC helper to decide wether to use JDBC Postgresql Adapter or JDBC Postgresql Adapter with Schemas
module Tenant
def self.jdbc_postgresql_adapter(config)
if Apartment.use_schemas
Expand Down
3 changes: 3 additions & 0 deletions lib/apartment/adapters/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'apartment/adapters/abstract_adapter'

module Apartment
# Helper module to decide wether to use mysql2 adapter or mysql2 adapter with schemas
module Tenant
def self.mysql2_adapter(config)
if Apartment.use_schemas
Expand All @@ -14,6 +15,7 @@ def self.mysql2_adapter(config)
end

module Adapters
# Mysql2 Adapter
class Mysql2Adapter < AbstractAdapter
def initialize(config)
super
Expand All @@ -28,6 +30,7 @@ def rescue_from
end
end

# Mysql2 Schemas Adapter
class Mysql2SchemaAdapter < AbstractAdapter
def initialize(config)
super
Expand Down

0 comments on commit 5566b8f

Please sign in to comment.