diff --git a/lib/shoulda/matchers/active_model/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb index 26d1ab12..29e724f1 100644 --- a/lib/shoulda/matchers/active_model/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -194,13 +194,37 @@ module Shoulda # :nodoc: def expected_message if @options.key?(:expected_message) if Symbol === @options[:expected_message] - default_error_message(@options[:expected_message], :model_name => model_name, :attribute => @attribute) + default_expected_message else @options[:expected_message] end end end + def default_expected_message + if strict? + default_full_message + else + default_attribute_message + end + end + + def default_full_message + "#{human_attribute_name} #{default_attribute_message}" + end + + def human_attribute_name + @instance.class.human_attribute_name(@attribute) + end + + def default_attribute_message + default_error_message( + @options[:expected_message], + :model_name => model_name, + :attribute => @attribute + ) + end + def model_name @instance.class.to_s.underscore end diff --git a/lib/shoulda/matchers/active_model/validation_matcher.rb b/lib/shoulda/matchers/active_model/validation_matcher.rb index 612f8dc2..01a1624a 100644 --- a/lib/shoulda/matchers/active_model/validation_matcher.rb +++ b/lib/shoulda/matchers/active_model/validation_matcher.rb @@ -6,6 +6,12 @@ module Shoulda # :nodoc: def initialize(attribute) @attribute = attribute + @strict = false + end + + def strict + @strict = true + self end def negative_failure_message @@ -24,6 +30,9 @@ module Shoulda # :nodoc: new(value). for(@attribute). with_message(message) + if strict? + allow = allow.strict + end if allow.matches?(@subject) @negative_failure_message = allow.failure_message true @@ -38,6 +47,9 @@ module Shoulda # :nodoc: new(value). for(@attribute). with_message(message) + if strict? + disallow = disallow.strict + end if disallow.matches?(@subject) @failure_message = disallow.negative_failure_message false @@ -46,6 +58,10 @@ module Shoulda # :nodoc: true end end + + def strict? + @strict + end end end end diff --git a/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb index f7bf8a4f..7f93afb9 100644 --- a/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +++ b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb @@ -120,4 +120,22 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(false) end end + + if Rails::VERSION::STRING.to_f >= 3.2 + context "a strict attribute which must be included in a range" do + before do + @model = define_model(:example, :attr => :integer) do + validates_inclusion_of :attr, :in => 2..5, :strict => true + end.new + end + + it "should accept ensuring the correct range" do + @model.should ensure_inclusion_of(:attr).in_range(2..5).strict + end + + it "should not accept ensuring another range" do + @model.should_not ensure_inclusion_of(:attr).in_range(2..6).strict + end + end + end end diff --git a/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb index de45106c..2f1b2145 100644 --- a/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb @@ -117,4 +117,19 @@ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do end end + if Rails::VERSION::STRING.to_f >= 3.2 + context "a strictly required attribute" do + before do + define_model :example, :attr => :string do + validates_presence_of :attr, :strict => true + end + @model = Example.new + end + + it "should require a value" do + @model.should validate_presence_of(:attr).strict + end + end + end + end