From 4e9584baf0421883d41e415f62df65a741625acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Sch=C3=B6fmann?= Date: Thu, 15 Jan 2009 22:23:54 +0800 Subject: [PATCH] use 'adapter_name' instead of 'class.to_s' to determine database type Signed-off-by: Marco Otte-Witte --- lib/kvlr/reports_as_sparkline/grouping.rb | 12 ++++++------ spec/classes/grouping_spec.rb | 16 ++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sparkline/grouping.rb index 93486e1..786b803 100644 --- a/lib/kvlr/reports_as_sparkline/grouping.rb +++ b/lib/kvlr/reports_as_sparkline/grouping.rb @@ -18,7 +18,7 @@ def identifier end def date_parts_from_db_string(db_string) #:nodoc: - if ActiveRecord::Base.connection.class.to_s == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' + if ActiveRecord::Base.connection.adapter_name =~ /postgres/i case @identifier when :hour return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:to_i) @@ -33,7 +33,7 @@ def date_parts_from_db_string(db_string) #:nodoc: end else parts = db_string.split('/').map(&:to_i) - if ActiveRecord::Base.connection.class.to_s == 'ActiveRecord::ConnectionAdapters::MysqlAdapter' + if ActiveRecord::Base.connection.adapter_name =~ /mysql/i if @identifier == :week && parts[1] > 52 parts[0] += 1 parts[1] = 1 @@ -46,12 +46,12 @@ def date_parts_from_db_string(db_string) #:nodoc: end def to_sql(date_column) #:nodoc: - return case ActiveRecord::Base.connection.class.to_s - when 'ActiveRecord::ConnectionAdapters::MysqlAdapter' + return case ActiveRecord::Base.connection.adapter_name + when /mysql/i mysql_format(date_column) - when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter' + when /sqlite/i sqlite_format(date_column) - when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' + when /postgres/i postgresql_format(date_column) end end diff --git a/spec/classes/grouping_spec.rb b/spec/classes/grouping_spec.rb index 4792664..21cdfbd 100644 --- a/spec/classes/grouping_spec.rb +++ b/spec/classes/grouping_spec.rb @@ -15,7 +15,7 @@ describe 'for MySQL' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::MysqlAdapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL') end it 'should use DATE_FORMAT with format string "%Y/%m/%d/%H" for grouping :hour' do @@ -39,7 +39,7 @@ describe 'for PostgreSQL' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('PostgreSQL') end for grouping in [:hour, :day, :week, :month] do @@ -55,7 +55,7 @@ describe 'for SQLite3' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('SQLite') end it 'should use strftime with format string "%Y/%m/%d/%H" for grouping :hour' do @@ -83,7 +83,7 @@ describe 'for SQLite3' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('SQLite') end for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do @@ -106,7 +106,7 @@ describe 'for PostgreSQL' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('PostgreSQL') end it 'should split the date part of the string with "-" and read out the hour for grouping :hour' do @@ -130,7 +130,7 @@ describe 'for MySQL' do before do - ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::MysqlAdapter) + ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL') end for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:week, '2008/40'], [:month, '2008/12']] do @@ -153,7 +153,3 @@ end end - -class ActiveRecord::ConnectionAdapters::MysqlAdapter; end -class ActiveRecord::ConnectionAdapters::SQLite3Adapter; end -class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter; end