Ensure Reload reloads custom_fields, too

This commit is contained in:
Benjamin Kampmann 2014-04-29 19:23:13 +02:00
parent 230453b411
commit f757706861
2 changed files with 26 additions and 0 deletions

View File

@ -7,6 +7,12 @@ module HasCustomFields
after_save :save_custom_fields after_save :save_custom_fields
end end
def reload(options = nil)
@custom_fields = nil
@custom_fields_orig = nil
super
end
def custom_fields def custom_fields
@custom_fields ||= refresh_custom_fields_from_db.dup @custom_fields ||= refresh_custom_fields_from_db.dup
end end

View File

@ -67,6 +67,26 @@ describe HasCustomFields do
test_item.custom_fields["a"].should == "0" test_item.custom_fields["a"].should == "0"
end end
it "reload loads from database" do
test_item = CustomFieldsTestItem.new
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"
CustomFieldsTestItem.exec_sql("UPDATE custom_fields_test_item_custom_fields SET value='1' WHERE custom_fields_test_item_id=? AND name='a'", test_item.id)
# still the same, did not load
test_item.custom_fields["a"].should == "0"
# refresh loads from database
test_item.reload.custom_fields["a"].should == "1"
test_item.custom_fields["a"].should == "1"
end
it "double save actually saves" do it "double save actually saves" do
test_item = CustomFieldsTestItem.new test_item = CustomFieldsTestItem.new