Permalink
Browse files

Merge pull request #5 from mguterl/boolean_handle_blanks

boolean type should encode blank strings to nil
  • Loading branch information...
2 parents 8e1656a + 21a8743 commit c5d4f5961cf031952515f38e36128f15730c9fe3 @technoweenie technoweenie committed Oct 2, 2011
Showing with 14 additions and 2 deletions.
  1. +1 −1 lib/serialized_attributes/types.rb
  2. +5 −0 test/serialized_attributes_test.rb
  3. +2 −1 test/test_helper.rb
  4. +6 −0 test/types_test.rb
@@ -29,7 +29,7 @@ class Boolean < AttributeType
attr_reader :default
def parse(input) input && input.respond_to?(:to_i) ? (input.to_i > 0) : input end
def encode(input)
- return nil if input.nil?
+ return nil if input.blank?
input ? 1 : 0
end
end
@@ -238,6 +238,11 @@ def @record.title=(v)
assert !@record.data.key?('active')
end
+ test "ignores empty strings for booleans" do
+ @newbie.clearance = ""
+ assert_nil @newbie.clearance
+ end
+
test "attempts to re-encode data when saving" do
assert_not_nil @record.title
@record.raw_data = nil
View
@@ -76,7 +76,8 @@ class SerializedRecordWithDefaults < ActiveRecord::Base
boolean :active, :default => true
array :names, :default => %w(a b c)
hash :extras, :default => {:a => 1}
+ boolean :clearance, :default => nil
end
before_save { |r| false } # cancel the save
-end
+end
View
@@ -9,4 +9,10 @@ class SerializedAttributesTypesTest < ActiveSupport::TestCase
assert_equal nil, type.encode(nil)
end
+ test "boolean type encodes blank string properly" do
+ type = SerializedAttributes::Boolean.new
+
+ assert_equal nil, type.encode("")
+ end
+
end

0 comments on commit c5d4f59

Please sign in to comment.