Skip to content

Commit

Permalink
Emit warning if keyword arguments passed to default plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Dec 22, 2017
1 parent 358394b commit 739a68f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mobility/plugins/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def initialize(default_option)
default = options.has_key?(:default) ? options.delete(:default) : default_option
if (value = super(locale, options)).nil?
return default unless default.is_a?(Proc)
# TODO: Remove in v1.0
if default.parameters.any? { |n, v| [:keyreq, :keyopt].include?(n) && [:model, :attribute, :locale, :options].include?(v) }
warn "WARNING: The Mobility default plugin no longer accepts keyword arguments."
return value
end
args = [attribute, locale, options]
args = args.first(default.arity) unless default.arity < 0
model.instance_exec(*args, &default)
Expand Down
9 changes: 9 additions & 0 deletions spec/mobility/plugins/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
expect(backend.read(:fr, default: default_as_option, this: 'option')).to eq("default title")
end
end

# TODO: Remove in v1.0
it "emits warning if proc takes keyword arguments" do
expect(backend_double).to receive(:read).once.with(:fr, this: 'option').and_return(nil)
default_as_option = Proc.new { |model:, attribute:, locale:, options:| }
expect {
backend.read(:fr, default: default_as_option, this: 'option')
}.to output(/#{"WARNING: The Mobility default plugin no longer accepts keyword arguments."}/).to_stderr
end
end
end
end
Expand Down

0 comments on commit 739a68f

Please sign in to comment.