Skip to content

Commit

Permalink
fixed nil assign bug
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmh committed Feb 24, 2009
1 parent ecb7e9b commit 704bee6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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] ||= {}
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/model/active_record/translated_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 704bee6

Please sign in to comment.