Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the qualifier allow_blank to validate_length_of #725

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -302,11 +302,6 @@ def in_range(range)
self
end

def allow_blank(allow_blank = true)
@options[:allow_blank] = allow_blank
self
end

def allow_nil(allow_nil = true)
@options[:allow_nil] = allow_nil
self
Expand Down Expand Up @@ -362,20 +357,11 @@ def matches_for_range?

def matches_for_array?
allows_all_values_in_array? &&
allows_blank_value? &&
allow_blank_matches? &&
allows_nil_value? &&
disallows_value_outside_of_array?
end

def allows_blank_value?
if @options.key?(:allow_blank)
blank_values = ['', ' ', "\n", "\r", "\t", "\f"]
@options[:allow_blank] == blank_values.all? { |value| allows_value_of(value) }
else
true
end
end

def allows_nil_value?
if @options.key?(:allow_nil)
@options[:allow_nil] == allows_value_of(nil)
Expand Down
23 changes: 22 additions & 1 deletion lib/shoulda/matchers/active_model/validate_length_of_matcher.rb
Expand Up @@ -216,6 +216,27 @@ module ActiveModel
# with_long_message('Secret key must be less than 100 characters')
# end
#
# ##### allow_blank
#
# Use `allow_blank` to assert that the attribute allows blank.
#
# class User
# include ActiveModel::Model
# attr_accessor :bio
#
# validates_length_of :bio, minimum: 15, allow_blank: true
# end
#
# # RSpec
# describe User do
# it { should validate_length_of(:bio).is_at_least(15).allow_blank }
# end
#
# # Test::Unit
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:bio).is_at_least(15).allow_blank
# end
#
# @return [ValidateLengthOfMatcher]
#
def validate_length_of(attr)
Expand Down Expand Up @@ -293,7 +314,7 @@ def description
def matches?(subject)
super(subject)
translate_messages!
lower_bound_matches? && upper_bound_matches?
lower_bound_matches? && upper_bound_matches? && allow_blank_matches?
end

private
Expand Down
14 changes: 14 additions & 0 deletions lib/shoulda/matchers/active_model/validation_matcher.rb
Expand Up @@ -19,6 +19,11 @@ def on(context)
self
end

def allow_blank
@options[:allow_blank] = true
self
end

def strict
@strict = true
self
Expand Down Expand Up @@ -95,6 +100,15 @@ def disallow_value_matcher(value, message)
def strict?
@strict
end

def allow_blank_matches?
if @options.key?(:allow_blank)
blank_values = ['', ' ', "\n", "\r", "\t", "\f"]
@options[:allow_blank] == blank_values.all? { |value| allows_value_of(value) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [90/80]

else
true
end
end
end
end
end
Expand Down
Expand Up @@ -161,6 +161,27 @@
end
end

context 'qualified with allow_blank' do
context 'and validating with allow_blank' do
it 'accepts' do
expect(validating_length(minimum: 2, allow_blank: true)).
to validate_length_of(:attr).is_at_least(2).allow_blank
end
end

context 'and not validating with allow_blank' do
it 'rejects' do
assertion = lambda do
expect(validating_length(minimum: 2)).
to validate_length_of(:attr).is_at_least(2).allow_blank
end
expect(&assertion).to fail_with_message_including(
%[Did not expect errors when attr is set to ""]
)
end
end
end

def validating_length(options = {})
define_model(:example, attr: :string) do
validates_length_of :attr, options
Expand Down