Make compatible with Rails 2 and 3

This commit is contained in:
Evgeniy Dolzhenko 2010-09-22 23:01:40 -07:00
parent e0015391fb
commit 61d31e964f
3 changed files with 49 additions and 18 deletions

10
lib/silent-postgres.rb Normal file → Executable file
View File

@ -1,3 +1,7 @@
if %w(test development).include?(ENV["RAILS_ENV"])
require "silent-postgres/railtie"
module SilentPostgres module SilentPostgres
SILENCED_METHODS = %w(tables indexes column_definitions pk_and_sequence_for last_insert_id) SILENCED_METHODS = %w(tables indexes column_definitions pk_and_sequence_for last_insert_id)
@ -8,7 +12,7 @@ module SilentPostgres
end end
SILENCED_METHODS.each do |m| SILENCED_METHODS.each do |m|
eval <<METHOD eval <<-METHOD
def #{m}_with_silencer(*args) def #{m}_with_silencer(*args)
@logger.silence do @logger.silence do
#{m}_without_silencer(*args) #{m}_without_silencer(*args)
@ -18,7 +22,5 @@ METHOD
end end
end end
if %w(test development).include?(ENV["RAILS_ENV"])
puts "Silencing Postgres"
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, SilentPostgres)
end end

20
lib/silent-postgres/railtie.rb Executable file
View File

@ -0,0 +1,20 @@
module SilentPostgres
def self.init!
puts "Silencing Postgres"
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, SilentPostgres)
end
if defined?(Rails::Railtie)
# Rails 3
class Railtie < Rails::Railtie
initializer 'silent_postgres.insert_into_active_record' do
ActiveSupport.on_load :active_record do
SilentPostgres.init!
end
end
end
else
# Rails 2
init!
end
end

9
silent-postgres.gemspec Executable file
View File

@ -0,0 +1,9 @@
Gem::Specification.new do |s|
s.name = %q{silent-postgres}
s.version = "0.0.4"
s.summary = %q{Rails plugin that silences Postgresql connection adapter verbose output}
s.email = ['bragi@ragnarson.com', 'dolzenko@gmail.com']
s.homepage = %q{http://github.com/dolzenko/silent-postgres}
s.authors = ["Łukasz Piestrzeniewicz", "Evgeniy Dolzhenko"]
s.files = ["MIT-LICENSE", "README", "lib", "silent-postgres.gemspec", "lib/silent-postgres", "lib/silent-postgres/railtie.rb", "lib/silent-postgres.rb", "init.rb"]
end