Permalink
Browse files

adding set_translations method which allows to set multiple translati…

…ons for a given translated record at once. It is possible to update only certain attributes for some locales as well leaving the other translations / attributes as they are. Options hash looks like:

options {
	:en => { :title => "Hello" }
	:de => { :title => "Hallo", :text => "Wie gehts?"}
}

That would update only the :title on the english version of the record but updating :title and :text on the german one.

Added tests as well
  • Loading branch information...
1 parent a46ab1e commit 3f7c2f64788500ab5936048f4453e2d2fa80b80d @hukl hukl committed Apr 8, 2009
Showing with 71 additions and 0 deletions.
  1. +12 −0 lib/globalize/model/active_record/translated.rb
  2. +59 −0 test/model/active_record/translated_test.rb
@@ -132,6 +132,18 @@ def update_globalize_record
def translated_locales
globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym }
end
+
+ def set_translations options
+ options.keys.each do |key|
+
+ unless translation = globalize_translations.find_by_locale(key.to_s)
+ translation = globalize_translations.create!(:locale => key.to_s)
+ end
+
+ translation.update_attributes!(options[key])
+ end
+ end
+
end
end
end
@@ -354,6 +354,65 @@ def tranlsations_included
with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
assert_equal default, with_include
end
+
+ test "setting multiple translations at once with options hash" do
+ Post.locale = :de
+ post = Post.create :subject => "foo1", :content => "foo1"
+ Post.locale = :en
+ post.update_attributes( :subject => "bar1", :content => "bar1" )
+
+ options = { :de => {:subject => "foo2", :content => "foo2"},
+ :en => {:subject => "bar2", :content => "bar2"} }
+ post.set_translations options
+ post.reload
+
+ assert ["bar2", "bar2"], [post.subject, post.content]
+ Post.locale = :de
+ assert ["foo2", "foo2"], [post.subject, post.content]
+ end
+
+ test "setting only one translation with set_translations" do
+ Post.locale = :de
+ post = Post.create :subject => "foo1", :content => "foo1"
+ Post.locale = :en
+ post.update_attributes( :subject => "bar1", :content => "bar1" )
+
+ options = { :en => {:subject => "bar2", :content => "bar2"} }
+ post.set_translations options
+ post.reload
+
+ assert ["bar2", "bar2"], [post.subject, post.content]
+ Post.locale = :de
+ assert ["foo1", "foo1"], [post.subject, post.content]
+ end
+
+ test "setting only selected attributes with set_translations" do
+ Post.locale = :de
+ post = Post.create :subject => "foo1", :content => "foo1"
+ Post.locale = :en
+ post.update_attributes( :subject => "bar1", :content => "bar1" )
+
+ options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
+ post.set_translations options
+ post.reload
+
+ assert ["bar2", "bar1"], [post.subject, post.content]
+ Post.locale = :de
+ assert ["foo1", "foo2"], [post.subject, post.content]
+ end
+
+ test "setting invalid attributes raises ArgumentError" do
+ Post.locale = :de
+ post = Post.create :subject => "foo1", :content => "foo1"
+ Post.locale = :en
+ post.update_attributes( :subject => "bar1", :content => "bar1" )
+
+ options = { :de => {:fake => "foo2"} }
+ exception = assert_raise(ActiveRecord::UnknownAttributeError) do
+ post.set_translations options
+ end
+ assert_equal "unknown attribute: fake", exception.message
+ end
end
# TODO should validate_presence_of take fallbacks into account? maybe we need

0 comments on commit 3f7c2f6

Please sign in to comment.