Skip to content

Commit

Permalink
Fallback to Rails message if AdequateError translation is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
lulalala committed Jan 8, 2018
1 parent b4ee77e commit 133d0a0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
adequate_errors (0.1.0)
adequate_errors (0.1.2)
activemodel

GEM
Expand All @@ -19,7 +19,7 @@ GEM
diff-lcs (1.3)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
minitest (5.10.3)
minitest (5.11.1)
rake (10.5.0)
rspec (3.7.0)
rspec-core (~> 3.7.0)
Expand Down
6 changes: 5 additions & 1 deletion lib/adequate_errors/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def message
model: @base.model_name.human,
attribute: humanized_attribute,
value: value,
object: @base
object: @base,
exception_handler: ->(exception, locale, key, option) {
rails_errors = @base.errors
rails_errors.full_message(@attribute, rails_errors.generate_message(@attribute, @type, @options))
}
}.merge!(@options)

I18n.translate(key, i18n_options)
Expand Down
2 changes: 1 addition & 1 deletion lib/adequate_errors/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AdequateErrors
VERSION = "0.1.1"
VERSION = "0.1.2"
end
7 changes: 7 additions & 0 deletions test/cases/adequate_errors/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,12 @@
)
assert_equal "Title foo bar baz", subject.message
end

it 'falls back to Rails message if translation can not be found' do
I18n.backend.store_translations(:en,{errors: {messages: {too_much_water: "has too much water"}}})

subject = AdequateErrors::Error.new(model, :title, :too_much_water)
assert_equal "Title has too much water", subject.message
end
end
end

0 comments on commit 133d0a0

Please sign in to comment.