From 8376dd2fbf1c5619d1914db755ca1ed99d1c24ff Mon Sep 17 00:00:00 2001 From: Dave Schweisguth Date: Sun, 25 May 2014 19:27:49 -0700 Subject: [PATCH] Emit BOOLEAN_ALLOWS_BOOLEAN_MESSAGE regardless of the order of the array of all booleans Documented ensure_inclusion_of change --- NEWS.md | 3 +++ .../validate_inclusion_of_matcher.rb | 2 +- .../validate_inclusion_of_matcher_spec.rb | 18 ++++++++++-------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index 91d125a0..ae813025 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,9 @@ * Fix `delegate_method` so that it does not raise an error if the method that returns the delegate object is private. +* Warn when `ensure_inclusion_of` is chained with `.in_array([false, true])` + as well as with `.in_array([true, false])`. + ### Improvements * `have_and_belongs_to_many` now checks to make sure that the join table diff --git a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb index 3a656397..bdb1af03 100644 --- a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb @@ -382,7 +382,7 @@ EOT def disallows_value_outside_of_array? if attribute_type == :boolean case @array - when [true, false] + when [false, true], [true, false] Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE return true when [nil] diff --git a/spec/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb index 9ca4897c..34195e3d 100644 --- a/spec/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb @@ -449,16 +449,18 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher do end end - it 'prints a warning' do - valid_values = [true, false] - builder = build_object_allowing(valid_values) - message = 'You are using `validate_inclusion_of` to assert that a boolean column allows boolean values and disallows non-boolean ones' + [[false, true], [true, false]].each do |booleans| + it 'prints a warning' do + valid_values = booleans + builder = build_object_allowing(valid_values) + message = 'You are using `validate_inclusion_of` to assert that a boolean column allows boolean values and disallows non-boolean ones' - stderr = capture(:stderr) do - expect_to_match_in_array(builder, valid_values) + stderr = capture(:stderr) do + expect_to_match_in_array(builder, valid_values) + end + + expect(stderr.gsub(/\n+/, ' ')).to include(message) end - - expect(stderr.gsub(/\n+/, ' ')).to include(message) end end end