Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont use custom rubocop #94

Merged
merged 6 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 73 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
inherit_from: .rubocop_todo.yml

inherit_gem:
perx-rubocop:
- default.yml

AllCops:
Exclude:
- 'gemfiles/**/*.gemfile'
Expand All @@ -20,3 +16,76 @@ Style/NumericLiterals:
Layout/EmptyLineAfterMagicComment:
Exclude:
- spec/schemas/**/*.rb

Metrics/BlockLength:
Exclude:
- spec/**/*.rb

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true

Layout/SpaceAroundMethodCallOperator:
Enabled: true

Lint/DeprecatedOpenSSLConstant:
Enabled: true

Lint/DuplicateElsifCondition:
Enabled: true

Lint/MixedRegexpCaptureTypes:
Enabled: true

Lint/RaiseException:
Enabled: true

Lint/StructNewOverride:
Enabled: true

Style/AccessorGrouping:
Enabled: true

Style/ArrayCoercion:
Enabled: true

Style/BisectedAttrAccessor:
Enabled: true

Style/CaseLikeIf:
Enabled: true

Style/ExponentialNotation:
Enabled: true

Style/HashAsLastArrayItem:
Enabled: true

Style/HashEachMethods:
Enabled: true

Style/HashLikeCase:
Enabled: true

Style/HashTransformKeys:
Enabled: true

Style/HashTransformValues:
Enabled: true

Style/RedundantAssignment:
Enabled: true

Style/RedundantFetchBlock:
Enabled: true

Style/RedundantFileExtensionInRequire:
Enabled: true

Style/RedundantRegexpCharacterClass:
Enabled: true

Style/RedundantRegexpEscape:
Enabled: true

Style/SlicingWithRange:
Enabled: true
63 changes: 50 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,66 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-02-16 15:36:55 +0800 using RuboCop version 0.77.0.
# 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: 5
# 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: 11
# Configuration parameters: CountComments, ExcludedMethods.
# Offense count: 3
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 176
Max: 102

# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 200
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 151

# Offense count: 4
# Configuration parameters: CountComments, ExcludedMethods.
# 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'
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ env:
jobs:
include:
- name: Rubocop Lint
script: gem install perx-rubocop && rubocop
script: gem install rubocop

allow_failures:
- rvm: ruby-head
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

source 'https://my.diffend.io/protect/gems'
source 'http://rubygems.org'

gemspec

gem 'perx-rubocop', '~> 0.0.3'
gem 'rails', '>= 3.1.2'
gem 'rubocop'

group :local do
gem 'guard-rspec', '~> 4.2'
Expand Down
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
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ namespace :db do
apartment_db_file = 'spec/config/database.yml'
rails_db_file = 'spec/dummy/config/database.yml'

FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, verbose: true) unless File.exist?(apartment_db_file)
FileUtils.copy(rails_db_file + '.sample', rails_db_file, verbose: true) unless File.exist?(rails_db_file)
unless File.exist?(apartment_db_file)
FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, verbose: true)
end
FileUtils.copy(rails_db_file + '.sample', rails_db_file, verbose: true) unless File.exist?(rails_db_file)
end
end

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

source "http://rubygems.org"

gem "perx-rubocop", "~> 0.0.3"
gem "rails", "~> 5.0.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

source "http://rubygems.org"

gem "perx-rubocop", "~> 0.0.3"
gem "rails", "~> 5.1.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

source "http://rubygems.org"

gem "perx-rubocop", "~> 0.0.3"
gem "rails", "~> 5.2.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

source "http://rubygems.org"

gem "perx-rubocop", "~> 0.0.3"
gem "rails", "~> 6.0.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_master.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

source "http://rubygems.org"

gem "perx-rubocop", "~> 0.0.3"
gem "rails", git: "https://github.com/rails/rails.git"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
Expand Down
8 changes: 5 additions & 3 deletions lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
require 'active_record'
require 'apartment/tenant'

# require_relative 'apartment/arel/visitors/postgresql'

require_relative 'apartment/active_record/log_subscriber'
require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')

if ActiveRecord.version.release >= Gem::Version.new('6.0')
require_relative 'apartment/active_record/connection_handling'
end

if ActiveRecord.version.release >= Gem::Version.new('6.1')
require_relative 'apartment/active_record/schema_migration'
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
2 changes: 0 additions & 2 deletions lib/apartment/active_record/internal_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# frozen_string_literal: true

# rubocop:disable Rails/ApplicationRecord
class InternalMetadata < ActiveRecord::Base # :nodoc:
class << self
def table_exists?
connection.table_exists?(table_name)
end
end
end
# rubocop:enable Rails/ApplicationRecord
9 changes: 6 additions & 3 deletions lib/apartment/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# 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
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true) unless Apartment.connection.schema_search_path.nil?
unless Apartment.connection.schema_search_path.nil?
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ",
ActiveSupport::LogSubscriber::YELLOW, true)
end
"#{database}#{schema}"
end

def payload_binds(binds, type_casted_binds)
return unless (binds || []).empty?

casted_params = type_casted_binds(type_casted_binds)
binds = ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
binds
' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
end

def sql(event)
Expand Down
2 changes: 0 additions & 2 deletions lib/apartment/active_record/schema_migration.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# frozen_string_literal: true

module ActiveRecord
# rubocop:disable Rails/ApplicationRecord
class SchemaMigration < ActiveRecord::Base # :nodoc:
class << self
def table_exists?
connection.table_exists?(table_name)
end
end
end
# rubocop:enable Rails/ApplicationRecord
end
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
3 changes: 2 additions & 1 deletion lib/apartment/adapters/abstract_jdbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

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

def multi_tenantify_with_tenant_db_name(config, tenant)
config[:url] = "#{config[:url].gsub(%r{(\S+)\/.+$}, '\1')}/#{environmentify(tenant)}"
config[:url] = "#{config[:url].gsub(%r{(\S+)/.+$}, '\1')}/#{environmentify(tenant)}"
end

def rescue_from
Expand Down
Loading