From ddca9c8a01ed529e707325c533fdac4c06eef31b Mon Sep 17 00:00:00 2001 From: mrbrdo Date: Tue, 3 May 2022 14:50:56 +0200 Subject: [PATCH 1/2] add failing spec to demonstrate generating empty translations prevents saving parent when translations have validations --- spec/mobility/backends/active_record/table_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/mobility/backends/active_record/table_spec.rb b/spec/mobility/backends/active_record/table_spec.rb index 774768e6..bb51673a 100644 --- a/spec/mobility/backends/active_record/table_spec.rb +++ b/spec/mobility/backends/active_record/table_spec.rb @@ -109,6 +109,20 @@ end end + it "does not prevent saving when a nil translation is generated and invalid" do + Article::Translation.validates :title, presence: true + + Mobility.locale = :en + article = Article.new(title: "New Article") + expect(article.save).to eq(true) + + Mobility.locale = :ja + article.title + + Mobility.locale = :en + expect(article.save).to eq(true) + end + it "removes nil translations when saving persisted record" do Mobility.locale = :en article = Article.create(title: "New Article") From 023b53d23cc60f3adc3769b93c78b4dede3c10b7 Mon Sep 17 00:00:00 2001 From: mrbrdo Date: Thu, 5 May 2022 01:13:57 +0200 Subject: [PATCH 2/2] Fix table backend's attr reader building empty associations which can prevent valid? on parent model (issue #569) --- lib/mobility/backends/table.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mobility/backends/table.rb b/lib/mobility/backends/table.rb index c48440b7..19339ba9 100644 --- a/lib/mobility/backends/table.rb +++ b/lib/mobility/backends/table.rb @@ -85,7 +85,8 @@ module Table # @!group Backend Accessors # @!macro backend_reader def read(locale, **options) - translation_for(locale, **options).send(attribute) + translation = translations.in_locale(locale) + translation.send(attribute) if translation end # @!macro backend_writer