From 01c9d08c8eb9128c739132838e4eddfbc84f673d Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sat, 26 Apr 2014 11:10:17 -0400 Subject: [PATCH] allow_value.with_message takes i18n interp values --- NEWS.md | 8 ++++++++ .../active_model/allow_value_matcher.rb | 15 +++++++++++++-- .../active_model/allow_value_matcher_spec.rb | 18 ++++++++++++++++++ spec/support/activemodel_helpers.rb | 8 ++++++-- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index f85d260d..70cda544 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,13 @@ # HEAD +## Features + +* Teach `with_message` qualifier on `allow_value` to accept a hash of i18n + interpolation values: + `allow_value('foo').for(:attr).with_message(:greater_than, values: { count: 20 })`. + +## Bug fixes + * Fix `ComparisonMatcher` so that `validate_numericality_of` comparison matchers work with large numbers. diff --git a/lib/shoulda/matchers/active_model/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb index 89a32ca7..34a1ade7 100644 --- a/lib/shoulda/matchers/active_model/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -53,9 +53,12 @@ module Shoulda # :nodoc: def with_message(message, options={}) self.options[:expected_message] = message + self.options[:expected_message_values] = options.fetch(:values, {}) + if options.key?(:against) self.attribute_to_check_message_against = options[:against] end + self end @@ -173,10 +176,18 @@ module Shoulda # :nodoc: def default_attribute_message default_error_message( options[:expected_message], + default_attribute_message_values + ) + end + + def default_attribute_message_values + defaults = { model_name: model_name, instance: instance, - attribute: attribute_to_set - ) + attribute: attribute_to_set, + } + + defaults.merge(options[:expected_message_values]) end def model_name diff --git a/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb b/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb index e3f6254a..48781bb1 100644 --- a/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb @@ -68,6 +68,24 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do expect(validating_format(with: /abc/, message: 'bad value')). not_to allow_value('xyz').for(:attr).with_message(/bad/) end + + it 'allows interpolation values for the message to be provided' do + options = { + attribute_name: :attr, + attribute_type: :string + } + + record = record_with_custom_validation(options) do + if self.attr == 'xyz' + self.errors.add :attr, :greater_than, count: 2 + end + end + + expect(record). + not_to allow_value('xyz'). + for(:attr). + with_message(:greater_than, values: { count: 2 }) + end end context 'an attribute where the message occurs on another attribute' do diff --git a/spec/support/activemodel_helpers.rb b/spec/support/activemodel_helpers.rb index 84d2531a..29b2a95e 100644 --- a/spec/support/activemodel_helpers.rb +++ b/spec/support/activemodel_helpers.rb @@ -1,11 +1,15 @@ module ActiveModelHelpers - def custom_validation(&block) - define_model(:example, attr: :integer) do + def custom_validation(options = {}, &block) + attribute_name = options.fetch(:attribute_name, :attr) + attribute_type = options.fetch(:attribute_type, :integer) + + define_model(:example, attribute_name => attribute_type) do validate :custom_validation define_method(:custom_validation, &block) end.new end + alias record_with_custom_validation custom_validation def validating_format(options) define_model :example, attr: :string do