From d4f0ee20d5507ba147f31aa03081f685e31ab46a Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Sat, 25 Mar 2017 23:43:14 +0900 Subject: [PATCH] Raise InvalidLocale exception when getting locale that is not available --- lib/mobility.rb | 13 ++++++++++--- lib/mobility/instance_methods.rb | 2 ++ spec/mobility/attributes_spec.rb | 20 ++++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/mobility.rb b/lib/mobility.rb index 3bb90b29b..55f8634e9 100644 --- a/lib/mobility.rb +++ b/lib/mobility.rb @@ -208,6 +208,15 @@ def normalize_locale_accessor(attribute, locale = Mobility.locale) "#{attribute}_#{normalize_locale(locale)}".freeze end + # Raises InvalidLocale exception if the locale passed in is present but not available. + # @param [String,Symbol] locale + # @raise [InvalidLocale] if locale is present but not available + def enforce_available_locales!(locale) + if I18n.enforce_available_locales + raise Mobility::InvalidLocale.new(locale) unless (I18n.locale_available?(locale) || locale.nil?) + end + end + protected def read_locale @@ -216,9 +225,7 @@ def read_locale def set_locale(locale) locale = locale.to_sym if locale - if I18n.enforce_available_locales - raise Mobility::InvalidLocale.new(locale) unless (I18n.available_locales.include?(locale) || locale.nil?) - end + enforce_available_locales!(locale) storage[:mobility_locale] = locale end end diff --git a/lib/mobility/instance_methods.rb b/lib/mobility/instance_methods.rb index 6dccba826..0dd475efb 100644 --- a/lib/mobility/instance_methods.rb +++ b/lib/mobility/instance_methods.rb @@ -24,11 +24,13 @@ def mobility_present?(*args) end def mobility_set(attribute, value, locale: Mobility.locale) + Mobility.enforce_available_locales!(locale) mobility_backend_for(attribute).write(locale.to_sym, value == false ? value : value.presence) end def mobility_read(attribute, **options) locale = options.delete(:locale) || Mobility.locale + Mobility.enforce_available_locales!(locale) mobility_backend_for(attribute).read(locale.to_sym, options) end end diff --git a/spec/mobility/attributes_spec.rb b/spec/mobility/attributes_spec.rb index 8352fde2d..ac307d520 100644 --- a/spec/mobility/attributes_spec.rb +++ b/spec/mobility/attributes_spec.rb @@ -153,6 +153,10 @@ expect(article.title(locale: "fr")).to eq("foo") end + it "raises InvalidLocale exception if locale is not in I18n.available_locales" do + expect { article.title(locale: :it) }.to raise_error(Mobility::InvalidLocale) + end + it "correctly maps other options to getter" do expect(backend).to receive(:read).with(:de, fallbacks: false).and_return("foo") expect(article.title(fallbacks: false)).to eq("foo") @@ -197,8 +201,8 @@ # to consider storing blank values. it "converts blanks to nil when receiving from backend getter" do Article.include described_class.new(:reader, "title", { backend: backend_klass }) - allow(Mobility).to receive(:locale).and_return(:ru) - expect(backend).to receive(:read).with(:ru, {}).and_return("") + allow(Mobility).to receive(:locale).and_return(:cz) + expect(backend).to receive(:read).with(:cz, {}).and_return("") expect(article.title).to eq(nil) end @@ -211,8 +215,8 @@ it "does not convert false values to nil when receiving from backend getter" do Article.include described_class.new(:reader, "title", { backend: backend_klass }) - allow(Mobility).to receive(:locale).and_return(:ru) - expect(backend).to receive(:read).with(:ru, {}).and_return(false) + allow(Mobility).to receive(:locale).and_return(:cz) + expect(backend).to receive(:read).with(:cz, {}).and_return(false) expect(article.title).to eq(false) end @@ -264,15 +268,15 @@ end context "with locale_accessors a hash" do - let(:options) { { locale_accessors: [:en, :pt] } } + let(:options) { { locale_accessors: [:en, :'pt-BR'] } } it "defines accessors for locales in locale_accessors hash" do expect(backend).to receive(:read).twice.with(:en, {}).and_return("enfoo") expect(article.title_en).to eq("enfoo") expect(article.title_en?).to eq(true) - expect(backend).to receive(:read).twice.with(:pt, {}).and_return("ptfoo") - expect(article.title_pt).to eq("ptfoo") - expect(article.title_pt?).to eq(true) + expect(backend).to receive(:read).twice.with(:'pt-BR', {}).and_return("ptfoo") + expect(article.title_pt_br).to eq("ptfoo") + expect(article.title_pt_br?).to eq(true) end it "does not define accessors for locales not in locale_accessors hash" do