From f7577068613dd162c55929a0f947985bcf0102d4 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Tue, 29 Apr 2014 19:23:13 +0200 Subject: [PATCH] Ensure Reload reloads custom_fields, too --- app/models/concerns/has_custom_fields.rb | 6 ++++++ .../concern/has_custom_fields_spec.rb | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index c763db04d7d..f6b9261984e 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -7,6 +7,12 @@ module HasCustomFields after_save :save_custom_fields end + def reload(options = nil) + @custom_fields = nil + @custom_fields_orig = nil + super + end + def custom_fields @custom_fields ||= refresh_custom_fields_from_db.dup end diff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rb index 7f81dceb5ea..e37e9a72aa2 100644 --- a/spec/components/concern/has_custom_fields_spec.rb +++ b/spec/components/concern/has_custom_fields_spec.rb @@ -67,6 +67,26 @@ describe HasCustomFields do test_item.custom_fields["a"].should == "0" 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 test_item = CustomFieldsTestItem.new