Skip to content

Instantly share code, notes, and snippets.

@carlossanchezp
Last active December 17, 2015 22:39
Show Gist options
  • Save carlossanchezp/5684012 to your computer and use it in GitHub Desktop.
Save carlossanchezp/5684012 to your computer and use it in GitHub Desktop.
SchemaPlus::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
def included(base) #:nodoc:
base.alias_method_chain :initialize, :schema_plus
end
def initialize_with_schema_plus(*args) #:nodoc:
initialize_without_schema_plus(*args)
adapter = case adapter_name
# name of MySQL adapter depends on mysql gem
# * with mysql gem adapter is named MySQL
# * with mysql2 gem adapter is named Mysql2
# Here we handle this and hopefully futher adapter names
when /^MySQL/i then 'MysqlAdapter'
when 'PostgreSQL', 'PostGIS' then 'PostgresqlAdapter'
when 'SQLite' then 'Sqlite3Adapter'
when 'SQLServer' then 'SQLServerAdapter'
end or raise "SchemaPlus: Unsupported adapter name #{adapter_name.inspect}"
# use 'end or raise' instead of 'else raise' to get
# 100% C0 code coverage despite having no test case
# for this.
if adapter != 'SQLServerAdapter'
adapter_module = SchemaPlus::ActiveRecord::ConnectionAdapters.const_get(adapter)
self.class.send(:include, adapter_module) unless self.class.include?(adapter_module)
self.post_initialize if self.respond_to? :post_initialize
extend(SchemaPlus::ActiveRecord::ForeignKeys)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment