Refactored delegation of error message generation

Moved delegation to ActiveModel::Errors#generate_message to
default_error_message and implemented a spec for it.
This commit is contained in:
Daniel Duvall 2013-02-14 14:25:46 -08:00 committed by Jason Draper
parent c8c69716e1
commit 3dd52fbd61
4 changed files with 41 additions and 16 deletions

View File

@ -142,15 +142,12 @@ module Shoulda # :nodoc:
end
def default_attribute_message
if @instance.errors.respond_to?(:generate_message)
@instance.errors.generate_message(@attribute, @options[:expected_message])
else
default_error_message(
@options[:expected_message],
:model_name => model_name,
:attribute => @attribute
)
end
default_error_message(
@options[:expected_message],
:model_name => model_name,
:instance => @instance,
:attribute => @attribute
)
end
def model_name

View File

@ -11,21 +11,30 @@ module Shoulda # :nodoc:
# Helper method that determines the default error message used by Active
# Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
# introduced I18n module used for localization.
# introduced I18n module used for localization. Use with Rails 3.0 and
# up will delegate to ActiveModel::Errors.generate_error if a model
# instance is given.
#
# default_error_message(:blank)
# default_error_message(:too_short, :count => 5)
# default_error_message(:too_long, :count => 60)
# default_error_message(:blank, :model_name => 'user', :attribute => 'name')
# default_error_message(:blank, :instance => #<Model>, :attribute => 'name')
def default_error_message(key, options = {})
model_name = options.delete(:model_name)
attribute = options.delete(:attribute)
default_translation = [ :"activerecord.errors.models.#{model_name}.#{key}",
:"activerecord.errors.messages.#{key}",
:"errors.attributes.#{attribute}.#{key}",
:"errors.messages.#{key}" ]
I18n.translate(:"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{key}",
{ :default => default_translation }.merge(options))
instance = options.delete(:instance)
if instance && instance.errors.respond_to?(:generate_message)
instance.errors.generate_message(attribute.to_sym, key, {})
else
default_translation = [ :"activerecord.errors.models.#{model_name}.#{key}",
:"activerecord.errors.messages.#{key}",
:"errors.attributes.#{attribute}.#{key}",
:"errors.messages.#{key}" ]
I18n.translate(:"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{key}",
{ :default => default_translation }.merge(options))
end
end
end
end

View File

@ -77,6 +77,21 @@ describe Shoulda::Matchers::ActiveModel::Helpers do
end
end
end
if active_model_3_0?
context 'if ActiveModel::Errors.generate_message behavior has changed' do
subject { define_model(:divergent_example, :attr => :string) { validates_presence_of :attr }.new }
let(:message) { 'Oh snap. Behavior has diverged.' }
before(:each) do
store_translations
ActiveModel::Errors.any_instance.expects(:generate_message).with(:attr, :blank, {}).at_least_once.returns(message)
end
it { should validate_presence_of(:attr) }
end
end
end
def assert_presence_validation_has_correct_message

View File

@ -1,4 +1,8 @@
RSpec.configure do |c|
def active_model_3_0?
::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 0
end
def active_model_3_1?
::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1
end