Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix behaviour that occurs when you translate your models after you define a STI child #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/globalize/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/active_record/sti_translated_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion test/data/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,4 +59,4 @@ class Validatee < ActiveRecord::Base
class User < ActiveRecord::Base
translates :name
validates_presence_of :name, :email
end
end