From 93b0ca2fe7100ec09ef0a7bc4fabb034714748c1 Mon Sep 17 00:00:00 2001 From: Gerard de Brieder Date: Thu, 22 Jan 2009 11:54:04 +0100 Subject: [PATCH] updated the getter to fallback to default locale of the requested one is nil --- lib/has_foreign_language.rb | 49 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/has_foreign_language.rb b/lib/has_foreign_language.rb index 1283818..a30cac0 100644 --- a/lib/has_foreign_language.rb +++ b/lib/has_foreign_language.rb @@ -11,30 +11,31 @@ def has_foreign_language(*args) # Define the Getter define_method(field.to_s) do if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0 - self.send("#{field}_#{I18n.locale}".to_sym) - else - super - end - end - - # Define the Setter - define_method("#{field}=") do |val| - if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0 - self["#{field}_#{I18n.locale}".to_sym] = val - else - self[field.to_sym] = val - end - end - - # Define the Default Getter - define_method("#{field}_#{I18n.default_locale}") do - self.send(field.to_sym) - end - - # Define the Default Setter - define_method("#{field}_#{I18n.default_locale}=") do |val| - self[field.to_sym] = val - end + result=self.send("#{field}_#{I18n.locale}".to_sym) + result.blank? ? super : self.send("#{field}_#{I18n.locale}".to_sym) #falling back to default if requested locale is nil + else + super + end + end + + # Define the Setter + define_method("#{field}=") do |val| + if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0 + self["#{field}_#{I18n.locale}".to_sym] = val + else + self[field.to_sym] = val + end + end + + # Define the Default Getter + define_method("#{field}_#{I18n.default_locale}") do + self.send(field.to_sym) + end + + # Define the Default Setter + define_method("#{field}_#{I18n.default_locale}=") do |val| + self[field.to_sym] = val + end end end end