Skip to content

Commit

Permalink
Merge pull request #1988 from hosseintoussi/refactor_full_messages_me…
Browse files Browse the repository at this point in the history
…thod

refactored the full_messages method and stop overriding full_message
  • Loading branch information
dblock authored Feb 10, 2020
2 parents b9d53a2 + 918ad55 commit 3f17a27
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).

* [#1988](https://github.com/ruby-grape/grape/pull/1988): Refactored the full_messages method and stop overriding full_message - [@hosseintoussi](https://github.com/hosseintoussi).
### 1.3.0 (2020/01/11)

#### Features
Expand Down
4 changes: 0 additions & 4 deletions lib/grape/exceptions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ def translate_attributes(keys, **options)
end.join(', ')
end

def translate_attribute(key, **options)
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
end

def translate_message(key, **options)
case key
when Symbol
Expand Down
23 changes: 11 additions & 12 deletions lib/grape/exceptions/validation_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module Grape
module Exceptions
class ValidationErrors < Grape::Exceptions::Base
ERRORS_FORMAT_KEY = 'grape.errors.format'
DEFAULT_ERRORS_FORMAT = '%{attributes} %{message}'

include Enumerable

attr_reader :errors
Expand Down Expand Up @@ -41,21 +44,17 @@ def to_json(**_opts)
end

def full_messages
messages = map { |attributes, error| full_message(attributes, error) }
messages = map do |attributes, error|
I18n.t(
ERRORS_FORMAT_KEY,
default: DEFAULT_ERRORS_FORMAT,
attributes: translate_attributes(attributes),
message: error.message
)
end
messages.uniq!
messages
end

private

def full_message(attributes, error)
I18n.t(
'grape.errors.format',
default: '%{attributes} %{message}',
attributes: attributes.count == 1 ? translate_attribute(attributes.first) : translate_attributes(attributes),
message: error.message
)
end
end
end
end

0 comments on commit 3f17a27

Please sign in to comment.