diff --git a/lib/globalize/active_record.rb b/lib/globalize/active_record.rb index 99ad91756..e11086a63 100644 --- a/lib/globalize/active_record.rb +++ b/lib/globalize/active_record.rb @@ -46,8 +46,11 @@ def translates(*attr_names) class_inheritable_accessor :translation_class, :translated_attribute_names class_inheritable_writer :required_attributes - self.translation_class = ActiveRecord.build_translation_class(self, options) - self.translated_attribute_names = attr_names.map(&:to_sym) + + (subclasses_of(self) + [self]).each do |klass| + klass.translation_class = ActiveRecord.build_translation_class(self, options) + klass.translated_attribute_names = attr_names.map(&:to_sym) + end include InstanceMethods extend ClassMethods, Migration diff --git a/test/active_record/sti_translated_test.rb b/test/active_record/sti_translated_test.rb index f529b8d6e..2550b62e4 100644 --- a/test/active_record/sti_translated_test.rb +++ b/test/active_record/sti_translated_test.rb @@ -46,4 +46,16 @@ def setup I18n.locale = 'he-IL' assert_equal 'baz', child.content end + + test "works when STI class is defined before globalize is applied" do + child = EarlyChild.create :content => 'foo' + I18n.locale = 'de-DE' + child.content = 'bar' + child.save + I18n.locale = 'en-US' + child = EarlyChild.first + assert_equal 'foo', child.content + I18n.locale = 'de-DE' + assert_equal 'bar', child.content + end end diff --git a/test/data/models.rb b/test/data/models.rb index 5408d6e23..a3bb51f59 100644 --- a/test/data/models.rb +++ b/test/data/models.rb @@ -17,6 +17,12 @@ class Blog < ActiveRecord::Base has_many :posts, :order => 'id ASC' end +class Parent < ActiveRecord::Base +end + +class EarlyChild < Parent +end + class Parent < ActiveRecord::Base translates :content end @@ -53,4 +59,4 @@ class Validatee < ActiveRecord::Base class User < ActiveRecord::Base translates :name validates_presence_of :name, :email -end +end \ No newline at end of file