Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add back message key in order to have error_codes #1366

Merged
merged 2 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
Copy link
Member

@dblock dblock Apr 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the "back" word ;) And add a space above this bullet point between it and features.


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
Copy link
Member

@dblock dblock Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I nitpick? Is this better? Avoids checking key? twice.

return unless args.key?(:message)
@message_key = args[:message] if args[:message].is_a?(Symbol)
args[:message] = translate_message(args[:message])

or with an if, I don't care.

Copy link
Contributor Author

@mkou mkou Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm wouldn't the return bypass the super method?
I like the idea of not checking the key twice though.

Copy link
Contributor Author

@mkou mkou Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer that version ?

super && return unless args.key?(:message)
@message_key = args[:message] if args[:message].is_a?(Symbol)
args[:message] = translate_message(args[:message])
super

or

if args.key?(:message)
  @message_key = args[:message] if args[:message].is_a?(Symbol)
  args[:message] = translate_message(args[:message])
end
super

(I think I prefer the last one)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely the latter 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise I'm with @ridiculous

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