Permalink
Browse files

Merge pull request #8 from mguterl/fix_broken_stuff_reported_in_comments

Fix broken stuff reported in comments
  • Loading branch information...
2 parents c5d4f59 + 9f6b4b8 commit 622642b3732474263e9c449cf8623e1c4dff0517 @technoweenie technoweenie committed Nov 23, 2011
Showing with 33 additions and 2 deletions.
  1. +9 −2 lib/serialized_attributes/types.rb
  2. +24 −0 test/types_test.rb
@@ -27,9 +27,16 @@ def parse(input) input.blank? ? nil : input.to_f end
class Boolean < AttributeType
attr_reader :default
- def parse(input) input && input.respond_to?(:to_i) ? (input.to_i > 0) : input end
+ def parse(input)
+ return nil if input == ""
+ input && input.respond_to?(:to_i) ? (input.to_i > 0) : input
+ end
+
def encode(input)
- return nil if input.blank?
+ return nil if input.nil? || input == ""
+ return 1 if input == 'true'
+ return 0 if input == 'false'
+
input ? 1 : 0
end
end
View
@@ -15,4 +15,28 @@ class SerializedAttributesTypesTest < ActiveSupport::TestCase
assert_equal nil, type.encode("")
end
+ test "boolean type handles strings that look like booleans" do
+ type = SerializedAttributes::Boolean.new
+
+ assert_equal 0, type.encode("false")
+ assert_equal 1, type.encode("true")
+ end
+
+ test "boolean type encodes booleans properly" do
+ type = SerializedAttributes::Boolean.new
+
+ assert_equal 0, type.encode(false)
+ assert_equal 1, type.encode(true)
+ end
+
+ test "boolean type parses properly" do
+ type = SerializedAttributes::Boolean.new
+
+ assert_equal false, type.parse(0)
+ assert_equal true, type.parse(1)
+ assert_equal false, type.parse("0")
+ assert_equal true, type.parse("1")
+ assert_equal nil, type.parse("")
+ end
+
end

0 comments on commit 622642b

Please sign in to comment.