From 7c9f0d0823bdce9a011d3bdd2823e15ffb7c1311 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 19 Jul 2009 19:02:31 +0200 Subject: updated globalize2 plugin --- vendor/plugins/globalize2/.gitignore | 4 - vendor/plugins/globalize2/LICENSE | 21 ++++ .../lib/globalize/model/active_record/adapter.rb | 2 +- .../globalize/model/active_record/translated.rb | 23 ++++- .../globalize2/test/backends/chained_test.rb | 1 + vendor/plugins/globalize2/test/data/post.rb | 9 ++ .../test/model/active_record/translated_test.rb | 109 +++++++++++++++++++++ 7 files changed, 160 insertions(+), 9 deletions(-) delete mode 100644 vendor/plugins/globalize2/.gitignore create mode 100644 vendor/plugins/globalize2/LICENSE diff --git a/vendor/plugins/globalize2/.gitignore b/vendor/plugins/globalize2/.gitignore deleted file mode 100644 index 24f36d5..0000000 --- a/vendor/plugins/globalize2/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -doc -spec/spec/db/* -vendor -NOTES \ No newline at end of file diff --git a/vendor/plugins/globalize2/LICENSE b/vendor/plugins/globalize2/LICENSE new file mode 100644 index 0000000..94a6b81 --- /dev/null +++ b/vendor/plugins/globalize2/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2008, 2009 Joshua Harvey + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb index 71faebd..93355b8 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb @@ -73,7 +73,7 @@ module Globalize translations = @record.globalize_translations end result, requested_locale = nil, locale - + # Walk through the fallbacks, starting with the current locale itself, and moving # to the next best choice, until we find a match. # Check the @globalize_set_translations cache first to see if we've just changed the diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb index c7ae9e9..f8fb230 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb @@ -23,10 +23,15 @@ module Globalize include InstanceMethods extend ClassMethods - alias_method_chain :reload, :globalize self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) - has_many :globalize_translations, :class_name => globalize_proxy.name, :extend => Extensions + has_many( + :globalize_translations, + :class_name => globalize_proxy.name, + :extend => Extensions, + :dependent => :delete_all, + :foreign_key => class_name.foreign_key + ) after_save :update_globalize_record end @@ -109,7 +114,7 @@ module Globalize end module InstanceMethods - def reload_with_globalize + def reload(options = nil) globalize.clear # clear all globalized attributes @@ -118,7 +123,7 @@ module Globalize @attributes.delete attr.to_s end - reload_without_globalize + super options end def globalize @@ -132,6 +137,16 @@ module Globalize 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| + + translation = globalize_translations.find_by_locale(key.to_s) || + globalize_translations.build(:locale => key.to_s) + translation.update_attributes!(options[key]) + end + end + end end end diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb index bc45179..bb0b3e0 100644 --- a/vendor/plugins/globalize2/test/backends/chained_test.rb +++ b/vendor/plugins/globalize2/test/backends/chained_test.rb @@ -58,6 +58,7 @@ end class TranslateChainedTest < ActiveSupport::TestCase def setup + I18n.locale = :en I18n.backend = Globalize::Backend::Chain.new @first_backend = I18n::Backend::Simple.new @last_backend = I18n::Backend::Simple.new diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/post.rb index fdfb7c0..6673dc4 100644 --- a/vendor/plugins/globalize2/test/data/post.rb +++ b/vendor/plugins/globalize2/test/data/post.rb @@ -12,4 +12,13 @@ class Parent < ActiveRecord::Base end class Child < Parent +end + +class Comment < ActiveRecord::Base + validates_presence_of :content + belongs_to :post +end + +class TranslatedComment < Comment + translates :content end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb index 6bb7b26..3e9ea00 100644 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb @@ -338,6 +338,115 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal [ :de, :es, :fr ], post.translated_locales assert_equal [ :de, :es, :fr ], Post.first.translated_locales end + + test "including globalize_translations" do + I18n.locale = :de + Post.create :subject => "Foo1", :content => "Bar1" + Post.create :subject => "Foo2", :content => "Bar2" + + class << Post + def tranlsations_included + self.all(:include => :globalize_translations) + end + end + + default = Post.all.map {|x| [x.subject, x.content]} + 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 + + test "reload accepting find options" do + p = Post.create :subject => "Foo", :content => "Bar" + assert p.reload(:readonly => true, :lock => true) + assert_raise(ArgumentError) { p.reload(:foo => :bar) } + end + + test "dependent destroy of translation" do + p = Post.create :subject => "Foo", :content => "Bar" + assert_equal 1, PostTranslation.count + p.destroy + assert_equal 0, PostTranslation.count + end + + test "translating subclass of untranslated comment model" do + translated_comment = TranslatedComment.create(:post => @post) + assert_nothing_raised { translated_comment.globalize_translations } + end + + test "modifiying translated comments works as expected" do + I18n.locale = :en + translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') + assert_equal 'foo', translated_comment.content + + I18n.locale = :de + translated_comment.content = 'bar' + assert translated_comment.save + assert_equal 'bar', translated_comment.content + + I18n.locale = :en + assert_equal 'foo', translated_comment.content + + assert_equal 2, translated_comment.globalize_translations.size + end end # TODO should validate_presence_of take fallbacks into account? maybe we need -- cgit v1.3