Skip to content

Commit

Permalink
Handle presence methods in FallthroughAccessors
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Mar 26, 2017
1 parent adbf35a commit 66f6305
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
5 changes: 2 additions & 3 deletions lib/mobility/fallthrough_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ def title
class FallthroughAccessors < Module
# @param [Array<String>] Array of attributes
def initialize(*attributes)
method_name_regex = /\A(#{attributes.join('|'.freeze)})_([a-z]{2}(_[a-z]{2})?)(=?)\z/.freeze
method_name_regex = /\A(#{attributes.join('|'.freeze)})_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze

define_method :method_missing do |method_name, *arguments, &block|
if method_name =~ method_name_regex
define_method :method_missing do |method_name, *arguments, &block| if method_name =~ method_name_regex
attribute = $1.to_sym
locale, suffix = $2.split('_'.freeze)
locale = "#{locale}-#{suffix.upcase}".freeze if suffix
Expand Down
13 changes: 11 additions & 2 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,28 @@
context "with fallthrough_accessors = true" do
let(:options) { { fallthrough_accessors: true } }

it "overrides method_missing to handle getters for any locale" do
it "handle getters for any locale" do
expect(backend).to receive(:read).with(:de, {}).and_return("foo")
expect(article.title_de).to eq("foo")
expect(backend).to receive(:read).with(:fr, {}).and_return("bar")
expect(article.title_fr).to eq("bar")
end

it "overrides method_missing to handle setters for any locale" do
it "handle setters for any locale" do
expect(backend).to receive(:write).with(:de, "foo", {}).and_return("foo")
expect(article.title_de="foo").to eq("foo")
expect(backend).to receive(:write).with(:fr, "bar", {}).and_return("bar")
expect(article.title_fr="bar").to eq("bar")
end

it "handles presence methods for any locale" do
expect(backend).to receive(:read).with(:de, {}).and_return("foo")
expect(article.title_de?).to eq(true)
expect(backend).to receive(:read).with(:de, {}).and_return("")
expect(article.title_de?).to eq(false)
expect(backend).to receive(:read).with(:de, {}).and_return(nil)
expect(article.title_de?).to eq(false)
end
end
end
end
Expand Down
35 changes: 31 additions & 4 deletions spec/mobility/fallthrough_accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,44 @@
model_class = stub_const 'MyModel', Class.new
model_class.class_eval do
def title
"title in #{Mobility.locale}"
values[Mobility.locale]
end

def title?
values[Mobility.locale].present?
end

def title=(value)
values[Mobility.locale] = value
end

private

def values
@values ||= {}
end
end
model_class.include(described_class.new(:title))
model_class
end

it "handles any locale" do
it "handles getters and setters in any locale" do
instance = model_class.new
expect(instance.title_fr).to eq(nil)
expect(instance.title_de).to eq(nil)
instance.title_fr = "Titre"
expect(instance.title_fr).to eq("Titre")
instance.title_de = "Titel"
expect(instance.title_de).to eq("Titel")
end

it "handles presence methods in any locale" do
instance = model_class.new
expect(instance.title_fr).to eq("title in fr")
expect(instance.title_de).to eq("title in de")
expect(instance.title_fr?).to eq(false)
instance.title_fr = ""
expect(instance.title_fr?).to eq(false)
instance.title_fr = "Titre"
expect(instance.title_fr?).to eq(true)
end

it "raises InvalidLocale if locale is not in I18n.available_locales" do
Expand Down

0 comments on commit 66f6305

Please sign in to comment.