Skip to content

Commit

Permalink
Fix rubocop warnings
Browse files Browse the repository at this point in the history
Enable more
  • Loading branch information
Philip Dubé committed Oct 18, 2023
1 parent de48c9f commit 677b9e2
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 53 deletions.
15 changes: 10 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ AllCops:
- 'node_modules/**/*'
- 'Vagrantfile'
TargetRubyVersion: 3.0
SuggestExtensions: false
NewCops: enable

Style/FrozenStringLiteralComment:
Gemspec/DevelopmentDependencies:
Enabled: false

Lint/ConstantDefinitionInBlock:
Enabled: false

Lint/EmptyBlock:
Enabled: false

Style/Documentation:
Exclude:
- '**/*.rb'
Enabled: false

Lint/ConstantDefinitionInBlock:
Enabled: false

Style/ClassAndModuleChildren:
Style/DocumentDynamicEvalDefinition:
Enabled: false

Metrics/BlockLength:
Expand Down
2 changes: 2 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

appraise 'rails-6.0' do
gem 'rails', '~> 6.0.3'
end
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'bundler/gem_tasks'
Expand Down
8 changes: 5 additions & 3 deletions activerecord-multi-tenant.gemspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'activerecord-multi-tenant/version'

Gem::Specification.new do |spec|
spec.name = 'activerecord-multi-tenant'
spec.version = MultiTenant::VERSION
spec.summary = 'ActiveRecord/Rails integration for multi-tenant databases, '\
'in particular the Citus extension for PostgreSQL'
spec.summary = 'ActiveRecord/Rails integration for multi-tenant databases, ' \
'in particular the Citus extension for PostgreSQL'
spec.description = ''
spec.authors = ['Citus Data']
spec.email = '[email protected]'
spec.required_ruby_version = '>= 3.0.0'
spec.metadata = { 'rubygems_mfa_required' => 'true' }

