From 46021b9351fc28fe3fcd7fc4ea970e2589ae14a0 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 10 May 2010 01:18:05 -0700 Subject: [PATCH] fix boolean casts --- .../connection_adapters/mysql2_adapter.rb | 6 ++- spec/active_record/active_record_spec.rb | 42 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/active_record/connection_adapters/mysql2_adapter.rb b/lib/active_record/connection_adapters/mysql2_adapter.rb index 9763c7f..8f47cfe 100644 --- a/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -68,7 +68,11 @@ module ActiveRecord end def type_cast_code(var_name) - nil + case type + when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" + else + nil + end end private diff --git a/spec/active_record/active_record_spec.rb b/spec/active_record/active_record_spec.rb index 1fe4155..0c23070 100644 --- a/spec/active_record/active_record_spec.rb +++ b/spec/active_record/active_record_spec.rb @@ -13,7 +13,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do ActiveRecord::Base.establish_connection(:adapter => 'mysql2') }.should_not raise_error(Mysql2::Error) end - + context "once connected" do before(:each) do @connection = ActiveRecord::Base.connection @@ -24,7 +24,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do @connection.execute("SELECT NOW() as n").first['n'].class.should eql(Time) end end - + context "columns" do before(:all) do ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test') @@ -33,6 +33,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do `id` mediumint(9) NOT NULL AUTO_INCREMENT, `null_test` varchar(10) DEFAULT NULL, `bit_test` bit(64) NOT NULL DEFAULT b'1', + `boolean_test` tinyint(1) DEFAULT 0, `tiny_int_test` tinyint(4) NOT NULL DEFAULT '1', `small_int_test` smallint(6) NOT NULL DEFAULT '1', `medium_int_test` mediumint(9) NOT NULL DEFAULT '1', @@ -75,6 +76,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do test = Mysql2Test2.new test.null_test.should be_nil test.bit_test.should eql("b'1'") + test.boolean_test.should eql(false) test.tiny_int_test.should eql(1) test.small_int_test.should eql(1) test.medium_int_test.should eql(1) @@ -103,6 +105,42 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do test.long_blob_test.should be_nil test.enum_test.should eql('val1') test.set_test.should eql('val1,val2') + test.save + end + + it "should have correct values when pulled from a db record" do + test = Mysql2Test2.last + test.null_test.should be_nil + test.bit_test.class.should eql(String) + test.boolean_test.should eql(false) + test.tiny_int_test.should eql(1) + test.small_int_test.should eql(1) + test.medium_int_test.should eql(1) + test.int_test.should eql(1) + test.big_int_test.should eql(1) + test.float_test.should eql('1.0000'.to_f) + test.double_test.should eql('1.0000'.to_f) + test.decimal_test.should eql(BigDecimal.new('1.0000')) + test.date_test.should eql(Date.parse('2010-01-01')) + test.date_time_test.should eql(Time.parse('2010-01-01 00:00:00')) + test.timestamp_test.class.should eql(Time) + test.time_test.class.should eql(Time) + test.year_test.should eql(2010) + test.char_test.should eql('abcdefghij') + test.varchar_test.should eql('abcdefghij') + test.binary_test.should eql('abcdefghij') + test.varbinary_test.should eql('abcdefghij') + test.tiny_blob_test.should eql("") + test.tiny_text_test.should be_nil + test.blob_test.should be_nil + test.text_test.should be_nil + test.medium_blob_test.should be_nil + test.medium_text_test.should be_nil + test.long_blob_test.should be_nil + test.long_text_test.should be_nil + test.long_blob_test.should be_nil + test.enum_test.should eql('val1') + test.set_test.should eql('val1,val2') end end end \ No newline at end of file