Skip to content

Commit

Permalink
use 'adapter_name' instead of 'class.to_s' to determine database type
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
  • Loading branch information
schoefmann authored and marcoow committed Jan 15, 2009
1 parent d48fcc5 commit 4e9584b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/kvlr/reports_as_sparkline/grouping.rb
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 6 additions & 10 deletions spec/classes/grouping_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -153,7 +153,3 @@
end

end

class ActiveRecord::ConnectionAdapters::MysqlAdapter; end
class ActiveRecord::ConnectionAdapters::SQLite3Adapter; end
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter; end

0 comments on commit 4e9584b

Please sign in to comment.