Skip to content

Commit

Permalink
Pass options to backend read from setter
Browse files Browse the repository at this point in the history
This will not be a very commonly used feature, but for consistency, and
since the backend `write` methods accept options, I think we should send
them from the setter methods on the model class through to the backend
write method.
  • Loading branch information
shioyama committed Mar 25, 2017
1 parent cd62beb commit 5d224fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/mobility/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def initialize(method, *attributes_, **options_)
end
end

define_method "#{attribute}=" do |value|
mobility_set(attribute, value)
define_method "#{attribute}=" do |value, **options|
mobility_set(attribute, value, **options)
end if %i[accessor writer].include?(method)

define_locale_accessors(attribute, @accessor_locales) if @accessor_locales
Expand Down
7 changes: 3 additions & 4 deletions lib/mobility/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ def mobility_present?(*args)
mobility_read(*args).present?
end

def mobility_set(attribute, value, locale: Mobility.locale)
def mobility_set(attribute, value, locale: Mobility.locale, **options)
Mobility.enforce_available_locales!(locale)
mobility_backend_for(attribute).write(locale.to_sym, value == false ? value : value.presence)
mobility_backend_for(attribute).write(locale.to_sym, value == false ? value : value.presence, **options)
end

def mobility_read(attribute, **options)
locale = options.delete(:locale) || Mobility.locale
def mobility_read(attribute, locale: Mobility.locale, **options)
Mobility.enforce_available_locales!(locale)
mobility_backend_for(attribute).read(locale.to_sym, options)
end
Expand Down
11 changes: 8 additions & 3 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,14 @@

shared_examples_for "writer" do
it "correctly maps setter method for translated attribute to backend" do
expect(backend).to receive(:write).with(:de, "foo")
expect(backend).to receive(:write).with(:de, "foo", {})
article.title = "foo"
end

it "correctly maps other options to setter" do
expect(backend).to receive(:write).with(:de, "foo", fallback: false).and_return("foo")
expect(article.send(:title=, "foo", fallback: false)).to eq("foo")
end
end

describe "method = :accessor" do
Expand Down Expand Up @@ -209,7 +214,7 @@
it "converts blanks to nil when sending to backend setter" do
Article.include described_class.new(:writer, "title", { backend: backend_klass })
allow(Mobility).to receive(:locale).and_return(:fr)
expect(backend).to receive(:write).with(:fr, nil)
expect(backend).to receive(:write).with(:fr, nil, {})
article.title = ""
end

Expand All @@ -223,7 +228,7 @@
it "does not convert false values to nil when sending to backend setter" do
Article.include described_class.new(:writer, "title", { backend: backend_klass })
allow(Mobility).to receive(:locale).and_return(:fr)
expect(backend).to receive(:write).with(:fr, false)
expect(backend).to receive(:write).with(:fr, false, {})
article.title = false
end
end
Expand Down

0 comments on commit 5d224fa

Please sign in to comment.