Skip to content

Commit

Permalink
Refactor adapter includes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthuhiggins committed Oct 23, 2013
1 parent cd309c2 commit 9cbd755
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
9 changes: 9 additions & 0 deletions lib/foreigner/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def load!
def configured_name
@configured_name ||= ActiveRecord::Base.connection_pool.spec.config[:adapter]
end

def safe_include(adapter_class_name, foreigner_module)
ActiveRecord::ConnectionAdapters.const_get(adapter_class_name).class_eval do
unless ancestors.include? foreigner_module
include foreigner_module
end
end
rescue
end
end
end
end
12 changes: 2 additions & 10 deletions lib/foreigner/connection_adapters/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,5 @@ def foreign_keys(table_name)
end
end

[:JdbcAdapter, :Mysql2Adapter].each do |adapter|
begin
ActiveRecord::ConnectionAdapters.const_get(adapter).class_eval do
unless ancestors.include? Foreigner::ConnectionAdapters::Mysql2Adapter
include Foreigner::ConnectionAdapters::Mysql2Adapter
end
end
rescue
end
end
Foreigner::Adapter.safe_include :JdbcAdapter, Foreigner::ConnectionAdapters::Mysql2Adapter
Foreigner::Adapter.safe_include :Mysql2Adapter, Foreigner::ConnectionAdapters::Mysql2Adapter
12 changes: 2 additions & 10 deletions lib/foreigner/connection_adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,5 @@ def foreign_keys(table_name)
end
end

[:JdbcAdapter, :PostgreSQLAdapter].each do |adapter|
begin
ActiveRecord::ConnectionAdapters.const_get(adapter).class_eval do
unless ancestors.include? Foreigner::ConnectionAdapters::PostgreSQLAdapter
include Foreigner::ConnectionAdapters::PostgreSQLAdapter
end
end
rescue
end
end
Foreigner::Adapter.safe_include :JdbcAdapter, Foreigner::ConnectionAdapters::PostgreSQLAdapter
Foreigner::Adapter.safe_include :PostgreSQLAdapter, Foreigner::ConnectionAdapters::PostgreSQLAdapter

0 comments on commit 9cbd755

Please sign in to comment.