spec.files = `git ls-files`.split("\n")
spec.test_files = `git ls-files -- {spec}/*`.split("\n")
spec.require_paths = ['lib']
spec.homepage = 'https://github.com/citusdata/activerecord-multi-tenant'
spec.license = 'MIT'
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/copy_from_client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MultiTenant
# Designed to be mixed into an ActiveRecord model to provide
# a copy_from_client method that allows for efficient bulk insertion of
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/fast_truncate.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Truncates only the tables that have been modified, according to sequence
# values
# Faster alternative to DatabaseCleaner.clean_with(:truncation, pre_count: true)
Expand Down
7 changes: 3 additions & 4 deletions lib/activerecord-multi-tenant/habtm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ def has_and_belongs_to_many_with_tenant(name, scope = nil, **options, &extension

# This method sets the tenant_id on the join table and executes before creation of the join table record.
define_method :tenant_set do
if tenant_enabled
raise MultiTenant::MissingTenantError, 'Tenant Id is not set' unless MultiTenant.current_tenant_id
return unless tenant_enabled
raise MultiTenant::MissingTenantError, 'Tenant Id is not set' unless MultiTenant.current_tenant_id

send("#{tenant_column}=", MultiTenant.current_tenant_id)
end
send("#{tenant_column}=", MultiTenant.current_tenant_id)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/migrations.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MultiTenant
module MigrationExtensions
def create_distributed_table(table_name, partition_key)
Expand Down
28 changes: 17 additions & 11 deletions lib/activerecord-multi-tenant/model_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require_relative './multi_tenant'
# frozen_string_literal: true

require_relative 'multi_tenant'

module MultiTenant
# Extension to the model to allow scoping of models to the current tenant. This is done by adding
# the multitenant method to the models that need to be scoped. This method is called in the
# model declaration.
# Adds scoped_by_tenant? partition_key, primary_key and inherited methods to the model
module ModelExtensionsClassMethods
DEFAULT_ID_FIELD = 'id'.freeze
DEFAULT_ID_FIELD = 'id'
# executes when multi_tenant method is called in the model. This method adds the following
# methods to the model that calls it.
# scoped_by_tenant? - returns true if the model is scoped by tenant
Expand Down Expand Up @@ -188,17 +190,21 @@ def inherited(subclass)
end

# skips statement caching for classes that is Multi-tenant or has a multi-tenant relation
class ActiveRecord::Associations::Association
alias skip_statement_cache_orig skip_statement_cache?
module ActiveRecord
module Associations
class Association
alias skip_statement_cache_orig skip_statement_cache?

def skip_statement_cache?(*scope)
return true if klass.respond_to?(:scoped_by_tenant?) && klass.scoped_by_tenant?
def skip_statement_cache?(*scope)
return true if klass.respond_to?(:scoped_by_tenant?) && klass.scoped_by_tenant?

if reflection.through_reflection
through_klass = reflection.through_reflection.klass
return true if through_klass.respond_to?(:scoped_by_tenant?) && through_klass.scoped_by_tenant?
end
if reflection.through_reflection
through_klass = reflection.through_reflection.klass
return true if through_klass.respond_to?(:scoped_by_tenant?) && through_klass.scoped_by_tenant?
end

skip_statement_cache_orig(*scope)
skip_statement_cache_orig(*scope)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/multi_tenant.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'active_support/current_attributes'

module MultiTenant
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/query_monitor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add generic warning when queries fail and there is no tenant set
# To handle this case, a QueryMonitor hook is created and registered
# to sql.active_record. This hook will log a warning when a query fails
Expand Down
4 changes: 3 additions & 1 deletion lib/activerecord-multi-tenant/query_rewriter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'active_record'
require_relative './arel_visitors_depth_first' unless Arel::Visitors.const_defined?(:DepthFirst)
require_relative 'arel_visitors_depth_first' unless Arel::Visitors.const_defined?(:DepthFirst)

# Iterates AST and adds tenant enforcement clauses to all relations
module MultiTenant
Expand Down
56 changes: 31 additions & 25 deletions lib/activerecord-multi-tenant/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
# frozen_string_literal: true

require 'sidekiq/client'

# Adds methods to handle tenant information both in the client and server.
module Sidekiq::Middleware::MultiTenant
# Get the current tenant and store in the message to be sent to Sidekiq.
class Client
def call(_worker_class, msg, _queue, _redis_pool)
if MultiTenant.current_tenant.present?
msg['multi_tenant'] ||=
{
'class' => MultiTenant.current_tenant_class,
'id' => MultiTenant.current_tenant_id
}
end
module Sidekiq
module Middleware
module MultiTenant
# Get the current tenant and store in the message to be sent to Sidekiq.
class Client
def call(_worker_class, msg, _queue, _redis_pool)
if MultiTenant.current_tenant.present?
msg['multi_tenant'] ||=
{
'class' => MultiTenant.current_tenant_class,
'id' => MultiTenant.current_tenant_id
}
end

yield
end
end
yield
end
end

# Pull the tenant out and run the current thread with it.
class Server
def call(_worker_class, msg, _queue, &block)
if msg.key?('multi_tenant')
tenant = begin
msg['multi_tenant']['class'].constantize.find(msg['multi_tenant']['id'])
rescue ActiveRecord::RecordNotFound
msg['multi_tenant']['id']
# Pull the tenant out and run the current thread with it.
class Server
def call(_worker_class, msg, _queue, &block)
if msg.key?('multi_tenant')
tenant = begin
msg['multi_tenant']['class'].constantize.find(msg['multi_tenant']['id'])
rescue ActiveRecord::RecordNotFound
msg['multi_tenant']['id']
end
MultiTenant.with(tenant, &block)
else
yield
end
end
MultiTenant.with(tenant, &block)
else
yield
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord-multi-tenant/table_node.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MultiTenant
module TableNode
# Return table name
Expand Down
4 changes: 3 additions & 1 deletion lib/activerecord-multi-tenant/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MultiTenant
VERSION = '2.4.0'.freeze
VERSION = '2.4.0'
end
2 changes: 2 additions & 0 deletions spec/activerecord-multi-tenant/associations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe MultiTenant, 'Association methods' do
Expand Down
2 changes: 1 addition & 1 deletion spec/activerecord-multi-tenant/multi_tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe MultiTenant do
describe '.load_current_tenant!' do
let(:fake_tenant) { OpenStruct.new(id: 1) }
let(:fake_tenant) { double(id: 1) }
let(:mock_klass) { double(find: fake_tenant) }

before do
Expand Down
2 changes: 2 additions & 0 deletions spec/activerecord-multi-tenant/record_callback_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

class ProjectWithCallbacks < ActiveRecord::Base
Expand Down
2 changes: 2 additions & 0 deletions spec/activerecord-multi-tenant/record_finding_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe MultiTenant, 'Record finding' do
Expand Down
2 changes: 2 additions & 0 deletions spec/activerecord-multi-tenant/sidekiq_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'
require 'sidekiq/client'
require 'activerecord-multi-tenant/sidekiq'
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Application < Rails::Application; end

require 'bundler'
Bundler.require(:default, :development)
require_relative './support/format_sql'
require_relative 'support/format_sql'

dbconfig = YAML.safe_load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
dbconfig = YAML.safe_load_file(File.join(File.dirname(__FILE__), 'database.yml'))
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'debug.log'))
ActiveRecord::Base.establish_connection(dbconfig['test'])

Expand Down

0 comments on commit 677b9e2

Please sign in to comment.