Skip to content

Commit

Permalink
Add super option to locale accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Jul 29, 2017
1 parent b580980 commit cd6a1fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/mobility/locale_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ def initialize(*attribute_names, locales: I18n.available_locales)
locales.each do |locale|
normalized_locale = Mobility.normalize_locale(locale)
define_method "#{name}_#{normalized_locale}" do |**options|
return super() if options.delete(:super)
warn warning_message if options.delete(:locale)
Mobility.with_locale(locale) { send(name, options) }
end
define_method "#{name}_#{normalized_locale}?" do |**options|
return super() if options.delete(:super)
warn warning_message if options.delete(:locale)
Mobility.with_locale(locale) { send("#{name}?", options) }
end
define_method "#{name}_#{normalized_locale}=" do |value, **options|
return super(value) if options.delete(:super)
warn warning_message if options.delete(:locale)
Mobility.with_locale(locale) { send("#{name}=", value, options) }
end
Expand Down
32 changes: 32 additions & 0 deletions spec/mobility/locale_accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ def title=(_value, **_); end
end
end
end

describe "super: true" do
it "calls super of locale accessor method" do
spy = double("model")
mod = Module.new do
define_method :title_en do
spy.title_en
end
define_method :title_en? do
spy.title_en?
end
define_method :title_en= do |value|
spy.title_en = value
end
end
base_model_class.include mod
base_model_class.include described_class.new(:title)

instance = base_model_class.new

aggregate_failures do
expect(spy).to receive(:title_en).and_return("model foo")
instance.title_en(super: true)

expect(spy).to receive(:title_en?).and_return(true)
instance.title_en?(super: true)

expect(spy).to receive(:title_en=).with("model foo")
instance.send(:title_en=, "model foo", super: true)
end
end
end
end

describe ".apply" do
Expand Down

0 comments on commit cd6a1fe

Please sign in to comment.