Add Support for Arrays to CustomFields

This commit is contained in:
Benjamin Kampmann
2014-04-25 19:15:23 +02:00
parent e502122c51
commit 1e70c3cbbd
2 changed files with 97 additions and 9 deletions

View File

@ -35,6 +35,7 @@ describe Concern::HasCustomFields do
test_item.custom_fields["bob"] = "marley"
test_item.custom_fields["jack"] = "black"
test_item.save
test_item = TestItem.find(test_item.id)
@ -51,6 +52,21 @@ describe Concern::HasCustomFields do
test_item.custom_fields.should == {"jack" => "jill"}
end
it "casts integers to string without error" do
test_item = TestItem.new
test_item.custom_fields["a"].should == nil
test_item.custom_fields["a"] = 0
test_item.custom_fields["a"].should == 0
test_item.save
# should be casted right after saving
test_item.custom_fields["a"].should == "0"
test_item = TestItem.find(test_item.id)
test_item.custom_fields["a"].should == "0"
end
it "double save actually saves" do
test_item = TestItem.new
@ -66,6 +82,33 @@ describe Concern::HasCustomFields do
end
it "handles arrays properly" do
test_item = TestItem.new
test_item.custom_fields = {"a" => ["b", "c", "d"]}
test_item.save
db_item = TestItem.find(test_item.id)
db_item.custom_fields.should == {"a" => ["b", "c", "d"]}
db_item.custom_fields["a"] = ["c", "d"]
db_item.save
db_item.custom_fields.should == {"a" => ["c", "d"]}
end
it "casts integers in arrays properly without error" do
test_item = TestItem.new
test_item.custom_fields = {"a" => ["b", 10, "d"]}
test_item.save
test_item.custom_fields.should == {"a" => ["b", "10", "d"]}
db_item = TestItem.find(test_item.id)
db_item.custom_fields.should == {"a" => ["b", "10", "d"]}
end
it "simple modifications don't interfere" do
test_item = TestItem.new