summaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-07-19 19:02:31 +0200
committerhukl <contact@smyck.org>2009-07-19 19:02:31 +0200
commit7c9f0d0823bdce9a011d3bdd2823e15ffb7c1311 (patch)
treeb82854becc5437517180aa6406a0baebdad7e71b /vendor
parent0d8879354fbd251e4509a0ab5c6b6e47aa3fa47d (diff)
updated globalize2 plugin
Diffstat (limited to 'vendor')
-rw-r--r--vendor/plugins/globalize2/.gitignore4
-rw-r--r--vendor/plugins/globalize2/LICENSE21
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb2
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb23
-rw-r--r--vendor/plugins/globalize2/test/backends/chained_test.rb1
-rw-r--r--vendor/plugins/globalize2/test/data/post.rb9
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb109
7 files changed, 160 insertions, 9 deletions
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 @@
1doc
2spec/spec/db/*
3vendor
4NOTES \ 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 @@
1The MIT License
2
3Copyright (c) 2008, 2009 Joshua Harvey
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE 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
73 translations = @record.globalize_translations 73 translations = @record.globalize_translations
74 end 74 end
75 result, requested_locale = nil, locale 75 result, requested_locale = nil, locale
76 76
77 # Walk through the fallbacks, starting with the current locale itself, and moving 77 # Walk through the fallbacks, starting with the current locale itself, and moving
78 # to the next best choice, until we find a match. 78 # to the next best choice, until we find a match.
79 # Check the @globalize_set_translations cache first to see if we've just changed the 79 # 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
23 23
24 include InstanceMethods 24 include InstanceMethods
25 extend ClassMethods 25 extend ClassMethods
26 alias_method_chain :reload, :globalize
27 26
28 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) 27 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self)
29 has_many :globalize_translations, :class_name => globalize_proxy.name, :extend => Extensions 28 has_many(
29 :globalize_translations,
30 :class_name => globalize_proxy.name,
31 :extend => Extensions,
32 :dependent => :delete_all,
33 :foreign_key => class_name.foreign_key
34 )
30 35
31 after_save :update_globalize_record 36 after_save :update_globalize_record
32 end 37 end
@@ -109,7 +114,7 @@ module Globalize
109 end 114 end
110 115
111 module InstanceMethods 116 module InstanceMethods
112 def reload_with_globalize 117 def reload(options = nil)
113 globalize.clear 118 globalize.clear
114 119
115 # clear all globalized attributes 120 # clear all globalized attributes
@@ -118,7 +123,7 @@ module Globalize
118 @attributes.delete attr.to_s 123 @attributes.delete attr.to_s
119 end 124 end
120 125
121 reload_without_globalize 126 super options
122 end 127 end
123 128
124 def globalize 129 def globalize
@@ -132,6 +137,16 @@ module Globalize
132 def translated_locales 137 def translated_locales
133 globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym } 138 globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym }
134 end 139 end
140
141 def set_translations options
142 options.keys.each do |key|
143
144 translation = globalize_translations.find_by_locale(key.to_s) ||
145 globalize_translations.build(:locale => key.to_s)
146 translation.update_attributes!(options[key])
147 end
148 end
149
135 end 150 end
136 end 151 end
137 end 152 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
58 58
59class TranslateChainedTest < ActiveSupport::TestCase 59class TranslateChainedTest < ActiveSupport::TestCase
60 def setup 60 def setup
61 I18n.locale = :en
61 I18n.backend = Globalize::Backend::Chain.new 62 I18n.backend = Globalize::Backend::Chain.new
62 @first_backend = I18n::Backend::Simple.new 63 @first_backend = I18n::Backend::Simple.new
63 @last_backend = I18n::Backend::Simple.new 64 @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
12end 12end
13 13
14class Child < Parent 14class Child < Parent
15end
16
17class Comment < ActiveRecord::Base
18 validates_presence_of :content
19 belongs_to :post
20end
21
22class TranslatedComment < Comment
23 translates :content
15end \ No newline at end of file 24end \ 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
338 assert_equal [ :de, :es, :fr ], post.translated_locales 338 assert_equal [ :de, :es, :fr ], post.translated_locales
339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales 339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales
340 end 340 end
341
342 test "including globalize_translations" do
343 I18n.locale = :de
344 Post.create :subject => "Foo1", :content => "Bar1"
345 Post.create :subject => "Foo2", :content => "Bar2"
346
347 class << Post
348 def tranlsations_included
349 self.all(:include => :globalize_translations)
350 end
351 end
352
353 default = Post.all.map {|x| [x.subject, x.content]}
354 with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
355 assert_equal default, with_include
356 end
357
358 test "setting multiple translations at once with options hash" do
359 Post.locale = :de
360 post = Post.create :subject => "foo1", :content => "foo1"
361 Post.locale = :en
362 post.update_attributes( :subject => "bar1", :content => "bar1" )
363
364 options = { :de => {:subject => "foo2", :content => "foo2"},
365 :en => {:subject => "bar2", :content => "bar2"} }
366 post.set_translations options
367 post.reload
368
369 assert ["bar2", "bar2"], [post.subject, post.content]
370 Post.locale = :de
371 assert ["foo2", "foo2"], [post.subject, post.content]
372 end
373
374 test "setting only one translation with set_translations" do
375 Post.locale = :de
376 post = Post.create :subject => "foo1", :content => "foo1"
377 Post.locale = :en
378 post.update_attributes( :subject => "bar1", :content => "bar1" )
379
380 options = { :en => {:subject => "bar2", :content => "bar2"} }
381 post.set_translations options
382 post.reload
383
384 assert ["bar2", "bar2"], [post.subject, post.content]
385 Post.locale = :de
386 assert ["foo1", "foo1"], [post.subject, post.content]
387 end
388
389 test "setting only selected attributes with set_translations" do
390 Post.locale = :de
391 post = Post.create :subject => "foo1", :content => "foo1"
392 Post.locale = :en
393 post.update_attributes( :subject => "bar1", :content => "bar1" )
394
395 options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
396 post.set_translations options
397 post.reload
398
399 assert ["bar2", "bar1"], [post.subject, post.content]
400 Post.locale = :de
401 assert ["foo1", "foo2"], [post.subject, post.content]
402 end
403
404 test "setting invalid attributes raises ArgumentError" do
405 Post.locale = :de
406 post = Post.create :subject => "foo1", :content => "foo1"
407 Post.locale = :en
408 post.update_attributes( :subject => "bar1", :content => "bar1" )
409
410 options = { :de => {:fake => "foo2"} }
411 exception = assert_raise(ActiveRecord::UnknownAttributeError) do
412 post.set_translations options
413 end
414 assert_equal "unknown attribute: fake", exception.message
415 end
416
417 test "reload accepting find options" do
418 p = Post.create :subject => "Foo", :content => "Bar"
419 assert p.reload(:readonly => true, :lock => true)
420 assert_raise(ArgumentError) { p.reload(:foo => :bar) }
421 end
422
423 test "dependent destroy of translation" do
424 p = Post.create :subject => "Foo", :content => "Bar"
425 assert_equal 1, PostTranslation.count
426 p.destroy
427 assert_equal 0, PostTranslation.count
428 end
429
430 test "translating subclass of untranslated comment model" do
431 translated_comment = TranslatedComment.create(:post => @post)
432 assert_nothing_raised { translated_comment.globalize_translations }
433 end
434
435 test "modifiying translated comments works as expected" do
436 I18n.locale = :en
437 translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
438 assert_equal 'foo', translated_comment.content
439
440 I18n.locale = :de
441 translated_comment.content = 'bar'
442 assert translated_comment.save
443 assert_equal 'bar', translated_comment.content
444
445 I18n.locale = :en
446 assert_equal 'foo', translated_comment.content
447
448 assert_equal 2, translated_comment.globalize_translations.size
449 end
341end 450end
342 451
343# TODO should validate_presence_of take fallbacks into account? maybe we need 452# TODO should validate_presence_of take fallbacks into account? maybe we need