Permalink
Browse files

Fixed update to pass all tests (or updated test for new situation)

Removed check_default_language and added test for default_locale when adding values to the record
  • Loading branch information...
1 parent a1fc094 commit 2d16af19794524baccca7b315850202f84671346 @veger committed Dec 30, 2009
Showing with 38 additions and 21 deletions.
  1. +9 −15 lib/globalize/active_record.rb
  2. +7 −1 test/active_record/translates_test.rb
  3. +18 −4 test/active_record_test.rb
  4. +4 −1 test/data/schema.rb
@@ -51,7 +51,6 @@ def translates(*attr_names)
include InstanceMethods
extend ClassMethods, Migration
- before_save :check_default_language
after_save :save_translations!
has_many :translations, :class_name => translation_class.name,
:foreign_key => class_name.foreign_key,
@@ -125,13 +124,18 @@ def method_missing(method, *args)
def find_first_by_translated_attr_and_locales(name, value)
query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
locales = Globalize.fallbacks(locale || I18n.locale).map(&:to_s)
- find(:first, :joins => :translations, :conditions => [query, value, locales])
+ result = find(:first, :joins => :translations, :conditions => [query, value, locales])
+ result = find(:first, :conditions => [ "`#{name}` = ?", value]) if !result
+ result
end
def translated_attr_accessor(name)
define_method "#{name}=", lambda { |value|
+ # Inform ActiveRecord::Dirty module about the change
+ send("#{name}_will_change!") if @attributes.has_key?(name.to_s)
+
globalize.write(self.class.locale || I18n.locale, name, value)
- self[name] = value
+ self[name] = value if (self.class.locale || I18n.locale) == I18n.default_locale
}
define_method name, lambda { |*args|
globalize.fetch(args.first || self.class.locale || I18n.locale, name) || self[name]
@@ -162,7 +166,8 @@ def available_locales
end
def translated_locales
- translations.map(&:locale)
+ # The original language is also a translation according to the original definitions of the plugin
+ translations.map(&:locale) << I18n.default_locale
end
def translated_attributes
@@ -186,17 +191,6 @@ def reload(options = nil)
end
protected
-
- def check_default_language
- if I18n.locale != I18n.default_locale
- # Rollback the default texts (since we are storing translated texts)
- translated_attribute_names.each do |name|
- # Only rollback the translated fields
- self[name] = changes[name.to_s][0].to_s
- end
- end
- end
-
def save_translations!
globalize.save_translations!
end
@@ -40,7 +40,13 @@ def setup
test 'defines a scope for retrieving locales that have complete translations' do
post = Post.create!(:subject => 'subject', :content => 'content')
- assert_equal [:en], post.translated_locales
+
+ Post.locale = :de
+ post.subject = 'Titel'
+ post.content = 'Inhalt'
+ post.save!
+
+ assert_equal [:de, :en], post.translated_locales
end
test 'sets the given attributes to translated_attribute_names' do
View
@@ -23,10 +23,13 @@ def assert_translated(locale, record, names, expected)
post = Post.create(:subject => 'title')
I18n.locale = :de
post.update_attributes(:subject => 'Titel')
+ I18n.locale = :he
+ post.update_attributes(:subject => 'foo')
+ # Translations table should contain translations for :de and :he and not for :en
assert_equal 2, post.translations.size
- assert_equal %w(de en), post.translations.map(&:locale).map(&:to_s).sort
- assert_equal %w(Titel title), post.translations.map(&:subject).sort
+ assert_equal %w(de he), post.translations.map(&:locale).map(&:to_s).sort
+ assert_equal %w(Titel foo), post.translations.map(&:subject).sort
end
test "a translated record has German translations" do
@@ -59,13 +62,15 @@ def assert_translated(locale, record, names, expected)
assert_equal 'foo', value
end
- test "update_attribute succeeds with valid values" do
+ test "update_attribute succeeds with valid values for translated fields" do
+ I18n.locale = :de
post = Post.create(:subject => 'foo', :content => 'bar')
post.update_attribute(:subject, 'baz')
assert_equal 'baz', Post.first.subject
end
- test "update_attributes fails with invalid values" do
+ test "update_attributes fails with invalid values for translates fields" do
+ I18n.locale = :de
post = Post.create(:subject => 'foo', :content => 'bar')
assert !post.update_attributes(:subject => '')
assert_nil post.reload.attributes['subject']
@@ -377,6 +382,10 @@ def translations_included
test "dependent destroy of translation" do
p = Post.create(:subject => "Foo", :content => "Bar")
+ I18n.locale = :de
+ p.subject = "[de] foo"
+ p.content = "[de] bar"
+ p.save!
assert_equal 1, PostTranslation.count
p.destroy
assert_equal 0, PostTranslation.count
@@ -397,6 +406,11 @@ def translations_included
assert translated_comment.save
assert_equal 'bar', translated_comment.content
+ I18n.locale = :he
+ translated_comment.content = 'baz'
+ assert translated_comment.save
+ assert_equal 'baz', translated_comment.content
+
I18n.locale = :en
assert_equal 'foo', translated_comment.content
View
@@ -4,7 +4,9 @@
end
create_table :posts, :force => true do |t|
- t.references :blog
+ t.string :subject
+ t.text :content
+ t.references :blog
end
create_table :post_translations, :force => true do |t|
@@ -15,6 +17,7 @@
end
create_table :parents, :force => true do |t|
+ t.string :content
end
create_table :parent_translations, :force => true do |t|

0 comments on commit 2d16af1

Please sign in to comment.