From fdf56a307901024a37d3bd14328ca46e4887da19 Mon Sep 17 00:00:00 2001 From: Thom Ritterfeld Date: Sun, 1 Feb 2015 11:03:45 +0100 Subject: [PATCH 1/3] Change to translation_for Change the setter to !translation_for(locale) it is now possible that the cache is empty but the DB has already a translation and the update still fails. --- lib/globalize-accessors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/globalize-accessors.rb b/lib/globalize-accessors.rb index bb90ef8..ef0792d 100644 --- a/lib/globalize-accessors.rb +++ b/lib/globalize-accessors.rb @@ -36,7 +36,7 @@ def define_setter(attr_name, locale) localized_attr_name = localized_attr_name_for(attr_name, locale) define_method :"#{localized_attr_name}=" do |value| - return if !translation_caches[locale] && value.blank? + return if !translation_for(locale) && value.blank? write_attribute(attr_name, value, :locale => locale) translation_for(locale)[attr_name] = value end From a376c3fc3ca0465e07d3fb8e87638a86c7efbc34 Mon Sep 17 00:00:00 2001 From: Thom Ritterfeld Date: Sun, 1 Feb 2015 12:05:52 +0100 Subject: [PATCH 2/3] Bugfix for locales that does not exist translation_for is not returning nil if there is no locale available --- lib/globalize-accessors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/globalize-accessors.rb b/lib/globalize-accessors.rb index ef0792d..c3931a0 100644 --- a/lib/globalize-accessors.rb +++ b/lib/globalize-accessors.rb @@ -36,7 +36,7 @@ def define_setter(attr_name, locale) localized_attr_name = localized_attr_name_for(attr_name, locale) define_method :"#{localized_attr_name}=" do |value| - return if !translation_for(locale) && value.blank? + return if !translation_for(locale,false) && value.blank? write_attribute(attr_name, value, :locale => locale) translation_for(locale)[attr_name] = value end From 7c59a21ba8f2a75ead6fc2eb2e33e2d19a45bc12 Mon Sep 17 00:00:00 2001 From: Thom Ritterfeld Date: Sun, 1 Feb 2015 12:07:02 +0100 Subject: [PATCH 3/3] If all_blank delete translation Added delete translation check method --- lib/globalize-accessors.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/globalize-accessors.rb b/lib/globalize-accessors.rb index c3931a0..fe1e805 100644 --- a/lib/globalize-accessors.rb +++ b/lib/globalize-accessors.rb @@ -35,11 +35,25 @@ def define_getter(attr_name, locale) def define_setter(attr_name, locale) localized_attr_name = localized_attr_name_for(attr_name, locale) + define_method :"delete_translation_if_all_blank" do |locale| + should_delete = true + translated_attributes.keys.each do |key| + should_delete = translation_for(locale)[key].blank? + break if not should_delete + end + if should_delete + translations.where(:locale => locale).delete_all() + reload() + end + end + define_method :"#{localized_attr_name}=" do |value| return if !translation_for(locale,false) && value.blank? write_attribute(attr_name, value, :locale => locale) translation_for(locale)[attr_name] = value + delete_translation_if_all_blank(locale) end + if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name) attr_accessible :"#{localized_attr_name}" end