Skip to content

Commit

Permalink
Raise InvalidLocale exception when getting locale that is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Mar 25, 2017
1 parent 212f078 commit d4f0ee2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 10 additions & 3 deletions lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/mobility/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d4f0ee2

Please sign in to comment.