Permalink
Browse files

Allow setting explicit nil values

  • Loading branch information...
1 parent b34a42a commit 28ba403f105ea7935ca031eb9783f3a18e3454b2 @lifo lifo committed Apr 27, 2010
Showing with 20 additions and 1 deletion.
  1. +11 −1 app/models/tolk/translation.rb
  2. +9 −0 test/unit/format_test.rb
@@ -3,7 +3,7 @@ class Translation < ActiveRecord::Base
set_table_name "tolk_translations"
serialize :text
- validates_presence_of :text, :unless => proc {|r| r.primary }
+ validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil }
validates_uniqueness_of :phrase_id, :scope => :locale_id
@@ -18,6 +18,9 @@ class Translation < ActiveRecord::Base
attr_accessor :primary
before_validation :fix_text_type, :unless => proc {|r| r.primary }
+ attr_accessor :explicit_nil
+ before_validation :set_explicit_nil
+
def up_to_date?
not out_of_date?
end
@@ -48,6 +51,13 @@ def value
private
+ def set_explicit_nil
+ if self.text == '~'
+ self.text = nil
+ self.explicit_nil = true
+ end
+ end
+
def fix_text_type
if primary_translation.present?
if self.text.is_a?(String) && !primary_translation.text.is_a?(String)
View
@@ -61,6 +61,15 @@ def test_creating_translations_fails_on_mismatch_with_primary_translation
assert_equal [1, 2], success.text
end
+ def test_creating_translations_with_nil_values
+ # implicit nil value
+ assert_raises(ActiveRecord::RecordInvalid) { @spanish.translations.create!(:phrase => ph('string')) }
+
+ # explicit nil value
+ niltrans = @spanish.translations.create!(:phrase => ph('string'), :text => '~')
+ assert ! niltrans.text
+ end
+
def test_creating_translation_with_wrong_type
assert_raises(ActiveRecord::RecordInvalid) { @spanish.translations.create!(:text => [1, 2], :phrase => ph('string')) }

0 comments on commit 28ba403

Please sign in to comment.