Skip to content
Browse files

backport changes from adva_cms

* fix issue with active_support/dependencies class reloading
* do not raise an exception on missing translations in static backend (dubious change)
* add a clear_cache method, only clear the cache on reload (i.e. keep the stash) (dubious change)
* add a translated_attributes method
  • Loading branch information...
1 parent 785e4bc commit 133da77c3c09618caffb41ba0324f81c18aabd53 Clemens Kofler and Sven Fuchs committed with svenfuchs Sep 22, 2009
View
4 init.rb
@@ -1,6 +1,4 @@
-require 'rails_edge_load_path_patch.rb' unless I18n.respond_to?(:load_path)
-
-ActiveRecord::Base.send :include, Globalize::Model::ActiveRecord::Translated
+require 'globalize/model/active_record'
I18n.backend = Globalize::Backend::Static.new
View
3 lib/globalize/backend/static.rb
@@ -20,7 +20,8 @@ def translate(locale, key, options = {})
result ||= default locale, default, options
attrs = {:requested_locale => locale, :locale => fallback, :key => key, :options => options}
- translation(result, attrs) || raise(I18n::MissingTranslationData.new(locale, key, options))
+ translation(result, attrs)
+# translation(result, attrs) || raise(I18n::MissingTranslationData.new(locale, key, options))
end
protected
View
28 lib/globalize/model/active_record/adapter.rb
@@ -4,31 +4,31 @@ class AttributeStash < Hash
def contains?(locale, attr_name)
locale = locale.to_sym
self[locale] ||= {}
- self[locale].has_key? attr_name
+ self[locale].has_key? attr_name
end
-
+
def read(locale, attr_name)
locale = locale.to_sym
self[locale] ||= {}
self[locale][attr_name]
end
-
+
def write(locale, attr_name, value)
locale = locale.to_sym
self[locale] ||= {}
self[locale][attr_name] = value
end
end
-
+
class Adapter
def initialize(record)
@record = record
-
+
# TODO what exactly are the roles of cache and stash
@cache = AttributeStash.new
@stash = AttributeStash.new
end
-
+
def fetch(locale, attr_name)
# locale = I18n.locale
is_cached = @cache.contains?(locale, attr_name)
@@ -38,12 +38,12 @@ def fetch(locale, attr_name)
value
end
end
-
+
def stash(locale, attr_name, value)
@stash.write locale, attr_name, value
@cache.write locale, attr_name, value
end
-
+
def update_translations!
@stash.each do |locale, attrs|
translation = @record.globalize_translations.find_or_initialize_by_locale(locale.to_s)
@@ -52,15 +52,19 @@ def update_translations!
end
@stash.clear
end
-
+
# Clears the cache
def clear
@cache.clear
@stash.clear
end
+
+ def clear_cache
+ @cache.clear
+ end
private
-
+
def fetch_attribute(locale, attr_name)
fallbacks = I18n.fallbacks[locale].map{|tag| tag.to_s}.map(&:to_sym)
@@ -76,11 +80,11 @@ def fetch_attribute(locale, attr_name)
# 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
+ # Check the @globalize_set_translations cache first to see if we've just changed the
# attribute and not saved yet.
fallbacks.each do |fallback|
# TODO should we be checking stash or just cache?
- result = @stash.read(fallback, attr_name) || begin
+ result = @cache.read(fallback, attr_name) || begin
translation = translations.detect {|tr| tr.locale == fallback }
translation && translation.send(attr_name)
end
View
12 lib/globalize/model/active_record/translated.rb
@@ -129,7 +129,7 @@ def i18n_attr(attribute_name)
module InstanceMethods
def reload(options = nil)
- globalize.clear
+ globalize.clear_cache
# clear all globalized attributes
# TODO what's the best way to handle this?
@@ -140,6 +140,10 @@ def reload(options = nil)
super(options)
end
+ def translated_attributes
+ self.class.globalize_options[:translated_attributes].inject({}) {|h, tf| h[tf] = send(tf); h }
+ end
+
def globalize
@globalize ||= Adapter.new self
end
@@ -154,7 +158,7 @@ def translated_locales
end
end
- def set_translations options
+ 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)
@@ -165,4 +169,6 @@ def set_translations options
end
end
end
-end
+end
+
+ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
View
20 test/backends/static_test.rb
@@ -9,10 +9,10 @@
class StaticTest < ActiveSupport::TestCase
def setup
I18n.backend = Globalize::Backend::Static.new
- translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}},
- :"en" => {:bar => "bar in en"},
- :"de-DE" => {:baz => "baz in de-DE"},
- :"de" => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '', :format => '%n %u'}}}}}
+ translations = { :"en-US" => { :foo => "foo in en-US", :boz => 'boz', :buz => { :bum => 'bum' } },
+ :"en" => { :bar => "bar in en", :skip_last_comma => "false" },
+ :"de-DE" => { :baz => "baz in de-DE" },
+ :"de" => { :boo => "boo in de", :number => { :currency => { :format => { :unit => '', :format => '%n %u' } } } } }
translations.each do |locale, data|
I18n.backend.store_translations locale, data
end
@@ -59,7 +59,7 @@ def setup
test "returns the fallback translation for the key if present for a fallback locale" do
I18n.backend.store_translations :de, :non_default => "non_default in de"
assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE")
- end
+ end
test "returns an array of translations" do
assert_instance_of Array, I18n.translate([:foo, :boz])
@@ -92,9 +92,13 @@ def setup
assert_equal "10.00 €", currency
end
- test "makes sure interpolation does not break even with False as string" do
- assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma")
- end
+ # test "makes sure interpolation does not break even with False as string" do
+ # result = ''
+ # assert_nothing_raised do
+ # result = I18n.t(:missing, :default => "{{value}}", :value => false)
+ # end
+ # assert_equal "false", result
+ # end
end
class TranslationStaticTest < ActiveSupport::TestCase
View
12 test/data/models.rb
@@ -18,12 +18,20 @@ class Comment < ActiveRecord::Base
validates_presence_of :content
belongs_to :post
end
-
+
class TranslatedComment < Comment
translates :content
end
class UltraLongModelNameWithoutProper < ActiveRecord::Base
translates :subject, :content
validates_presence_of :subject
-end
+end
+
+class Reloader < Parent
+ after_create :do_reload
+
+ def do_reload
+ reload
+ end
+end
View
28 test/model/active_record/migration_test.rb
@@ -74,31 +74,29 @@ def setup
Blog.drop_translation_table!
end
end
-
+
test "translation_index_name returns a readable index name when it's not longer than 50 characters" do
assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name)
end
-
+
test "translation_index_name returns a hashed index name when it's longer than 50 characters" do
class UltraLongModelNameWithoutProper < ActiveRecord::Base
translates :foo
end
- expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd'
- actual = UltraLongModelNameWithoutProper.send(:translation_index_name)
-
- assert_equal expected, actual
+ name = UltraLongModelNameWithoutProper.send(:translation_index_name)
+ assert_match /^index_[a-z0-9]{40}$/, name
end
-
+
test 'globalize table added when table has long name' do
UltraLongModelNameWithoutProper.create_translation_table!(
:subject => :string, :content => :text
)
-
+
assert UltraLongModelNameWithoutProper.connection.table_exists?(
:ultra_long_model_name_without_proper_translations
)
- assert UltraLongModelNameWithoutProper.connection.index_exists?(
- :ultra_long_model_name_without_proper_translations,
+ assert UltraLongModelNameWithoutProper.connection.index_exists?(
+ :ultra_long_model_name_without_proper_translations,
:name => UltraLongModelNameWithoutProper.send(
:translation_index_name
)
@@ -109,17 +107,17 @@ class UltraLongModelNameWithoutProper < ActiveRecord::Base
UltraLongModelNameWithoutProper.create_translation_table!(
:subject => :string, :content => :text
)
-
+
UltraLongModelNameWithoutProper.drop_translation_table!
-
+
assert !UltraLongModelNameWithoutProper.connection.table_exists?(
:ultra_long_model_name_without_proper_translations
)
-
+
assert !UltraLongModelNameWithoutProper.connection.index_exists?(
- :ultra_long_model_name_without_proper_translations,
+ :ultra_long_model_name_without_proper_translations,
:ultra_long_model_name_without_proper_id
)
end
-end
+end
View
5 test/model/active_record/translated_test.rb
@@ -338,6 +338,11 @@ def teardown
assert_equal [ :de, :es, :fr ], post.translated_locales
assert_equal [ :de, :es, :fr ], Post.first.translated_locales
end
+
+ test "saving record correctly after post-save reload" do
+ reloader = Reloader.create :content => 'foo'
+ assert_equal 'foo', reloader.content
+ end
test "including globalize_translations" do
I18n.locale = :de

0 comments on commit 133da77

Please sign in to comment.
Something went wrong with that request. Please try again.