Skip to content

Commit

Permalink
Store back message_key on Grape::Exceptions::Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkou committed Apr 20, 2016
1 parent 0331563 commit b8d8712
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

* Your contribution here.

#### Features
* [#1366](https://github.com/ruby-grape/grape/pull/1366): Store back `message_key` on `Grape::Exceptions::Validation` - [@mkou](https://github.com/mkou).

0.16.2 (4/12/2016)
==================

Expand Down
1 change: 1 addition & 0 deletions lib/grape/exceptions/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Validation < Grape::Exceptions::Base
def initialize(args = {})
fail 'Params are missing:' unless args.key? :params
@params = args[:params]
@message_key = args[:message] if args.key?(:message) && args[:message].is_a?(Symbol)
args[:message] = translate_message(args[:message]) if args.key? :message
super
end
Expand Down
10 changes: 10 additions & 0 deletions spec/grape/exceptions/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@
it 'fails when params are missing' do
expect { Grape::Exceptions::Validation.new(message: 'presence') }.to raise_error(RuntimeError, 'Params are missing:')
end
context 'when message is a symbol' do
it 'stores message_key' do
expect(Grape::Exceptions::Validation.new(params: ['id'], message: :presence).message_key).to eq(:presence)
end
end
context 'when message is a String' do
it 'does not store the message_key' do
expect(Grape::Exceptions::Validation.new(params: ['id'], message: 'presence').message_key).to eq(nil)
end
end
end

0 comments on commit b8d8712

Please sign in to comment.