From 704bee6f00a8639562e6ea49675ff28eaabb73c6 Mon Sep 17 00:00:00 2001 From: Joshua Harvey Date: Tue, 24 Feb 2009 14:26:00 +0200 Subject: [PATCH] fixed nil assign bug --- lib/globalize/model/active_record/adapter.rb | 9 ++++++++- test/model/active_record/translated_test.rb | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/globalize/model/active_record/adapter.rb b/lib/globalize/model/active_record/adapter.rb index 31bab246c..d1c8db813 100644 --- a/lib/globalize/model/active_record/adapter.rb +++ b/lib/globalize/model/active_record/adapter.rb @@ -1,6 +1,12 @@ module Globalize module Model class AttributeStash < Hash + def contains?(locale, attr_name) + locale = locale.to_sym + self[locale] ||= {} + self[locale].has_key? attr_name + end + def read(locale, attr_name) locale = locale.to_sym self[locale] ||= {} @@ -25,7 +31,8 @@ def initialize(record) def fetch(locale, attr_name) # locale = I18n.locale - @cache.read(locale, attr_name) || begin + is_cached = @cache.contains?(locale, attr_name) + is_cached ? @cache.read(locale, attr_name) : begin value = fetch_attribute locale, attr_name @cache.write locale, attr_name, value if value && value.locale == locale value diff --git a/test/model/active_record/translated_test.rb b/test/model/active_record/translated_test.rb index d476a6b19..8fcbd9608 100644 --- a/test/model/active_record/translated_test.rb +++ b/test/model/active_record/translated_test.rb @@ -268,6 +268,13 @@ def teardown post.subject = 'baz' assert_equal 'foo', post.reload.subject end + + test 'complex writing and stashing' do + post = Post.create :subject => 'foo', :content => 'bar' + post.subject = nil + assert_nil post.subject + assert !post.valid? + end end # TODO should validate_presence_of take fallbacks into account? maybe we need