mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Allow the use of %{attribute} or %{model} in i18n translations.
Fix a bug where shoulda was raising an exception, when a translation in en.yml was modified to include %{attribute} or %{model}
This commit is contained in:
parent
cf848e6d78
commit
d92d548d42
6 changed files with 77 additions and 1 deletions
2
NEWS.md
2
NEWS.md
|
@ -1,5 +1,7 @@
|
|||
# HEAD
|
||||
|
||||
* Fixes the issue where using %{attribute} or %{model} in I18n translations
|
||||
raised exceptions
|
||||
* Support datetime columns in `validate_uniqueness_of.scoped_to`
|
||||
* Add `allow_nil` option to the `validate_uniqueness_of` matcher
|
||||
|
||||
|
|
|
@ -104,6 +104,7 @@ module Shoulda # :nodoc:
|
|||
if Symbol === @short_message
|
||||
@short_message = default_error_message(@short_message,
|
||||
:model_name => @subject.class.to_s.underscore,
|
||||
:instance => @subject,
|
||||
:attribute => @attribute,
|
||||
:count => @options[:minimum])
|
||||
end
|
||||
|
@ -111,6 +112,7 @@ module Shoulda # :nodoc:
|
|||
if Symbol === @long_message
|
||||
@long_message = default_error_message(@long_message,
|
||||
:model_name => @subject.class.to_s.underscore,
|
||||
:instance => @subject,
|
||||
:attribute => @attribute,
|
||||
:count => @options[:maximum])
|
||||
end
|
||||
|
|
|
@ -26,7 +26,7 @@ module Shoulda # :nodoc:
|
|||
instance = options.delete(:instance)
|
||||
|
||||
if instance && instance.errors.respond_to?(:generate_message)
|
||||
instance.errors.generate_message(attribute.to_sym, key, {})
|
||||
instance.errors.generate_message(attribute.to_sym, key, options)
|
||||
else
|
||||
default_translation = [ :"activerecord.errors.models.#{model_name}.#{key}",
|
||||
:"activerecord.errors.messages.#{key}",
|
||||
|
|
|
@ -105,6 +105,52 @@ describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
|
|||
end
|
||||
end
|
||||
|
||||
context 'using translations' do
|
||||
after { I18n.backend.reload! }
|
||||
|
||||
context "a too_long translation containing %{attribute}, %{model}" do
|
||||
before do
|
||||
stub_translation(
|
||||
"activerecord.errors.messages.too_long",
|
||||
"The %{attribute} of your %{model} is too long (maximum is %{count} characters)")
|
||||
end
|
||||
|
||||
it "does not raise an exception" do
|
||||
expect {
|
||||
validating_length(:maximum => 4).should ensure_length_of(:attr).is_at_most(4)
|
||||
}.to_not raise_exception(I18n::MissingInterpolationArgument)
|
||||
end
|
||||
end
|
||||
|
||||
context "a too_short translation containing %{attribute}, %{model}" do
|
||||
before do
|
||||
stub_translation(
|
||||
"activerecord.errors.messages.too_short",
|
||||
"The %{attribute} of your %{model} is too short (minimum is %{count} characters)")
|
||||
end
|
||||
|
||||
it "does not raise an exception" do
|
||||
expect {
|
||||
validating_length(:minimum => 4).should ensure_length_of(:attr).is_at_least(4)
|
||||
}.to_not raise_exception(I18n::MissingInterpolationArgument)
|
||||
end
|
||||
end
|
||||
|
||||
context "a wrong_length translation containing %{attribute}, %{model}" do
|
||||
before do
|
||||
stub_translation(
|
||||
"activerecord.errors.messages.wrong_length",
|
||||
"The %{attribute} of your %{model} is the wrong length (should be %{count} characters)")
|
||||
end
|
||||
|
||||
it "does not raise an exception" do
|
||||
expect {
|
||||
validating_length(:is => 4).should ensure_length_of(:attr).is_equal_to(4)
|
||||
}.to_not raise_exception(I18n::MissingInterpolationArgument)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def validating_length(options = {})
|
||||
define_model(:example, :attr => :string) do
|
||||
validates_length_of :attr, options
|
||||
|
|
|
@ -82,6 +82,22 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
|
|||
end
|
||||
end
|
||||
|
||||
context "an i18n translation containing %{attribute} and %{model}" do
|
||||
before do
|
||||
stub_translation(
|
||||
"activerecord.errors.messages.blank",
|
||||
"Please enter a %{attribute} for your %{model}")
|
||||
end
|
||||
|
||||
after { I18n.backend.reload! }
|
||||
|
||||
it "does not raise an exception" do
|
||||
expect {
|
||||
validating_presence.should validate_presence_of(:attr)
|
||||
}.to_not raise_exception
|
||||
end
|
||||
end
|
||||
|
||||
if active_model_3_2?
|
||||
context 'a strictly required attribute' do
|
||||
it 'accepts when the :strict options match' do
|
||||
|
|
10
spec/support/i18n_faker.rb
Normal file
10
spec/support/i18n_faker.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
module I18nFaker
|
||||
def stub_translation(key, message)
|
||||
yml = key.split('.').reverse.inject(message) { |a, n| { n => a } }
|
||||
I18n.backend.store_translations(:en, yml)
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include I18nFaker
|
||||
end
|
Loading…
Reference in a new issue