summaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-02-25 22:37:22 +0100
committerhukl <contact@smyck.org>2009-02-25 22:37:22 +0100
commitf4c73763e5b3d65f3377fb1238a94122a1c272a4 (patch)
tree8f6b276f9432e31ae0fe7bffe010e680081a1e0a /vendor
parent19b07ff63c626f76ad512b95ddbf16cddd2bf855 (diff)
updated globalize2
Diffstat (limited to 'vendor')
-rw-r--r--vendor/plugins/globalize2/lib/globalize/backend/static.rb3
-rw-r--r--vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb27
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record.rb6
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb13
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb48
-rw-r--r--vendor/plugins/globalize2/test/data/post.rb7
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/migration_test.rb2
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb75
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb85
-rw-r--r--vendor/plugins/globalize2/test/test_helper.rb4
10 files changed, 251 insertions, 19 deletions
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/static.rb b/vendor/plugins/globalize2/lib/globalize/backend/static.rb
index 3903517..fb9e1fe 100644
--- a/vendor/plugins/globalize2/lib/globalize/backend/static.rb
+++ b/vendor/plugins/globalize2/lib/globalize/backend/static.rb
@@ -51,7 +51,8 @@ module Globalize
51 translation(value, meta) 51 translation(value, meta)
52 end 52 end
53 else 53 else
54 raise "unexpected translation type: #{result.inspect}" 54 result
55 # raise "unexpected translation type: #{result.inspect}"
55 end 56 end
56 end 57 end
57 end 58 end
diff --git a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb b/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb
new file mode 100644
index 0000000..e32be28
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb
@@ -0,0 +1,27 @@
1# A simple exception handler that behaves like the default exception handler
2# but also raises on missing translations.
3#
4# Useful for identifying missing translations during testing.
5#
6# E.g.
7#
8# require 'globalize/i18n/missing_translations_raise_handler
9# I18n.exception_handler = :missing_translations_raise_handler
10module I18n
11 class << self
12 def missing_translations_raise_handler(exception, locale, key, options)
13 raise exception
14 end
15 end
16
17# self.exception_handler = :missing_translations_raise_handler
18end
19
20I18n.exception_handler = :missing_translations_raise_handler
21
22ActionView::Helpers::TranslationHelper.module_eval do
23 def translate(key, options = {})
24 I18n.translate(key, options)
25 end
26 alias :t :translate
27end
diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb
index 450729c..9645842 100644
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb
+++ b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb
@@ -6,7 +6,7 @@ require 'globalize/model/active_record/translated'
6module Globalize 6module Globalize
7 module Model 7 module Model
8 module ActiveRecord 8 module ActiveRecord
9 class << self 9 class << self
10 def create_proxy_class(klass) 10 def create_proxy_class(klass)
11 Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ 11 Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){
12 belongs_to "#{klass.name.underscore}".intern 12 belongs_to "#{klass.name.underscore}".intern
@@ -24,10 +24,10 @@ module Globalize
24 def define_accessors(klass, attr_names) 24 def define_accessors(klass, attr_names)
25 attr_names.each do |attr_name| 25 attr_names.each do |attr_name|
26 klass.send :define_method, attr_name, lambda { 26 klass.send :define_method, attr_name, lambda {
27 globalize.fetch I18n.locale, attr_name 27 globalize.fetch self.class.locale, attr_name
28 } 28 }
29 klass.send :define_method, "#{attr_name}=", lambda {|val| 29 klass.send :define_method, "#{attr_name}=", lambda {|val|
30 globalize.stash I18n.locale, attr_name, val 30 globalize.stash self.class.locale, attr_name, val
31 self[attr_name] = val 31 self[attr_name] = val
32 } 32 }
33 end 33 end
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 12d7564..d1c8db8 100644
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb
+++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb
@@ -1,6 +1,12 @@
1module Globalize 1module Globalize
2 module Model 2 module Model
3 class AttributeStash < Hash 3 class AttributeStash < Hash
4 def contains?(locale, attr_name)
5 locale = locale.to_sym
6 self[locale] ||= {}
7 self[locale].has_key? attr_name
8 end
9
4 def read(locale, attr_name) 10 def read(locale, attr_name)
5 locale = locale.to_sym 11 locale = locale.to_sym
6 self[locale] ||= {} 12 self[locale] ||= {}
@@ -17,13 +23,16 @@ module Globalize
17 class Adapter 23 class Adapter
18 def initialize(record) 24 def initialize(record)
19 @record = record 25 @record = record
26
27 # TODO what exactly are the roles of cache and stash
20 @cache = AttributeStash.new 28 @cache = AttributeStash.new
21 @stash = AttributeStash.new 29 @stash = AttributeStash.new
22 end 30 end
23 31
24 def fetch(locale, attr_name) 32 def fetch(locale, attr_name)
25 # locale = I18n.locale 33 # locale = I18n.locale
26 @cache.read(locale, attr_name) || begin 34 is_cached = @cache.contains?(locale, attr_name)
35 is_cached ? @cache.read(locale, attr_name) : begin
27 value = fetch_attribute locale, attr_name 36 value = fetch_attribute locale, attr_name
28 @cache.write locale, attr_name, value if value && value.locale == locale 37 @cache.write locale, attr_name, value if value && value.locale == locale
29 value 38 value
@@ -47,6 +56,7 @@ module Globalize
47 # Clears the cache 56 # Clears the cache
48 def clear 57 def clear
49 @cache.clear 58 @cache.clear
59 @stash.clear
50 end 60 end
51 61
52 private 62 private
@@ -61,6 +71,7 @@ module Globalize
61 # Check the @globalize_set_translations cache first to see if we've just changed the 71 # Check the @globalize_set_translations cache first to see if we've just changed the
62 # attribute and not saved yet. 72 # attribute and not saved yet.
63 fallbacks.each do |fallback| 73 fallbacks.each do |fallback|
74 # TODO should we be checking stash or just cache?
64 result = @stash.read(fallback, attr_name) || begin 75 result = @stash.read(fallback, attr_name) || begin
65 translation = translations.detect {|tr| tr.locale == fallback } 76 translation = translations.detect {|tr| tr.locale == fallback }
66 translation && translation.send(attr_name) 77 translation && translation.send(attr_name)
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 710cde5..c7ae9e9 100644
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb
+++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb
@@ -19,18 +19,16 @@ module Globalize
19 19
20 # Only set up once per class 20 # Only set up once per class
21 unless included_modules.include? InstanceMethods 21 unless included_modules.include? InstanceMethods
22 class_inheritable_accessor :globalize_options 22 class_inheritable_accessor :globalize_options, :globalize_proxy
23
23 include InstanceMethods 24 include InstanceMethods
24 extend ClassMethods 25 extend ClassMethods
26 alias_method_chain :reload, :globalize
25 27
26 proxy_class = Globalize::Model::ActiveRecord.create_proxy_class(self) 28 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self)
27 has_many :globalize_translations, :class_name => proxy_class.name, :extend => Extensions 29 has_many :globalize_translations, :class_name => globalize_proxy.name, :extend => Extensions
28 30
29 after_save :update_globalize_record 31 after_save :update_globalize_record
30
31 def i18n_attr(attribute_name)
32 self.name.underscore + "_translations.#{attribute_name}"
33 end
34 end 32 end
35 33
36 self.globalize_options = options 34 self.globalize_options = options
@@ -41,6 +39,14 @@ module Globalize
41 extend Callbacks 39 extend Callbacks
42 Callbacks.instance_methods.each {|cb| send cb } 40 Callbacks.instance_methods.each {|cb| send cb }
43 end 41 end
42
43 def locale=(locale)
44 @@locale = locale
45 end
46
47 def locale
48 (defined?(@@locale) && @@locale) || I18n.locale
49 end
44 end 50 end
45 51
46 # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here 52 # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here
@@ -59,13 +65,13 @@ module Globalize
59 def method_missing(method, *args) 65 def method_missing(method, *args)
60 if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) 66 if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym)
61 find(:first, :joins => :globalize_translations, 67 find(:first, :joins => :globalize_translations,
62 :conditions => [i18n_attr($1)+" = ? AND "+i18n_attr('locale')+" IN (?)", 68 :conditions => [ "#{i18n_attr($1)} = ? AND #{i18n_attr('locale')} IN (?)",
63 args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}]) 69 args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}])
64 else 70 else
65 super 71 super
66 end 72 end
67 end 73 end
68 74
69 def create_translation_table!(fields) 75 def create_translation_table!(fields)
70 translated_fields = self.globalize_options[:translated_attributes] 76 translated_fields = self.globalize_options[:translated_attributes]
71 translated_fields.each do |f| 77 translated_fields.each do |f|
@@ -94,9 +100,27 @@ module Globalize
94 translation_table_name = self.name.underscore + '_translations' 100 translation_table_name = self.name.underscore + '_translations'
95 self.connection.drop_table translation_table_name 101 self.connection.drop_table translation_table_name
96 end 102 end
103
104 private
105
106 def i18n_attr(attribute_name)
107 self.base_class.name.underscore + "_translations.#{attribute_name}"
108 end
97 end 109 end
98 110
99 module InstanceMethods 111 module InstanceMethods
112 def reload_with_globalize
113 globalize.clear
114
115 # clear all globalized attributes
116 # TODO what's the best way to handle this?
117 self.class.globalize_options[:translated_attributes].each do |attr|
118 @attributes.delete attr.to_s
119 end
120
121 reload_without_globalize
122 end
123
100 def globalize 124 def globalize
101 @globalize ||= Adapter.new self 125 @globalize ||= Adapter.new self
102 end 126 end
@@ -104,6 +128,10 @@ module Globalize
104 def update_globalize_record 128 def update_globalize_record
105 globalize.update_translations! 129 globalize.update_translations!
106 end 130 end
131
132 def translated_locales
133 globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym }
134 end
107 end 135 end
108 end 136 end
109 end 137 end
diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/post.rb
index 20a2495..fdfb7c0 100644
--- a/vendor/plugins/globalize2/test/data/post.rb
+++ b/vendor/plugins/globalize2/test/data/post.rb
@@ -6,3 +6,10 @@ end
6class Blog < ActiveRecord::Base 6class Blog < ActiveRecord::Base
7 has_many :posts, :order => 'id ASC' 7 has_many :posts, :order => 'id ASC'
8end 8end
9
10class Parent < ActiveRecord::Base
11 translates :content
12end
13
14class Child < Parent
15end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
index 09804c7..c539fb6 100644
--- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
+++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
@@ -10,7 +10,7 @@ require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10 10
11class MigrationTest < ActiveSupport::TestCase 11class MigrationTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 reset_db! 'no_globalize_schema' 13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb'))
14 end 14 end
15 15
16 test 'globalize table added' do 16 test 'globalize table added' do
diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb
new file mode 100644
index 0000000..14d7d0f
--- /dev/null
+++ b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb
@@ -0,0 +1,75 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2require 'active_record'
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10
11class StiTranslatedTest < ActiveSupport::TestCase
12 def setup
13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 end
17
18 def teardown
19 I18n.fallbacks.clear
20 end
21
22 test "works with simple dynamic finders" do
23 foo = Child.create :content => 'foo'
24 Child.create :content => 'bar'
25 child = Child.find_by_content('foo')
26 assert_equal foo, child
27 end
28
29 test 'change attribute on globalized model' do
30 child = Child.create :content => 'foo'
31 assert_equal [], child.changed
32 child.content = 'bar'
33 assert_equal [ 'content' ], child.changed
34 child.content = 'baz'
35 assert_member 'content', child.changed
36 end
37
38 test 'change attribute on globalized model after locale switching' do
39 child = Child.create :content => 'foo'
40 assert_equal [], child.changed
41 child.content = 'bar'
42 I18n.locale = :de
43 assert_equal [ 'content' ], child.changed
44 end
45
46 test 'fallbacks with lots of locale switching' do
47 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
48 child = Child.create :content => 'foo'
49
50 I18n.locale = :'de-DE'
51 assert_equal 'foo', child.content
52
53 I18n.locale = :'en-US'
54 child.update_attribute :content, 'bar'
55
56 I18n.locale = :'de-DE'
57 assert_equal 'bar', child.content
58 end
59
60 test "saves all locales, even after locale switching" do
61 child = Child.new :content => 'foo'
62 I18n.locale = 'de-DE'
63 child.content = 'bar'
64 I18n.locale = 'he-IL'
65 child.content = 'baz'
66 child.save
67 I18n.locale = 'en-US'
68 child = Child.first
69 assert_equal 'foo', child.content
70 I18n.locale = 'de-DE'
71 assert_equal 'bar', child.content
72 I18n.locale = 'he-IL'
73 assert_equal 'baz', child.content
74 end
75end \ 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 ec507e0..6bb7b26 100644
--- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
+++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
@@ -12,7 +12,8 @@ class TranslatedTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 I18n.locale = :'en-US' 13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear 14 I18n.fallbacks.clear
15 reset_db! 15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 ActiveRecord::Base.locale = nil
16 end 17 end
17 18
18 def teardown 19 def teardown
@@ -92,6 +93,13 @@ class TranslatedTest < ActiveSupport::TestCase
92 assert_equal 'baz', Post.first.subject 93 assert_equal 'baz', Post.first.subject
93 end 94 end
94 95
96 test "update_attributes failure" do
97 post = Post.create :subject => 'foo', :content => 'bar'
98 assert !post.update_attributes( { :subject => '' } )
99 assert_nil post.reload.attributes['subject']
100 assert_equal 'foo', post.subject
101 end
102
95 test "validates presence of :subject" do 103 test "validates presence of :subject" do
96 post = Post.new 104 post = Post.new
97 assert !post.save 105 assert !post.save
@@ -255,6 +263,81 @@ class TranslatedTest < ActiveSupport::TestCase
255 I18n.locale = :'de-DE' 263 I18n.locale = :'de-DE'
256 assert_equal 'bar', post.subject 264 assert_equal 'bar', post.subject
257 end 265 end
266
267 test 'reload' do
268 post = Post.create :subject => 'foo', :content => 'bar'
269 post.subject = 'baz'
270 assert_equal 'foo', post.reload.subject
271 end
272
273 test 'complex writing and stashing' do
274 post = Post.create :subject => 'foo', :content => 'bar'
275 post.subject = nil
276 assert_nil post.subject
277 assert !post.valid?
278 end
279
280 test 'translated class locale setting' do
281 assert ActiveRecord::Base.respond_to?(:locale)
282 assert_equal :'en-US', I18n.locale
283 assert_equal :'en-US', ActiveRecord::Base.locale
284 I18n.locale = :de
285 assert_equal :de, I18n.locale
286 assert_equal :de, ActiveRecord::Base.locale
287 ActiveRecord::Base.locale = :es
288 assert_equal :de, I18n.locale
289 assert_equal :es, ActiveRecord::Base.locale
290 I18n.locale = :fr
291 assert_equal :fr, I18n.locale
292 assert_equal :es, ActiveRecord::Base.locale
293 end
294
295 test "untranslated class responds to locale" do
296 assert Blog.respond_to?(:locale)
297 end
298
299 test "to ensure locales in different classes are the same" do
300 ActiveRecord::Base.locale = :de
301 assert_equal :de, ActiveRecord::Base.locale
302 assert_equal :de, Parent.locale
303 Parent.locale = :es
304 assert_equal :es, ActiveRecord::Base.locale
305 assert_equal :es, Parent.locale
306 end
307
308 test "attribute saving goes by content locale and not global locale" do
309 ActiveRecord::Base.locale = :de
310 assert_equal :'en-US', I18n.locale
311 Post.create :subject => 'foo'
312 assert_equal :de, Post.first.globalize_translations.first.locale
313 end
314
315 test "attribute loading goes by content locale and not global locale" do
316 post = Post.create :subject => 'foo'
317 assert_equal :'en-US', ActiveRecord::Base.locale
318 ActiveRecord::Base.locale = :de
319 assert_equal :'en-US', I18n.locale
320 post.update_attribute :subject, 'foo [de]'
321 assert_equal 'foo [de]', Post.first.subject
322 ActiveRecord::Base.locale = :'en-US'
323 assert_equal 'foo', Post.first.subject
324 end
325
326 test "access content locale before setting" do
327 Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)"
328 assert_nothing_raised { ActiveRecord::Base.locale }
329 end
330
331 test "translated_locales" do
332 Post.locale = :de
333 post = Post.create :subject => 'foo'
334 Post.locale = :es
335 post.update_attribute :subject, 'bar'
336 Post.locale = :fr
337 post.update_attribute :subject, 'baz'
338 assert_equal [ :de, :es, :fr ], post.translated_locales
339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales
340 end
258end 341end
259 342
260# TODO should validate_presence_of take fallbacks into account? maybe we need 343# TODO should validate_presence_of take fallbacks into account? maybe we need
diff --git a/vendor/plugins/globalize2/test/test_helper.rb b/vendor/plugins/globalize2/test/test_helper.rb
index 58b047d..956b352 100644
--- a/vendor/plugins/globalize2/test/test_helper.rb
+++ b/vendor/plugins/globalize2/test/test_helper.rb
@@ -7,14 +7,14 @@ require 'mocha'
7$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) 7$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
8 8
9class ActiveSupport::TestCase 9class ActiveSupport::TestCase
10 def reset_db!( schema = 'schema' ) 10 def reset_db!( schema_path )
11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine 11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine
12 ::ActiveRecord::Base.establish_connection({ 12 ::ActiveRecord::Base.establish_connection({
13 :adapter => 'sqlite3', 13 :adapter => 'sqlite3',
14 :dbfile => ':memory:' 14 :dbfile => ':memory:'
15 }) 15 })
16 ::ActiveRecord::Base.silence do 16 ::ActiveRecord::Base.silence do
17 load File.expand_path(File.join(File.dirname(__FILE__), 'data', schema + '.rb')) 17 load schema_path
18 end 18 end
19 end 19 end
20 20