Skip to content

Commit

Permalink
Error messages with the i18n gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Jan 23, 2015
1 parent e369678 commit e75b24a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 44 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
source "https://rubygems.org"

group :development do
gem 'i18n', '~> 0.7.0'
gem 'rake', '~> 10.3'
gem 'rspec', '~> 3.1'
gem 'rspec-collection_matchers', '~> 1.1'
gem 'jeweler', '~> 2.0'
gem 'rake-version', '~> 0.4'
gem 'simplecov', '~> 0.9'
gem 'coveralls', '~> 0.7', require: false
gem 'simplecov', '~> 0.9.1'
gem 'coveralls', '~> 0.7.3', require: false
end
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ GEM
oauth2
hashie (3.3.2)
highline (1.6.21)
i18n (0.7.0)
jeweler (2.0.1)
builder
bundler (>= 1.0)
Expand Down Expand Up @@ -89,10 +90,11 @@ PLATFORMS
ruby

DEPENDENCIES
coveralls (~> 0.7)
coveralls (~> 0.7.3)
i18n (~> 0.7.0)
jeweler (~> 2.0)
rake (~> 10.3)
rake-version (~> 0.4)
rspec (~> 3.1)
rspec-collection_matchers (~> 1.1)
simplecov (~> 0.9)
simplecov (~> 0.9.1)
40 changes: 11 additions & 29 deletions lib/errapi/plugins/i18n_messages.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
class Errapi::Plugins::I18nMessages
MESSAGES = {
array_length: {
wrong_length: 'This array must contain exactly %{check_value} elements but has %{checked_value} elements.',
too_short: 'This array must contain at least %{check_value} elements but has only %{checked_value} elements.',
too_long: 'This array must contain at most %{check_value} elements but has %{checked_value} elements.'
},
presence: {
nil: 'This value cannot be null.',
empty: 'This value cannot be empty.',
blank: 'This value cannot be blank.'
},
string_length: {
wrong_length: 'This string must be exactly %{check_value} characters long but has %{checked_value} characters.',
too_short: 'This string must be at least %{check_value} characters long but has only %{checked_value} characters.',
too_long: 'This string must be at most %{check_value} characters long but has %{checked_value} characters.'
},
type: {
wrong_type: 'This value is of the wrong type.'
}
}
require 'i18n'

def _disabled_build_error error, context
if !error.message && MESSAGES.key?(error.validation) && message = MESSAGES[error.validation][error.reason]
class Errapi::Plugins::I18nMessages

%w(check_value checked_value).each do |interpolated|
if error.respond_to? interpolated
message = message.gsub /\%\{#{interpolated}\}/, interpolated
end
end
def serialize_error error, serialized
return if serialized.key? :message

error.message = message
if I18n.exists? error.reason
interpolation_values = INTERPOLATION_KEYS.inject({}){ |memo,key| memo[key] = error.send(key); memo }.reject{ |k,v| v.nil? }
serialized[:message] = I18n.t error.reason, interpolation_values
end
end

private

INTERPOLATION_KEYS = %i(check_value checked_value)
end
9 changes: 9 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
en:
missing: "This value is required."
"null": "This value cannot be null."
empty: "This value cannot be empty."
blank: "This value cannot be blank."
too_long: "This value is too long (the maximum is %{check_value} while the actual length is %{checked_value})."
too_short: "This value is too short (the maximum is %{check_value} while the actual length is %{checked_value})."
wrong_length: "This value is the wrong length (the expected length is %{check_value} while the actual length is %{checked_value}."
wrong_type: "This value is of the wrong type."
50 changes: 39 additions & 11 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@
validations.validate h, context, location_type: :dotted

expect(context.errors?).to be(true)
expect(context.errors).to have(6).items
expect(context.errors?(location: 'bar.foo')).to be(true)
expect(context.errors?(location: 'qux')).to be(true)
expect(context.errors?(location: 'baz.2.a')).to be(true)
expect(context.errors?(location: 'baz.4.a')).to be(true)
expect(context.errors?(location: 'bar.baz')).to be(true)
expect(context.errors?(location: 'corge.grault')).to be(true)
expect(context.errors).to have(6).items
end

it "should conditionally execute validations based on custom conditions" do
Expand Down Expand Up @@ -147,13 +147,13 @@
validations.validate h, context, location_type: :dotted

expect(context.errors?).to be(true)
expect(context.errors).to have(6).items
expect(context.errors?(location: 'baz')).to be(true)
expect(context.errors?(location: 'qux')).to be(true)
expect(context.errors?(location: 'corge')).to be(true)
expect(context.errors?(location: 'grault')).to be(true)
expect(context.errors?(location: 'garply')).to be(true)
expect(context.errors?(location: 'waldo')).to be(true)
expect(context.errors).to have(6).items
end

it "should conditionally execute validations based on previous errors" do
Expand Down Expand Up @@ -194,9 +194,9 @@
validations.validate h, context, location_type: :dotted

expect(context.errors?).to be(true)
expect(context.errors).to have(2).items
expect(context.errors?(location: 'foo')).to be(true)
expect(context.errors?(location: 'bar')).to be(true)
expect(context.errors).to have(2).items
end

it "should serialize errors" do
Expand Down Expand Up @@ -233,16 +233,44 @@

validations.validate h, context, location_type: :dotted

expect(context.errors?).to be(true)
expect(context.errors).to have(6).items
expect(context.serialize).to eq({
errors: [
{ reason: :empty, location: 'qux', location_type: :dotted },
{ reason: :null, location: 'baz.2.a', location_type: :dotted },
{ reason: :blank, location: 'baz.3.a', location_type: :dotted },
{ reason: :missing, location: 'baz.4.a', location_type: :dotted },
{ reason: :missing, location: 'bar.baz', location_type: :dotted },
{ reason: :null, location: 'corge.grault', location_type: :dotted }
{
reason: :empty,
message: "This value cannot be empty.",
location: 'qux',
location_type: :dotted
},
{
reason: :null,
message: "This value cannot be null.",
location: 'baz.2.a',
location_type: :dotted
},
{
reason: :blank,
message: "This value cannot be blank.",
location: 'baz.3.a',
location_type: :dotted
},
{
reason: :missing,
message: "This value is required.",
location: 'baz.4.a',
location_type: :dotted
},
{
reason: :missing,
message: "This value is required.",
location: 'bar.baz',
location_type: :dotted
},
{
reason: :null,
message: "This value cannot be null.",
location: 'corge.grault',
location_type: :dotted
}
]
})
end
Expand Down
6 changes: 6 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end

config.before :suite do
I18n.load_path = [ File.expand_path(File.join(File.dirname(__FILE__), '..', 'locales', 'en.yml')) ]
I18n.locale = :en
I18n.default_locale = :en
end
end

require 'errapi'

0 comments on commit e75b24a

Please sign in to comment.