Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make translated attribute readers optionally accept a locale (e.g.

post.subject(:de))
  • Loading branch information...
commit 65c83aab6aaf9f7c05efeccaa3acc8d77b61ddb5 1 parent 614bb99
@svenfuchs svenfuchs authored
View
4 lib/globalize/active_record.rb
@@ -132,8 +132,8 @@ def translated_attr_accessor(name)
globalize.write(self.class.locale || I18n.locale, name, value)
self[name] = value
}
- define_method name, lambda {
- globalize.fetch(self.class.locale || I18n.locale, name)
+ define_method name, lambda { |*args|
+ globalize.fetch(args.first || self.class.locale || I18n.locale, name)
}
alias_method "#{name}_before_type_cast", name
end
View
32 test/active_record/translates_test.rb
@@ -52,4 +52,36 @@ def setup
assert post.respond_to?(:subject)
assert post.respond_to?(:subject=)
end
+
+ test 'attribute reader without arguments will use the current locale on ActiveRecord::Base or I18n' do
+ post = Post.with_locale(:de) do
+ Post.create!(:subject => 'Titel', :content => 'Inhalt')
+ end
+ I18n.locale = :de
+ assert_equal 'Titel', post.subject
+
+ I18n.locale = :en
+ ActiveRecord::Base.locale = :de
+ assert_equal 'Titel', post.subject
+ end
+
+ test 'attribute reader when passed a locale will use the given locale' do
+ post = Post.with_locale(:de) do
+ Post.create!(:subject => 'Titel', :content => 'Inhalt')
+ end
+ assert_equal 'Titel', post.subject(:de)
+ end
+
+ test 'attribute reader will use the current locale on ActiveRecord::Base or I18n' do
+ post = Post.with_locale(:en) do
+ Post.create!(:subject => 'title', :content => 'content')
+ end
+ I18n.locale = :de
+ post.subject = 'Titel'
+ assert_equal 'Titel', post.subject
+
+ ActiveRecord::Base.locale = :en
+ post.subject = 'title'
+ assert_equal 'title', post.subject
+ end
end
View
13 test/active_record_test.rb
@@ -55,7 +55,7 @@ def assert_translated(locale, record, names, expected)
end
test "attribute writers do return their argument" do
- value = (Post.new.subject = 'foo')
+ value = Post.new.subject = 'foo'
assert_equal 'foo', value
end
@@ -67,11 +67,20 @@ def assert_translated(locale, record, names, expected)
test "update_attributes fails with invalid values" do
post = Post.create(:subject => 'foo', :content => 'bar')
- assert !post.update_attributes( { :subject => '' } )
+ assert !post.update_attributes(:subject => '')
assert_nil post.reload.attributes['subject']
assert_equal 'foo', post.subject
end
+ test "passing the locale to create uses the given locale" do
+ post = Post.create(:subject => 'Titel', :content => 'Inhalt', :locale => :de)
+ assert_equal :en, I18n.locale
+ assert_nil ActiveRecord::Base.locale
+
+ I18n.locale = :de
+ assert_equal 'Titel', post.subject
+ end
+
test "passing the locale to attributes= uses the given locale" do
post = Post.create(:subject => 'title', :content => 'content')
post.update_attributes(:subject => 'Titel', :content => 'Inhalt', :locale => :de)
Please sign in to comment.
Something went wrong with that request. Please try again.