Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save whoahbot/107265 to your computer and use it in GitHub Desktop.

Select an option

Save whoahbot/107265 to your computer and use it in GitHub Desktop.
From e9aea3f6b29b2803221dbfacdb239a9f1599b2d5 Mon Sep 17 00:00:00 2001
From: Whoahbot <whoahbot@gmail.com>
Date: Tue, 5 May 2009 15:56:40 -0700
Subject: [PATCH] Adding in translations for before_type_cast, fixes a bug with rails formbuilder
---
lib/globalize/model/active_record.rb | 4 +++-
test/model/active_record/translated_test.rb | 5 +++++
2 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/lib/globalize/model/active_record.rb b/lib/globalize/model/active_record.rb
index 8f4e0b0..9309542 100644
--- a/lib/globalize/model/active_record.rb
+++ b/lib/globalize/model/active_record.rb
@@ -26,6 +26,9 @@ module Globalize
klass.send :define_method, attr_name, lambda {
globalize.fetch self.class.locale, attr_name
}
+ klass.send :define_method, "#{attr_name}_before_type_cast", lambda {
+ globalize.fetch self.class.locale, attr_name
+ }
klass.send :define_method, "#{attr_name}=", lambda {|val|
globalize.stash self.class.locale, attr_name, val
self[attr_name] = val
@@ -42,7 +45,6 @@ module Globalize
globalize.stash loc, attr_name, val
self[attr_name] = val
}
-
end
end
end
diff --git a/test/model/active_record/translated_test.rb b/test/model/active_record/translated_test.rb
index 061c763..872650a 100644
--- a/test/model/active_record/translated_test.rb
+++ b/test/model/active_record/translated_test.rb
@@ -354,6 +354,11 @@ class TranslatedTest < ActiveSupport::TestCase
with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
assert_equal default, with_include
end
+
+ test "attribute translated before type cast" do
+ Post.locale = :en
+ post = Post.create :subject => 'foo', :content => 'bar'
+ Post.locale = :de
+ post.update_attribute :subject, "German foo"
+ assert_equal 'German foo', post.subject_before_type_cast
+ Post.locale = :en
+ assert_equal 'foo', post.subject_before_type_cast
+ end
end
# TODO should validate_presence_of take fallbacks into account? maybe we need
--
1.5.5.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment