diff --git a/lib/shoulda/matchers/active_model/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb index f63daa0d9..96853035a 100644 --- a/lib/shoulda/matchers/active_model/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -37,7 +37,7 @@ def with_message(message) def matches?(instance) @instance = instance if Symbol === @expected_message - @expected_message = default_error_message(@expected_message) + @expected_message = default_error_message(@expected_message, :model_name => @instance.class.to_s.underscore, :attribute => @attribute) end @instance.send("#{@attribute}=", @value) !errors_match? diff --git a/lib/shoulda/matchers/active_model/helpers.rb b/lib/shoulda/matchers/active_model/helpers.rb index d735d4187..c06f17ec1 100644 --- a/lib/shoulda/matchers/active_model/helpers.rb +++ b/lib/shoulda/matchers/active_model/helpers.rb @@ -16,11 +16,19 @@ def pretty_error_messages(obj) # :nodoc: # default_error_message(:blank) # default_error_message(:too_short, :count => 5) # default_error_message(:too_long, :count => 60) - def default_error_message(key, values = {}) - if Object.const_defined?(:I18n) # Rails >= 2.2 - I18n.translate(:"activerecord.errors.messages.#{key}", {:default => :"errors.messages.#{key}"}.merge(values)) + # default_error_message(:blank, :model_name => 'user', :attribute => 'name') + def default_error_message(key, options = {}) + model_name = options.delete(:model_name) + attribute = options.delete(:attribute) + if Object.const_defined?(:I18n) # Rails >= 2.2 + I18n.translate( :"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{key}", { + :default => [ :"activerecord.errors.models.#{model_name}.#{key}", + :"activerecord.errors.messages.#{key}", + :"errors.attributes.#{attribute}.#{key}", + :"errors.messages.#{key}" + ]}.merge(options)) else # Rails <= 2.1.x - ::ActiveRecord::Errors.default_error_messages[key] % values[:count] + ::ActiveRecord::Errors.default_error_messages[key] % options[:count] end end end diff --git a/spec/shoulda/active_model/helpers_spec.rb b/spec/shoulda/active_model/helpers_spec.rb new file mode 100644 index 000000000..22b17bd51 --- /dev/null +++ b/spec/shoulda/active_model/helpers_spec.rb @@ -0,0 +1,88 @@ +# encoding: UTF-8 +require "spec_helper" + +include Shoulda::Matchers::ActiveModel + +def store_translations(options = {:without => []}) + options[:without] ||= [] + options[:without] = [options[:without]] unless options[:without].is_a? Array + + translations = { + :activerecord => { + :errors => { + :models => { + :example => { + :attributes => { + :attr => {} + } + } + }, + :messages => {} + } + }, + :errors => { + :attributes => { + :attr => {} + }, + :messages => {} + }} + translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = 'Don’t you do that to me!' unless options[:without].include?(:model_attribute) + translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!' unless options[:without].include?(:model) + translations[:activerecord][:errors][:messages][:blank] = 'Oh no!' unless options[:without].include?(:message) + translations[:errors][:attributes][:attr][:blank] = 'Seriously?' unless options[:without].include?(:attribute) + + I18n.backend.store_translations :en, translations +end + +describe Shoulda::Matchers::ActiveModel::Helpers do + describe "default_error_message" do + before do + define_model :example, :attr => :string do + validates_presence_of :attr + end + @model = Example.new + end + + after { I18n.backend.reload! } + + context "if the translation for the model attribute’s error exists" do + it "provides the right error message" do + store_translations + @model.should validate_presence_of(:attr) + end + end + + context "if no translation for the model attribute’s error exists" do + context "and the translation for the model’s error exists" do + it "provides the right error message" do + store_translations :without => :model_attribute + @model.should validate_presence_of(:attr) + end + end + + context "and no translation for the model’s error exists" do + context "and the translation for the message exists" do + it "provides the right error message" do + store_translations :without => [:model_attribute, :model] + @model.should validate_presence_of(:attr) + end + end + + context "and no translation for the message exists" do + context "and the translation for the attribute exists" do + it "provides the right error message" do + store_translations :without => [:model_attribute, :model, :message] + @model.should validate_presence_of(:attr) + end + end + + context "and no translation for the attribute exists" do + it "provides the general error message" do + @model.should validate_presence_of(:attr) + end + end + end + end + end + end +end \ No newline at end of file