Skip to content
This repository
Browse code

Added tests for boolean conversion, and added handlers for boolean ->…

… boolean and integer -> boolean transforms
  • Loading branch information...
commit 8ffae1836d5310bff0835ae7c0ae7378bde17fa3 1 parent 1b7ab17
Rob Sharp authored committed
8 lib/serialization_helper.rb
@@ -115,12 +115,16 @@ def self.convert_booleans(records, columns)
115 115 records.each do |record|
116 116 columns.each do |column|
117 117 next if is_boolean(record[column])
118   - record[column] = (record[column] == 't' or record[column] == '1')
  118 + record[column] = convert_boolean(record[column])
119 119 end
120 120 end
121 121 records
122 122 end
123 123
  124 + def self.convert_boolean(value)
  125 + ['t', '1', true, 1].include?(value)
  126 + end
  127 +
124 128 def self.boolean_columns(table)
125 129 columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } }
126 130 columns.map { |c| c.name }
@@ -192,4 +196,4 @@ def self.table_record_count(table)
192 196
193 197 end
194 198
195   -end
  199 +end
20 spec/serialization_utils_spec.rb
@@ -28,4 +28,24 @@
28 28 ActiveRecord::Base.connection.should_receive(:quote_table_name).with('values').and_return('`values`')
29 29 SerializationHelper::Utils.quote_table('values').should == '`values`'
30 30 end
  31 +
  32 + it "should convert ruby booleans to true and false" do
  33 + SerializationHelper::Utils.convert_boolean(true).should == true
  34 + SerializationHelper::Utils.convert_boolean(false).should == false
  35 + end
  36 +
  37 + it "should convert ruby string t and f to true and false" do
  38 + SerializationHelper::Utils.convert_boolean('t').should == true
  39 + SerializationHelper::Utils.convert_boolean('f').should == false
  40 + end
  41 +
  42 + it "should convert ruby string 1 and 0 to true and false" do
  43 + SerializationHelper::Utils.convert_boolean('1').should == true
  44 + SerializationHelper::Utils.convert_boolean('0').should == false
  45 + end
  46 +
  47 + it "should convert ruby integer 1 and 0 to true and false" do
  48 + SerializationHelper::Utils.convert_boolean(1).should == true
  49 + SerializationHelper::Utils.convert_boolean(0).should == false
  50 + end
31 51 end

0 comments on commit 8ffae18

Please sign in to comment.
Something went wrong with that request. Please try again.