Skip to content

Commit

Permalink
Return locale from backend read/write methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 7, 2018
1 parent ae06d74 commit 6241068
Show file tree
Hide file tree
Showing 38 changed files with 259 additions and 234 deletions.
6 changes: 4 additions & 2 deletions lib/mobility/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ def define_reader(attribute)
def #{attribute}(**options)
return super() if options.delete(:super)
#{set_locale_from_options_inline}
mobility_backends[:#{attribute}].read(locale, options)
_, value = mobility_backends[:#{attribute}].read(locale, options)
value
end
def #{attribute}?(**options)
Expand All @@ -207,7 +208,8 @@ def define_writer(attribute)
def #{attribute}=(value, **options)
return super(value) if options.delete(:super)
#{set_locale_from_options_inline}
mobility_backends[:#{attribute}].write(locale, value, options)
_, value = mobility_backends[:#{attribute}].write(locale, value, options)
value
end
EOM
end
Expand Down
3 changes: 2 additions & 1 deletion lib/mobility/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def locales
# @param [Symbol] locale Locale to read
# @return [TrueClass,FalseClass] Whether translation is present for locale
def present?(locale, options = {})
Util.present?(read(locale, options))
_, value = read(locale, options)
Util.present?(value)
end

# @!method model_class
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/active_record/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class ActiveRecord::Column
# @!group Backend Accessors
# @!macro backend_reader
def read(locale, _ = {})
model.read_attribute(column(locale))
[locale, model.read_attribute(column(locale))]
end

# @!macro backend_writer
def write(locale, value, _ = {})
model.send(:write_attribute, column(locale), value)
[locale, model.send(:write_attribute, column(locale), value)]
end
# @!endgroup

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/active_record/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ActiveRecord::Container
# @param [Hash] options
# @return [String,Integer,Boolean] Value of translation
def read(locale, _ = nil)
model_translations(locale)[attribute]
[locale, model_translations(locale)[attribute]]
end

# @note Translation may be a string, integer, boolean, hash or array
Expand All @@ -36,7 +36,7 @@ def read(locale, _ = nil)
# @return [String,Integer,Boolean] Updated value
def write(locale, value, _ = nil)
set_attribute_translation(locale, value)
model_translations(locale)[attribute]
[locale, model_translations(locale)[attribute]]
end
# @!endgroup

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Hash
# @!macro backend_reader
# @return [Object]
def read(locale, _ = {})
translations[locale]
[locale, translations[locale]]
end

# @!macro backend_writer
# @return [Object]
def write(locale, value, _ = {})
translations[locale] = value
[locale, translations[locale] = value]
end
# @!endgroup

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/hash_valued.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ module HashValued
#
# @!macro backend_reader
def read(locale, _options = nil)
translations[locale]
[locale, translations[locale]]
end

# @!macro backend_writer
def write(locale, value, _options = nil)
translations[locale] = value
[locale, translations[locale] = value]
end
# @!endgroup

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ module KeyValue
# @!group Backend Accessors
# @!macro backend_reader
def read(locale, options = {})
translation_for(locale, options).value
[locale, translation_for(locale, options).value]
end

# @!macro backend_writer
def write(locale, value, options = {})
translation_for(locale, options).value = value
[locale, translation_for(locale, options).value = value]
end
# @!endgroup

Expand Down
13 changes: 11 additions & 2 deletions lib/mobility/backends/sequel/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ class Sequel::Column
# @!macro backend_reader
def read(locale, _options = nil)
column = column(locale)
model[column] if model.columns.include?(column)
if model.columns.include?(column)
[locale, model[column]]
else
[locale, nil]
end
end

# @!group Backend Accessors
# @!macro backend_writer
def write(locale, value, _options = nil)
column = column(locale)
model[column] = value if model.columns.include?(column)
if model.columns.include?(column)
model[column] = value
[locale, value]
else
[locale, nil]
end
end

# @!macro backend_iterator
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/sequel/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Sequel::Container
# @param [Hash] options
# @return [String,Integer,Boolean] Value of translation
def read(locale, _ = nil)
model_translations(locale)[attribute]
[locale, model_translations(locale)[attribute]]
end

# @note Translation may be a string, integer, boolean, hash or array
Expand All @@ -35,7 +35,7 @@ def read(locale, _ = nil)
# @return [String,Integer,Boolean] Updated value
def write(locale, value, _ = nil)
set_attribute_translation(locale, value)
model_translations(locale)[attribute]
[locale, model_translations(locale)[attribute]]
end
# @!endgroup
#
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/sequel/pg_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class PgHash
include HashValued

def read(locale, options = {})
super(locale.to_s, options)
[locale, super(locale.to_s, options)[1]]
end

def write(locale, value, options = {})
super(locale.to_s, value, options)
[locale, super(locale.to_s, value, options)[1]]
end

# @!macro backend_iterator
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ module Table
# @!group Backend Accessors
# @!macro backend_reader
def read(locale, options = {})
translation_for(locale, options).send(attribute)
[locale, translation_for(locale, options).send(attribute)]
end

# @!macro backend_writer
def write(locale, value, options = {})
translation_for(locale, options).send("#{attribute}=", value)
[locale, translation_for(locale, options).send("#{attribute}=", value)]
end
# @!endgroup

Expand Down
9 changes: 6 additions & 3 deletions lib/mobility/plugins/active_model/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ def write(locale, value, options = {})
locale_accessor = Mobility.normalize_locale_accessor(attribute, locale)
if model.changed_attributes.has_key?(locale_accessor) && model.changed_attributes[locale_accessor] == value
model.send(:attributes_changed_by_setter).except!(locale_accessor)
elsif read(locale, options.merge(locale: true)) != value
model.send(:mobility_changed_attributes) << locale_accessor
model.send(:attribute_will_change!, locale_accessor)
else
_, backend_value = read(locale, options.merge(locale: true))
if backend_value != value
model.send(:mobility_changed_attributes) << locale_accessor
model.send(:attribute_will_change!, locale_accessor)
end
end
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def attribute_alias(attribute, locale = Mobility.locale)
def read(locale, **)
if (model_attributes_defined? &&
model_attributes.key?(alias_ = Query.attribute_alias(attribute, locale)))
model_attributes[alias_].value
[locale, model_attributes[alias_].value]
else
super
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mobility/plugins/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ module BackendMethods
def read(locale, **options)
return super(locale, options) if options.delete(:cache) == false
if cache.has_key?(locale)
cache[locale]
[locale, cache[locale]]
else
cache[locale] = super(locale, options)
super(locale, options).tap { |_, value| cache[locale] = value }
end
end

Expand All @@ -50,7 +50,7 @@ def read(locale, **options)
# *false* to disable cache.
def write(locale, value, **options)
return super if options.delete(:cache) == false
cache[locale] = super
super.tap { |_, value_| cache[locale] = value_ }
end
# @!endgroup

Expand Down
7 changes: 4 additions & 3 deletions lib/mobility/plugins/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ module BackendMethods
# *false* to disable presence filter.
def read(locale, accessor_options = {})
default = accessor_options.has_key?(:default) ? accessor_options.delete(:default) : options[:default]
if (value = super(locale, accessor_options)).nil?
Default[default, locale: locale, accessor_options: accessor_options, model: model, attribute: attribute]
locale, value = super(locale, accessor_options)
if value.nil?
[locale, Default[default, locale: locale, accessor_options: accessor_options, model: model, attribute: attribute]]
else
value
[locale, value]
end
end
# @!endgroup
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/plugins/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def define_read(fallbacks)

locales = fallback == true ? fallbacks[locale] : [locale, *fallback]
locales.each do |fallback_locale|
value = super(fallback_locale, options)
return value if Util.present?(value)
upstream_locale, value = super(fallback_locale, options)
return [upstream_locale, value] if Util.present?(value)
end

super(locale, options)
Expand Down
7 changes: 6 additions & 1 deletion lib/mobility/plugins/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ module BackendMethods
# @option options [Boolean] presence
# *false* to disable presence filter.
def read(locale, **options)
options.delete(:presence) == false ? super : Presence[super]
if options.delete(:presence) == false
super
else
locale, value = super
[locale, Presence[value]]
end
end

# @!macro backend_writer
Expand Down
9 changes: 6 additions & 3 deletions lib/mobility/plugins/sequel/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ def write(locale, value, options = {})
if model.column_changes.has_key?(locale_accessor) && model.initial_values[locale_accessor] == value
super
[model.changed_columns, model.initial_values].each { |h| h.delete(locale_accessor) }
elsif read(locale, options.merge(fallback: false)) != value
model.will_change_column(locale_accessor)
super
else
_, backend_value = read(locale, options.merge(fallback: false))
if backend_value != value
model.will_change_column(locale_accessor)
super
end
end
end
# @!endgroup
Expand Down
10 changes: 5 additions & 5 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,18 @@
shared_examples_for "reader" do
it "correctly maps getter method for translated attribute to backend" do
expect(Mobility).to receive(:locale).and_return(:de)
expect(listener).to receive(:read).with(:de, {}).and_return("foo")
expect(listener).to receive(:read).with(:de, {}).and_return([:de, "foo"])
expect(article.title).to eq("foo")
end

it "correctly maps presence method for translated attribute to backend" do
expect(Mobility).to receive(:locale).and_return(:de)
expect(listener).to receive(:read).with(:de, {}).and_return("foo")
expect(listener).to receive(:read).with(:de, {}).and_return([:de, "foo"])
expect(article.title?).to eq(true)
end

it "correctly maps locale through getter options and converts to boolean" do
expect(listener).to receive(:read).with(:fr, locale: true).and_return("foo")
expect(listener).to receive(:read).with(:fr, locale: true).and_return([:fr, "foo"])
expect(article.title(locale: "fr")).to eq("foo")
end

Expand All @@ -256,7 +256,7 @@

it "correctly maps other options to getter" do
expect(Mobility).to receive(:locale).and_return(:de)
expect(listener).to receive(:read).with(:de, someopt: "someval").and_return("foo")
expect(listener).to receive(:read).with(:de, someopt: "someval").and_return([:de, "foo"])
expect(article.title(someopt: "someval")).to eq("foo")
end

Expand All @@ -280,7 +280,7 @@

it "correctly maps other options to setter" do
expect(Mobility).to receive(:locale).and_return(:de)
expect(listener).to receive(:write).with(:de, "foo", someopt: "someval").and_return("foo")
expect(listener).to receive(:write).with(:de, "foo", someopt: "someval").and_return([:de, "foo"])
expect(article.send(:title=, "foo", someopt: "someval")).to eq("foo")
end

Expand Down
4 changes: 2 additions & 2 deletions spec/mobility/backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

describe "#present?" do
it "returns true if backend.read(locale) return non-blank value" do
expect(backend_double).to receive(:read).with(:en, {}).and_return("foo")
expect(backend_double).to receive(:read).with(:en, {}).and_return([:en, "foo"])
expect(backend.present?(:en)).to eq(true)
end

it "returns false if backend.read(locale) returns blank value" do
expect(backend_double).to receive(:read).with(:en, {}).and_return("")
expect(backend_double).to receive(:read).with(:en, {}).and_return([:en, ""])
expect(backend.present?(:en)).to eq(false)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/mobility/backends/active_record/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
describe "#read" do
it "returns attribute in locale from appropriate column" do
aggregate_failures do
expect(backend.read(:en)).to eq("Good post!")
expect(backend.read(:ja)).to eq("なかなか面白い記事")
expect(backend.read(:en)).to eq([:en, "Good post!"])
expect(backend.read(:ja)).to eq([:ja, "なかなか面白い記事"])
end
end

it "handles dashed locales" do
expect(backend.read(:"pt-BR")).to eq("Olá")
expect(backend.read(:'pt-BR')).to eq([:'pt-BR', "Olá"])
end
end

Expand Down
Loading

0 comments on commit 6241068

Please sign in to comment.