Skip to content

Commit

Permalink
Merge branch 'yujideveloper-bugfix/fix-multiple-database-connection'
Browse files Browse the repository at this point in the history
  • Loading branch information
scarroll32 committed Mar 16, 2018
2 parents 1faf48b + f3d7019 commit 7f6525e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def type_for(attr)
return nil unless attr && attr.valid?
name = attr.arel_attribute.name.to_s
table = attr.arel_attribute.relation.table_name
schema_cache = ::ActiveRecord::Base.connection.schema_cache
schema_cache = self.klass.connection.schema_cache
unless schema_cache.send(database_table_exists?, table)
raise "No table named #{table} exists."
end
Expand Down Expand Up @@ -290,7 +290,7 @@ def build_joins(relation)
join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
end
else
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, relation.table.name, join_list)
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list)
join_dependency = JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker)
join_nodes.each do |join|
join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
Expand Down Expand Up @@ -333,7 +333,7 @@ def build_association(name, parent = @base, klass = nil)
)
found_association = jd.join_root.children.last
else
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, parent.table.name, [])
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, parent.table.name, [])
jd = JoinDependency.new(
parent.base_klass,
parent.base_klass.arel_table,
Expand Down
7 changes: 7 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ module ActiveRecord
expect(subject.object).to be_an ::ActiveRecord::Relation
end

context "multiple database connection" do
it "does not raise error" do
expect { Person.ransack(name_cont: "test") }.not_to raise_error
expect { SubDB::OperationHistory.ransack(people_id_eq: 1) }.not_to raise_error
end
end

context 'with scopes' do
before do
allow(Person)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
line = '=' * message.length
puts line, message, line
Schema.create
SubDB::Schema.create
end

config.before(:all) { Sham.reset(:before_all) }
Expand Down
27 changes: 27 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,30 @@ def self.create
)
end
end

module SubDB
class Base < ActiveRecord::Base
self.abstract_class = true
establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
end

class OperationHistory < Base
end

module Schema
def self.create
s = ::ActiveRecord::Schema.new
s.instance_variable_set(:@connection, SubDB::Base.connection)
s.verbose = false
s.define({}) do
create_table :operation_histories, force: true do |t|
t.string :operation_type
t.integer :people_id
end
end
end
end
end

0 comments on commit 7f6525e

Please sign in to comment.