Skip to content

Commit

Permalink
add one more spec calling a model instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
doits committed Sep 26, 2019
1 parent 8ca33e8 commit 06cd86c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/mobility/plugins/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def define_read(fallbacks)
)

fallback_locales.each do |fallback_locale|
value = super(fallback_locale.to_sym, options)
return value if Util.present?(value)
fallback_value = super(fallback_locale.to_sym, options)
return fallback_value if Util.present?(fallback_value)
end

value
Expand Down
50 changes: 49 additions & 1 deletion spec/mobility/plugins/fallbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ def read(locale, **options)
end
Class.new(backend_subclass).include(described_class.new(fallbacks))
end
let(:object) { (stub_const 'MobilityModel', Class.new).include(Mobility).new }
let(:object) do
(stub_const 'MobilityModel', Class.new)
.include(Mobility)
.include(
Module.new do
def fallback_locale_method
'de-DE'
end
end
).new
end
subject { backend_class.new(object, "title") }

context "fallbacks is a hash" do
Expand Down Expand Up @@ -185,6 +195,44 @@ def read(locale, **options)
end
end

context "fallbacks is a proc calling a model method" do
let(:fallbacks) { proc { fallback_locale_method } }

it "returns value when value is not nil" do
expect(subject.read(:ja)).to eq("フー")
end

it "falls through to fallback locale when value is nil" do
expect(subject.read(:"en-US")).to eq("foo")
end

it "falls through to fallback locale when value is blank" do
expect(subject.read(:pt)).to eq("foo")
end

it "falls through to fallback locale when fallback: true option is passed" do
expect(subject.read(:"en-US", fallback: true)).to eq("foo")
end

it "uses locale passed in as value of fallback option when present" do
expect(subject.read(:"en-US", fallback: :ja)).to eq("フー")
end

it "uses array of locales passed in as value of fallback options when present" do
expect(subject.read(:"en-US", fallback: [:pl, :'de-DE'])).to eq("foo")
end

it "passes options to getter in fallback locale" do
expect(subject.read(:'en-US', bar: true)).to eq("bar")
end

it "does not modify options passed in" do
options = { fallback: false }
subject.read(:"en-US", options)
expect(options).to eq({ fallback: false })
end
end

context "fallbacks is true" do
let(:fallbacks) { true }

Expand Down

0 comments on commit 06cd86c

Please sign in to comment.